xref: /openbmc/linux/fs/fs-writeback.c (revision 331cbdee)
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>
2503ba3782SJens Axboe #include <linux/freezer.h>
261da177e4SLinus Torvalds #include <linux/writeback.h>
271da177e4SLinus Torvalds #include <linux/blkdev.h>
281da177e4SLinus Torvalds #include <linux/backing-dev.h>
29455b2864SDave Chinner #include <linux/tracepoint.h>
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;
43d46db3d5SWu Fengguang 	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;
490e175a18SCurt Wohlgemuth 	enum wb_reason reason;		/* why was writeback initiated? */
50c4a77a6cSJens Axboe 
518010c3b6SJens Axboe 	struct list_head list;		/* pending work list */
5283ba7b07SChristoph Hellwig 	struct completion *done;	/* set if the caller waits */
5303ba3782SJens Axboe };
5403ba3782SJens Axboe 
55455b2864SDave Chinner /*
56455b2864SDave Chinner  * We don't actually have pdflush, but this one is exported though /proc...
57455b2864SDave Chinner  */
58455b2864SDave Chinner int nr_pdflush_threads;
59455b2864SDave Chinner 
60f11b00f3SAdrian Bunk /**
61f11b00f3SAdrian Bunk  * writeback_in_progress - determine whether there is writeback in progress
62f11b00f3SAdrian Bunk  * @bdi: the device's backing_dev_info structure.
63f11b00f3SAdrian Bunk  *
6403ba3782SJens Axboe  * Determine whether there is writeback waiting to be handled against a
6503ba3782SJens Axboe  * backing device.
66f11b00f3SAdrian Bunk  */
67f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi)
68f11b00f3SAdrian Bunk {
6981d73a32SJan Kara 	return test_bit(BDI_writeback_running, &bdi->state);
70f11b00f3SAdrian Bunk }
71f11b00f3SAdrian Bunk 
72692ebd17SJan Kara static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
73692ebd17SJan Kara {
74692ebd17SJan Kara 	struct super_block *sb = inode->i_sb;
75692ebd17SJan Kara 
76aaead25bSChristoph Hellwig 	if (strcmp(sb->s_type->name, "bdev") == 0)
77aaead25bSChristoph Hellwig 		return inode->i_mapping->backing_dev_info;
78aaead25bSChristoph Hellwig 
79692ebd17SJan Kara 	return sb->s_bdi;
80692ebd17SJan Kara }
81692ebd17SJan Kara 
827ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head)
837ccf19a8SNick Piggin {
847ccf19a8SNick Piggin 	return list_entry(head, struct inode, i_wb_list);
857ccf19a8SNick Piggin }
867ccf19a8SNick Piggin 
8715eb77a0SWu Fengguang /*
8815eb77a0SWu Fengguang  * Include the creation of the trace points after defining the
8915eb77a0SWu Fengguang  * wb_writeback_work structure and inline functions so that the definition
9015eb77a0SWu Fengguang  * remains local to this file.
9115eb77a0SWu Fengguang  */
9215eb77a0SWu Fengguang #define CREATE_TRACE_POINTS
9315eb77a0SWu Fengguang #include <trace/events/writeback.h>
9415eb77a0SWu Fengguang 
956585027aSJan Kara /* Wakeup flusher thread or forker thread to fork it. Requires bdi->wb_lock. */
966585027aSJan Kara static void bdi_wakeup_flusher(struct backing_dev_info *bdi)
974195f73dSNick Piggin {
98fff5b85aSArtem Bityutskiy 	if (bdi->wb.task) {
99fff5b85aSArtem Bityutskiy 		wake_up_process(bdi->wb.task);
100fff5b85aSArtem Bityutskiy 	} else {
1011da177e4SLinus Torvalds 		/*
102fff5b85aSArtem Bityutskiy 		 * The bdi thread isn't there, wake up the forker thread which
103fff5b85aSArtem Bityutskiy 		 * will create and run it.
1041da177e4SLinus Torvalds 		 */
10503ba3782SJens Axboe 		wake_up_process(default_backing_dev_info.wb.task);
1061da177e4SLinus Torvalds 	}
1076585027aSJan Kara }
1086585027aSJan Kara 
1096585027aSJan Kara static void bdi_queue_work(struct backing_dev_info *bdi,
1106585027aSJan Kara 			   struct wb_writeback_work *work)
1116585027aSJan Kara {
1126585027aSJan Kara 	trace_writeback_queue(bdi, work);
1136585027aSJan Kara 
1146585027aSJan Kara 	spin_lock_bh(&bdi->wb_lock);
1156585027aSJan Kara 	list_add_tail(&work->list, &bdi->work_list);
1166585027aSJan Kara 	if (!bdi->wb.task)
1176585027aSJan Kara 		trace_writeback_nothread(bdi, work);
1186585027aSJan Kara 	bdi_wakeup_flusher(bdi);
1196467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
12003ba3782SJens Axboe }
1211da177e4SLinus Torvalds 
12283ba7b07SChristoph Hellwig static void
12383ba7b07SChristoph Hellwig __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
1240e175a18SCurt Wohlgemuth 		      bool range_cyclic, enum wb_reason reason)
1251da177e4SLinus Torvalds {
12683ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
12703ba3782SJens Axboe 
128bcddc3f0SJens Axboe 	/*
129bcddc3f0SJens Axboe 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
130bcddc3f0SJens Axboe 	 * wakeup the thread for old dirty data writeback
131bcddc3f0SJens Axboe 	 */
13283ba7b07SChristoph Hellwig 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
13383ba7b07SChristoph Hellwig 	if (!work) {
134455b2864SDave Chinner 		if (bdi->wb.task) {
135455b2864SDave Chinner 			trace_writeback_nowork(bdi);
13683ba7b07SChristoph Hellwig 			wake_up_process(bdi->wb.task);
137455b2864SDave Chinner 		}
13883ba7b07SChristoph Hellwig 		return;
13983ba7b07SChristoph Hellwig 	}
14083ba7b07SChristoph Hellwig 
14183ba7b07SChristoph Hellwig 	work->sync_mode	= WB_SYNC_NONE;
14283ba7b07SChristoph Hellwig 	work->nr_pages	= nr_pages;
14383ba7b07SChristoph Hellwig 	work->range_cyclic = range_cyclic;
1440e175a18SCurt Wohlgemuth 	work->reason	= reason;
14583ba7b07SChristoph Hellwig 
146f11fcae8SJens Axboe 	bdi_queue_work(bdi, work);
14703ba3782SJens Axboe }
148b6e51316SJens Axboe 
149b6e51316SJens Axboe /**
150b6e51316SJens Axboe  * bdi_start_writeback - start writeback
151b6e51316SJens Axboe  * @bdi: the backing device to write from
152b6e51316SJens Axboe  * @nr_pages: the number of pages to write
153786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
154b6e51316SJens Axboe  *
155b6e51316SJens Axboe  * Description:
156b6e51316SJens Axboe  *   This does WB_SYNC_NONE opportunistic writeback. The IO is only
15725985edcSLucas De Marchi  *   started when this function returns, we make no guarantees on
1580e3c9a22SJens Axboe  *   completion. Caller need not hold sb s_umount semaphore.
159b6e51316SJens Axboe  *
160b6e51316SJens Axboe  */
1610e175a18SCurt Wohlgemuth void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
1620e175a18SCurt Wohlgemuth 			enum wb_reason reason)
163b6e51316SJens Axboe {
1640e175a18SCurt Wohlgemuth 	__bdi_start_writeback(bdi, nr_pages, true, reason);
165d3ddec76SWu Fengguang }
166d3ddec76SWu Fengguang 
167c5444198SChristoph Hellwig /**
168c5444198SChristoph Hellwig  * bdi_start_background_writeback - start background writeback
169c5444198SChristoph Hellwig  * @bdi: the backing device to write from
170c5444198SChristoph Hellwig  *
171c5444198SChristoph Hellwig  * Description:
1726585027aSJan Kara  *   This makes sure WB_SYNC_NONE background writeback happens. When
1736585027aSJan Kara  *   this function returns, it is only guaranteed that for given BDI
1746585027aSJan Kara  *   some IO is happening if we are over background dirty threshold.
1756585027aSJan Kara  *   Caller need not hold sb s_umount semaphore.
176c5444198SChristoph Hellwig  */
177c5444198SChristoph Hellwig void bdi_start_background_writeback(struct backing_dev_info *bdi)
178c5444198SChristoph Hellwig {
1796585027aSJan Kara 	/*
1806585027aSJan Kara 	 * We just wake up the flusher thread. It will perform background
1816585027aSJan Kara 	 * writeback as soon as there is no other work to do.
1826585027aSJan Kara 	 */
18371927e84SWu Fengguang 	trace_writeback_wake_background(bdi);
1846585027aSJan Kara 	spin_lock_bh(&bdi->wb_lock);
1856585027aSJan Kara 	bdi_wakeup_flusher(bdi);
1866585027aSJan Kara 	spin_unlock_bh(&bdi->wb_lock);
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds /*
190a66979abSDave Chinner  * Remove the inode from the writeback list it is on.
191a66979abSDave Chinner  */
192a66979abSDave Chinner void inode_wb_list_del(struct inode *inode)
193a66979abSDave Chinner {
194f758eeabSChristoph Hellwig 	struct backing_dev_info *bdi = inode_to_bdi(inode);
195a66979abSDave Chinner 
196f758eeabSChristoph Hellwig 	spin_lock(&bdi->wb.list_lock);
197f758eeabSChristoph Hellwig 	list_del_init(&inode->i_wb_list);
198f758eeabSChristoph Hellwig 	spin_unlock(&bdi->wb.list_lock);
199f758eeabSChristoph Hellwig }
200a66979abSDave Chinner 
201a66979abSDave Chinner /*
2026610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
2036610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
2046610a0bcSAndrew Morton  *
2056610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
20666f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
2076610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
2086610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
2096610a0bcSAndrew Morton  */
210f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
2116610a0bcSAndrew Morton {
212f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
21303ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
21466f3b8e2SJens Axboe 		struct inode *tail;
2156610a0bcSAndrew Morton 
2167ccf19a8SNick Piggin 		tail = wb_inode(wb->b_dirty.next);
21766f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
2186610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
2196610a0bcSAndrew Morton 	}
2207ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_dirty);
2216610a0bcSAndrew Morton }
2226610a0bcSAndrew Morton 
2236610a0bcSAndrew Morton /*
22466f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
225c986d1e2SAndrew Morton  */
226f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
227c986d1e2SAndrew Morton {
228f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
2297ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_more_io);
230c986d1e2SAndrew Morton }
231c986d1e2SAndrew Morton 
2321c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
2331c0eeaf5SJoern Engel {
234365b94aeSJan Kara 	inode->i_state &= ~I_SYNC;
235365b94aeSJan Kara 	/* Waiters must see I_SYNC cleared before being woken up */
2361c0eeaf5SJoern Engel 	smp_mb();
2371c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
2381c0eeaf5SJoern Engel }
2391c0eeaf5SJoern Engel 
240d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
241d2caa3c5SJeff Layton {
242d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
243d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
244d2caa3c5SJeff Layton 	/*
245d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
246d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
247d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
2485b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
249d2caa3c5SJeff Layton 	 */
250d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
251d2caa3c5SJeff Layton #endif
252d2caa3c5SJeff Layton 	return ret;
253d2caa3c5SJeff Layton }
254d2caa3c5SJeff Layton 
255c986d1e2SAndrew Morton /*
256697e6fedSJan Kara  * Move expired (dirtied after work->older_than_this) dirty inodes from
257697e6fedSJan Kara  * @delaying_queue to @dispatch_queue.
2582c136579SFengguang Wu  */
259e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue,
2602c136579SFengguang Wu 			       struct list_head *dispatch_queue,
261ad4e38ddSCurt Wohlgemuth 			       struct wb_writeback_work *work)
2622c136579SFengguang Wu {
2635c03449dSShaohua Li 	LIST_HEAD(tmp);
2645c03449dSShaohua Li 	struct list_head *pos, *node;
265cf137307SJens Axboe 	struct super_block *sb = NULL;
2665c03449dSShaohua Li 	struct inode *inode;
267cf137307SJens Axboe 	int do_sb_sort = 0;
268e84d0a4fSWu Fengguang 	int moved = 0;
2695c03449dSShaohua Li 
2702c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
2717ccf19a8SNick Piggin 		inode = wb_inode(delaying_queue->prev);
272ad4e38ddSCurt Wohlgemuth 		if (work->older_than_this &&
273ad4e38ddSCurt Wohlgemuth 		    inode_dirtied_after(inode, *work->older_than_this))
2742c136579SFengguang Wu 			break;
275cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
276cf137307SJens Axboe 			do_sb_sort = 1;
277cf137307SJens Axboe 		sb = inode->i_sb;
2787ccf19a8SNick Piggin 		list_move(&inode->i_wb_list, &tmp);
279e84d0a4fSWu Fengguang 		moved++;
2805c03449dSShaohua Li 	}
2815c03449dSShaohua Li 
282cf137307SJens Axboe 	/* just one sb in list, splice to dispatch_queue and we're done */
283cf137307SJens Axboe 	if (!do_sb_sort) {
284cf137307SJens Axboe 		list_splice(&tmp, dispatch_queue);
285e84d0a4fSWu Fengguang 		goto out;
286cf137307SJens Axboe 	}
287cf137307SJens Axboe 
2885c03449dSShaohua Li 	/* Move inodes from one superblock together */
2895c03449dSShaohua Li 	while (!list_empty(&tmp)) {
2907ccf19a8SNick Piggin 		sb = wb_inode(tmp.prev)->i_sb;
2915c03449dSShaohua Li 		list_for_each_prev_safe(pos, node, &tmp) {
2927ccf19a8SNick Piggin 			inode = wb_inode(pos);
2935c03449dSShaohua Li 			if (inode->i_sb == sb)
2947ccf19a8SNick Piggin 				list_move(&inode->i_wb_list, dispatch_queue);
2952c136579SFengguang Wu 		}
2962c136579SFengguang Wu 	}
297e84d0a4fSWu Fengguang out:
298e84d0a4fSWu Fengguang 	return moved;
2995c03449dSShaohua Li }
3002c136579SFengguang Wu 
3012c136579SFengguang Wu /*
3022c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
3034ea879b9SWu Fengguang  * Before
3044ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
3054ea879b9SWu Fengguang  *         =============>    gf         edc     BA
3064ea879b9SWu Fengguang  * After
3074ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
3084ea879b9SWu Fengguang  *         =============>    g          fBAedc
3094ea879b9SWu Fengguang  *                                           |
3104ea879b9SWu Fengguang  *                                           +--> dequeue for IO
3112c136579SFengguang Wu  */
312ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
3132c136579SFengguang Wu {
314e84d0a4fSWu Fengguang 	int moved;
315f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
3164ea879b9SWu Fengguang 	list_splice_init(&wb->b_more_io, &wb->b_io);
317ad4e38ddSCurt Wohlgemuth 	moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, work);
318ad4e38ddSCurt Wohlgemuth 	trace_writeback_queue_io(wb, work, moved);
31966f3b8e2SJens Axboe }
32066f3b8e2SJens Axboe 
321a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc)
32266f3b8e2SJens Axboe {
32303ba3782SJens Axboe 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
324a9185b41SChristoph Hellwig 		return inode->i_sb->s_op->write_inode(inode, wbc);
32503ba3782SJens Axboe 	return 0;
32666f3b8e2SJens Axboe }
32708d8e974SFengguang Wu 
3282c136579SFengguang Wu /*
329169ebd90SJan Kara  * Wait for writeback on an inode to complete. Called with i_lock held.
330169ebd90SJan Kara  * Caller must make sure inode cannot go away when we drop i_lock.
33101c03194SChristoph Hellwig  */
332169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode)
333169ebd90SJan Kara 	__releases(inode->i_lock)
334169ebd90SJan Kara 	__acquires(inode->i_lock)
33501c03194SChristoph Hellwig {
33601c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
33701c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
33801c03194SChristoph Hellwig 
33901c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
34058a9d3d8SRichard Kennedy 	while (inode->i_state & I_SYNC) {
341250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
34201c03194SChristoph Hellwig 		__wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
343250df6edSDave Chinner 		spin_lock(&inode->i_lock);
34458a9d3d8SRichard Kennedy 	}
34501c03194SChristoph Hellwig }
34601c03194SChristoph Hellwig 
34701c03194SChristoph Hellwig /*
348169ebd90SJan Kara  * Wait for writeback on an inode to complete. Caller must have inode pinned.
349169ebd90SJan Kara  */
350169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode)
351169ebd90SJan Kara {
352169ebd90SJan Kara 	spin_lock(&inode->i_lock);
353169ebd90SJan Kara 	__inode_wait_for_writeback(inode);
354169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
355169ebd90SJan Kara }
356169ebd90SJan Kara 
357169ebd90SJan Kara /*
358169ebd90SJan Kara  * Sleep until I_SYNC is cleared. This function must be called with i_lock
359169ebd90SJan Kara  * held and drops it. It is aimed for callers not holding any inode reference
360169ebd90SJan Kara  * so once i_lock is dropped, inode can go away.
361169ebd90SJan Kara  */
362169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode)
363169ebd90SJan Kara 	__releases(inode->i_lock)
364169ebd90SJan Kara {
365169ebd90SJan Kara 	DEFINE_WAIT(wait);
366169ebd90SJan Kara 	wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
367169ebd90SJan Kara 	int sleep;
368169ebd90SJan Kara 
369169ebd90SJan Kara 	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
370169ebd90SJan Kara 	sleep = inode->i_state & I_SYNC;
371169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
372169ebd90SJan Kara 	if (sleep)
373169ebd90SJan Kara 		schedule();
374169ebd90SJan Kara 	finish_wait(wqh, &wait);
375169ebd90SJan Kara }
376169ebd90SJan Kara 
377169ebd90SJan Kara /*
378ccb26b5aSJan Kara  * Find proper writeback list for the inode depending on its current state and
379ccb26b5aSJan Kara  * possibly also change of its state while we were doing writeback.  Here we
380ccb26b5aSJan Kara  * handle things such as livelock prevention or fairness of writeback among
381ccb26b5aSJan Kara  * inodes. This function can be called only by flusher thread - noone else
382ccb26b5aSJan Kara  * processes all inodes in writeback lists and requeueing inodes behind flusher
383ccb26b5aSJan Kara  * thread's back can have unexpected consequences.
384ccb26b5aSJan Kara  */
385ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
386ccb26b5aSJan Kara 			  struct writeback_control *wbc)
387ccb26b5aSJan Kara {
388ccb26b5aSJan Kara 	if (inode->i_state & I_FREEING)
389ccb26b5aSJan Kara 		return;
390ccb26b5aSJan Kara 
391ccb26b5aSJan Kara 	/*
392ccb26b5aSJan Kara 	 * Sync livelock prevention. Each inode is tagged and synced in one
393ccb26b5aSJan Kara 	 * shot. If still dirty, it will be redirty_tail()'ed below.  Update
394ccb26b5aSJan Kara 	 * the dirty time to prevent enqueue and sync it again.
395ccb26b5aSJan Kara 	 */
396ccb26b5aSJan Kara 	if ((inode->i_state & I_DIRTY) &&
397ccb26b5aSJan Kara 	    (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
398ccb26b5aSJan Kara 		inode->dirtied_when = jiffies;
399ccb26b5aSJan Kara 
4004f8ad655SJan Kara 	if (wbc->pages_skipped) {
4014f8ad655SJan Kara 		/*
4024f8ad655SJan Kara 		 * writeback is not making progress due to locked
4034f8ad655SJan Kara 		 * buffers. Skip this inode for now.
4044f8ad655SJan Kara 		 */
4054f8ad655SJan Kara 		redirty_tail(inode, wb);
4064f8ad655SJan Kara 		return;
4074f8ad655SJan Kara 	}
4084f8ad655SJan Kara 
409ccb26b5aSJan Kara 	if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
410ccb26b5aSJan Kara 		/*
411ccb26b5aSJan Kara 		 * We didn't write back all the pages.  nfs_writepages()
412ccb26b5aSJan Kara 		 * sometimes bales out without doing anything.
413ccb26b5aSJan Kara 		 */
414ccb26b5aSJan Kara 		if (wbc->nr_to_write <= 0) {
415ccb26b5aSJan Kara 			/* Slice used up. Queue for next turn. */
416ccb26b5aSJan Kara 			requeue_io(inode, wb);
417ccb26b5aSJan Kara 		} else {
418ccb26b5aSJan Kara 			/*
419ccb26b5aSJan Kara 			 * Writeback blocked by something other than
420ccb26b5aSJan Kara 			 * congestion. Delay the inode for some time to
421ccb26b5aSJan Kara 			 * avoid spinning on the CPU (100% iowait)
422ccb26b5aSJan Kara 			 * retrying writeback of the dirty page/inode
423ccb26b5aSJan Kara 			 * that cannot be performed immediately.
424ccb26b5aSJan Kara 			 */
425ccb26b5aSJan Kara 			redirty_tail(inode, wb);
426ccb26b5aSJan Kara 		}
427ccb26b5aSJan Kara 	} else if (inode->i_state & I_DIRTY) {
428ccb26b5aSJan Kara 		/*
429ccb26b5aSJan Kara 		 * Filesystems can dirty the inode during writeback operations,
430ccb26b5aSJan Kara 		 * such as delayed allocation during submission or metadata
431ccb26b5aSJan Kara 		 * updates after data IO completion.
432ccb26b5aSJan Kara 		 */
433ccb26b5aSJan Kara 		redirty_tail(inode, wb);
434ccb26b5aSJan Kara 	} else {
435ccb26b5aSJan Kara 		/* The inode is clean. Remove from writeback lists. */
436ccb26b5aSJan Kara 		list_del_init(&inode->i_wb_list);
437ccb26b5aSJan Kara 	}
438ccb26b5aSJan Kara }
439ccb26b5aSJan Kara 
440ccb26b5aSJan Kara /*
4414f8ad655SJan Kara  * Write out an inode and its dirty pages. Do not update the writeback list
4424f8ad655SJan Kara  * linkage. That is left to the caller. The caller is also responsible for
4434f8ad655SJan Kara  * setting I_SYNC flag and calling inode_sync_complete() to clear it.
4441da177e4SLinus Torvalds  */
4451da177e4SLinus Torvalds static int
4464f8ad655SJan Kara __writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
447f758eeabSChristoph Hellwig 			 struct writeback_control *wbc)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
450251d6a47SWu Fengguang 	long nr_to_write = wbc->nr_to_write;
45101c03194SChristoph Hellwig 	unsigned dirty;
4521da177e4SLinus Torvalds 	int ret;
4531da177e4SLinus Torvalds 
4544f8ad655SJan Kara 	WARN_ON(!(inode->i_state & I_SYNC));
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
4571da177e4SLinus Torvalds 
45826821ed4SChristoph Hellwig 	/*
45926821ed4SChristoph Hellwig 	 * Make sure to wait on the data before writing out the metadata.
46026821ed4SChristoph Hellwig 	 * This is important for filesystems that modify metadata on data
46126821ed4SChristoph Hellwig 	 * I/O completion.
46226821ed4SChristoph Hellwig 	 */
463a9185b41SChristoph Hellwig 	if (wbc->sync_mode == WB_SYNC_ALL) {
46426821ed4SChristoph Hellwig 		int err = filemap_fdatawait(mapping);
4651da177e4SLinus Torvalds 		if (ret == 0)
4661da177e4SLinus Torvalds 			ret = err;
4671da177e4SLinus Torvalds 	}
4681da177e4SLinus Torvalds 
4695547e8aaSDmitry Monakhov 	/*
4705547e8aaSDmitry Monakhov 	 * Some filesystems may redirty the inode during the writeback
4715547e8aaSDmitry Monakhov 	 * due to delalloc, clear dirty metadata flags right before
4725547e8aaSDmitry Monakhov 	 * write_inode()
4735547e8aaSDmitry Monakhov 	 */
474250df6edSDave Chinner 	spin_lock(&inode->i_lock);
4756290be1cSJan Kara 	/* Clear I_DIRTY_PAGES if we've written out all dirty pages */
4766290be1cSJan Kara 	if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
4776290be1cSJan Kara 		inode->i_state &= ~I_DIRTY_PAGES;
4785547e8aaSDmitry Monakhov 	dirty = inode->i_state & I_DIRTY;
4795547e8aaSDmitry Monakhov 	inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC);
480250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
48126821ed4SChristoph Hellwig 	/* Don't write the inode if only I_DIRTY_PAGES was set */
48226821ed4SChristoph Hellwig 	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
483a9185b41SChristoph Hellwig 		int err = write_inode(inode, wbc);
4841da177e4SLinus Torvalds 		if (ret == 0)
4851da177e4SLinus Torvalds 			ret = err;
4861da177e4SLinus Torvalds 	}
4874f8ad655SJan Kara 	trace_writeback_single_inode(inode, wbc, nr_to_write);
4884f8ad655SJan Kara 	return ret;
4894f8ad655SJan Kara }
4904f8ad655SJan Kara 
4914f8ad655SJan Kara /*
4924f8ad655SJan Kara  * Write out an inode's dirty pages. Either the caller has an active reference
4934f8ad655SJan Kara  * on the inode or the inode has I_WILL_FREE set.
4944f8ad655SJan Kara  *
4954f8ad655SJan Kara  * This function is designed to be called for writing back one inode which
4964f8ad655SJan Kara  * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode()
4974f8ad655SJan Kara  * and does more profound writeback list handling in writeback_sb_inodes().
4984f8ad655SJan Kara  */
4994f8ad655SJan Kara static int
5004f8ad655SJan Kara writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
5014f8ad655SJan Kara 		       struct writeback_control *wbc)
5024f8ad655SJan Kara {
5034f8ad655SJan Kara 	int ret = 0;
5044f8ad655SJan Kara 
5054f8ad655SJan Kara 	spin_lock(&inode->i_lock);
5064f8ad655SJan Kara 	if (!atomic_read(&inode->i_count))
5074f8ad655SJan Kara 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
5084f8ad655SJan Kara 	else
5094f8ad655SJan Kara 		WARN_ON(inode->i_state & I_WILL_FREE);
5104f8ad655SJan Kara 
5114f8ad655SJan Kara 	if (inode->i_state & I_SYNC) {
5124f8ad655SJan Kara 		if (wbc->sync_mode != WB_SYNC_ALL)
5134f8ad655SJan Kara 			goto out;
5144f8ad655SJan Kara 		/*
515169ebd90SJan Kara 		 * It's a data-integrity sync. We must wait. Since callers hold
516169ebd90SJan Kara 		 * inode reference or inode has I_WILL_FREE set, it cannot go
517169ebd90SJan Kara 		 * away under us.
5184f8ad655SJan Kara 		 */
519169ebd90SJan Kara 		__inode_wait_for_writeback(inode);
5204f8ad655SJan Kara 	}
5214f8ad655SJan Kara 	WARN_ON(inode->i_state & I_SYNC);
5224f8ad655SJan Kara 	/*
5234f8ad655SJan Kara 	 * Skip inode if it is clean. We don't want to mess with writeback
5244f8ad655SJan Kara 	 * lists in this function since flusher thread may be doing for example
5254f8ad655SJan Kara 	 * sync in parallel and if we move the inode, it could get skipped. So
5264f8ad655SJan Kara 	 * here we make sure inode is on some writeback list and leave it there
5274f8ad655SJan Kara 	 * unless we have completely cleaned the inode.
5284f8ad655SJan Kara 	 */
5294f8ad655SJan Kara 	if (!(inode->i_state & I_DIRTY))
5304f8ad655SJan Kara 		goto out;
5314f8ad655SJan Kara 	inode->i_state |= I_SYNC;
5324f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
5334f8ad655SJan Kara 
5344f8ad655SJan Kara 	ret = __writeback_single_inode(inode, wb, wbc);
5351da177e4SLinus Torvalds 
536f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
537250df6edSDave Chinner 	spin_lock(&inode->i_lock);
5384f8ad655SJan Kara 	/*
5394f8ad655SJan Kara 	 * If inode is clean, remove it from writeback lists. Otherwise don't
5404f8ad655SJan Kara 	 * touch it. See comment above for explanation.
5414f8ad655SJan Kara 	 */
5424f8ad655SJan Kara 	if (!(inode->i_state & I_DIRTY))
5434f8ad655SJan Kara 		list_del_init(&inode->i_wb_list);
5444f8ad655SJan Kara 	spin_unlock(&wb->list_lock);
5451c0eeaf5SJoern Engel 	inode_sync_complete(inode);
5464f8ad655SJan Kara out:
5474f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
5481da177e4SLinus Torvalds 	return ret;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511a12d8bdSWu Fengguang static long writeback_chunk_size(struct backing_dev_info *bdi,
5521a12d8bdSWu Fengguang 				 struct wb_writeback_work *work)
553d46db3d5SWu Fengguang {
554d46db3d5SWu Fengguang 	long pages;
555d46db3d5SWu Fengguang 
556d46db3d5SWu Fengguang 	/*
557d46db3d5SWu Fengguang 	 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
558d46db3d5SWu Fengguang 	 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
559d46db3d5SWu Fengguang 	 * here avoids calling into writeback_inodes_wb() more than once.
560d46db3d5SWu Fengguang 	 *
561d46db3d5SWu Fengguang 	 * The intended call sequence for WB_SYNC_ALL writeback is:
562d46db3d5SWu Fengguang 	 *
563d46db3d5SWu Fengguang 	 *      wb_writeback()
564d46db3d5SWu Fengguang 	 *          writeback_sb_inodes()       <== called only once
565d46db3d5SWu Fengguang 	 *              write_cache_pages()     <== called once for each inode
566d46db3d5SWu Fengguang 	 *                   (quickly) tag currently dirty pages
567d46db3d5SWu Fengguang 	 *                   (maybe slowly) sync all tagged pages
568d46db3d5SWu Fengguang 	 */
569d46db3d5SWu Fengguang 	if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
570d46db3d5SWu Fengguang 		pages = LONG_MAX;
5711a12d8bdSWu Fengguang 	else {
5721a12d8bdSWu Fengguang 		pages = min(bdi->avg_write_bandwidth / 2,
5731a12d8bdSWu Fengguang 			    global_dirty_limit / DIRTY_SCOPE);
5741a12d8bdSWu Fengguang 		pages = min(pages, work->nr_pages);
5751a12d8bdSWu Fengguang 		pages = round_down(pages + MIN_WRITEBACK_PAGES,
5761a12d8bdSWu Fengguang 				   MIN_WRITEBACK_PAGES);
5771a12d8bdSWu Fengguang 	}
578d46db3d5SWu Fengguang 
579d46db3d5SWu Fengguang 	return pages;
580d46db3d5SWu Fengguang }
581d46db3d5SWu Fengguang 
58203ba3782SJens Axboe /*
583f11c9c5cSEdward Shishkin  * Write a portion of b_io inodes which belong to @sb.
584edadfb10SChristoph Hellwig  *
585edadfb10SChristoph Hellwig  * If @only_this_sb is true, then find and write all such
586f11c9c5cSEdward Shishkin  * inodes. Otherwise write only ones which go sequentially
587f11c9c5cSEdward Shishkin  * in reverse order.
588edadfb10SChristoph Hellwig  *
589d46db3d5SWu Fengguang  * Return the number of pages and/or inodes written.
590f11c9c5cSEdward Shishkin  */
591d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb,
592d46db3d5SWu Fengguang 				struct bdi_writeback *wb,
593d46db3d5SWu Fengguang 				struct wb_writeback_work *work)
59403ba3782SJens Axboe {
595d46db3d5SWu Fengguang 	struct writeback_control wbc = {
596d46db3d5SWu Fengguang 		.sync_mode		= work->sync_mode,
597d46db3d5SWu Fengguang 		.tagged_writepages	= work->tagged_writepages,
598d46db3d5SWu Fengguang 		.for_kupdate		= work->for_kupdate,
599d46db3d5SWu Fengguang 		.for_background		= work->for_background,
600d46db3d5SWu Fengguang 		.range_cyclic		= work->range_cyclic,
601d46db3d5SWu Fengguang 		.range_start		= 0,
602d46db3d5SWu Fengguang 		.range_end		= LLONG_MAX,
603d46db3d5SWu Fengguang 	};
604d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
605d46db3d5SWu Fengguang 	long write_chunk;
606d46db3d5SWu Fengguang 	long wrote = 0;  /* count both pages and inodes */
607d46db3d5SWu Fengguang 
60803ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
6097ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
610edadfb10SChristoph Hellwig 
611edadfb10SChristoph Hellwig 		if (inode->i_sb != sb) {
612d46db3d5SWu Fengguang 			if (work->sb) {
613edadfb10SChristoph Hellwig 				/*
614edadfb10SChristoph Hellwig 				 * We only want to write back data for this
615edadfb10SChristoph Hellwig 				 * superblock, move all inodes not belonging
616edadfb10SChristoph Hellwig 				 * to it back onto the dirty list.
617edadfb10SChristoph Hellwig 				 */
618f758eeabSChristoph Hellwig 				redirty_tail(inode, wb);
61966f3b8e2SJens Axboe 				continue;
62066f3b8e2SJens Axboe 			}
621edadfb10SChristoph Hellwig 
622edadfb10SChristoph Hellwig 			/*
623edadfb10SChristoph Hellwig 			 * The inode belongs to a different superblock.
624edadfb10SChristoph Hellwig 			 * Bounce back to the caller to unpin this and
625edadfb10SChristoph Hellwig 			 * pin the next superblock.
626edadfb10SChristoph Hellwig 			 */
627d46db3d5SWu Fengguang 			break;
628edadfb10SChristoph Hellwig 		}
629edadfb10SChristoph Hellwig 
6309843b76aSChristoph Hellwig 		/*
631331cbdeeSWanpeng Li 		 * Don't bother with new inodes or inodes being freed, first
632331cbdeeSWanpeng Li 		 * kind does not need periodic writeout yet, and for the latter
6339843b76aSChristoph Hellwig 		 * kind writeout is handled by the freer.
6349843b76aSChristoph Hellwig 		 */
635250df6edSDave Chinner 		spin_lock(&inode->i_lock);
6369843b76aSChristoph Hellwig 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
637250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
638fcc5c222SWu Fengguang 			redirty_tail(inode, wb);
6397ef0d737SNick Piggin 			continue;
6407ef0d737SNick Piggin 		}
641cc1676d9SJan Kara 		if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) {
642cc1676d9SJan Kara 			/*
643cc1676d9SJan Kara 			 * If this inode is locked for writeback and we are not
644cc1676d9SJan Kara 			 * doing writeback-for-data-integrity, move it to
645cc1676d9SJan Kara 			 * b_more_io so that writeback can proceed with the
646cc1676d9SJan Kara 			 * other inodes on s_io.
647cc1676d9SJan Kara 			 *
648cc1676d9SJan Kara 			 * We'll have another go at writing back this inode
649cc1676d9SJan Kara 			 * when we completed a full scan of b_io.
650cc1676d9SJan Kara 			 */
651cc1676d9SJan Kara 			spin_unlock(&inode->i_lock);
652cc1676d9SJan Kara 			requeue_io(inode, wb);
653cc1676d9SJan Kara 			trace_writeback_sb_inodes_requeue(inode);
654cc1676d9SJan Kara 			continue;
655cc1676d9SJan Kara 		}
656f0d07b7fSJan Kara 		spin_unlock(&wb->list_lock);
657f0d07b7fSJan Kara 
6584f8ad655SJan Kara 		/*
6594f8ad655SJan Kara 		 * We already requeued the inode if it had I_SYNC set and we
6604f8ad655SJan Kara 		 * are doing WB_SYNC_NONE writeback. So this catches only the
6614f8ad655SJan Kara 		 * WB_SYNC_ALL case.
6624f8ad655SJan Kara 		 */
663169ebd90SJan Kara 		if (inode->i_state & I_SYNC) {
664169ebd90SJan Kara 			/* Wait for I_SYNC. This function drops i_lock... */
665169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
666169ebd90SJan Kara 			/* Inode may be gone, start again */
667ead188f9SJan Kara 			spin_lock(&wb->list_lock);
668169ebd90SJan Kara 			continue;
669169ebd90SJan Kara 		}
6704f8ad655SJan Kara 		inode->i_state |= I_SYNC;
6714f8ad655SJan Kara 		spin_unlock(&inode->i_lock);
672169ebd90SJan Kara 
6731a12d8bdSWu Fengguang 		write_chunk = writeback_chunk_size(wb->bdi, work);
674d46db3d5SWu Fengguang 		wbc.nr_to_write = write_chunk;
675d46db3d5SWu Fengguang 		wbc.pages_skipped = 0;
676250df6edSDave Chinner 
677169ebd90SJan Kara 		/*
678169ebd90SJan Kara 		 * We use I_SYNC to pin the inode in memory. While it is set
679169ebd90SJan Kara 		 * evict_inode() will wait so the inode cannot be freed.
680169ebd90SJan Kara 		 */
6814f8ad655SJan Kara 		__writeback_single_inode(inode, wb, &wbc);
682d46db3d5SWu Fengguang 
683d46db3d5SWu Fengguang 		work->nr_pages -= write_chunk - wbc.nr_to_write;
684d46db3d5SWu Fengguang 		wrote += write_chunk - wbc.nr_to_write;
6854f8ad655SJan Kara 		spin_lock(&wb->list_lock);
6864f8ad655SJan Kara 		spin_lock(&inode->i_lock);
687d46db3d5SWu Fengguang 		if (!(inode->i_state & I_DIRTY))
688d46db3d5SWu Fengguang 			wrote++;
6894f8ad655SJan Kara 		requeue_inode(inode, wb, &wbc);
6904f8ad655SJan Kara 		inode_sync_complete(inode);
6910f1b1fd8SDave Chinner 		spin_unlock(&inode->i_lock);
692169ebd90SJan Kara 		cond_resched_lock(&wb->list_lock);
693d46db3d5SWu Fengguang 		/*
694d46db3d5SWu Fengguang 		 * bail out to wb_writeback() often enough to check
695d46db3d5SWu Fengguang 		 * background threshold and other termination conditions.
696d46db3d5SWu Fengguang 		 */
697d46db3d5SWu Fengguang 		if (wrote) {
698d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
699d46db3d5SWu Fengguang 				break;
700d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
701d46db3d5SWu Fengguang 				break;
7021da177e4SLinus Torvalds 		}
7038bc3be27SFengguang Wu 	}
704d46db3d5SWu Fengguang 	return wrote;
705f11c9c5cSEdward Shishkin }
70638f21977SNick Piggin 
707d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb,
708d46db3d5SWu Fengguang 				  struct wb_writeback_work *work)
709f11c9c5cSEdward Shishkin {
710d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
711d46db3d5SWu Fengguang 	long wrote = 0;
712f11c9c5cSEdward Shishkin 
713f11c9c5cSEdward Shishkin 	while (!list_empty(&wb->b_io)) {
7147ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
715f11c9c5cSEdward Shishkin 		struct super_block *sb = inode->i_sb;
716f11c9c5cSEdward Shishkin 
71712ad3ab6SDave Chinner 		if (!grab_super_passive(sb)) {
7180e995816SWu Fengguang 			/*
7190e995816SWu Fengguang 			 * grab_super_passive() may fail consistently due to
7200e995816SWu Fengguang 			 * s_umount being grabbed by someone else. Don't use
7210e995816SWu Fengguang 			 * requeue_io() to avoid busy retrying the inode/sb.
7220e995816SWu Fengguang 			 */
7230e995816SWu Fengguang 			redirty_tail(inode, wb);
724d19de7edSChristoph Hellwig 			continue;
725334132aeSChristoph Hellwig 		}
726d46db3d5SWu Fengguang 		wrote += writeback_sb_inodes(sb, wb, work);
727d19de7edSChristoph Hellwig 		drop_super(sb);
728f11c9c5cSEdward Shishkin 
729d46db3d5SWu Fengguang 		/* refer to the same tests at the end of writeback_sb_inodes */
730d46db3d5SWu Fengguang 		if (wrote) {
731d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
732d46db3d5SWu Fengguang 				break;
733d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
734f11c9c5cSEdward Shishkin 				break;
735f11c9c5cSEdward Shishkin 		}
736d46db3d5SWu Fengguang 	}
73766f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
738d46db3d5SWu Fengguang 	return wrote;
73966f3b8e2SJens Axboe }
74066f3b8e2SJens Axboe 
7410e175a18SCurt Wohlgemuth long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
7420e175a18SCurt Wohlgemuth 				enum wb_reason reason)
743edadfb10SChristoph Hellwig {
744d46db3d5SWu Fengguang 	struct wb_writeback_work work = {
745d46db3d5SWu Fengguang 		.nr_pages	= nr_pages,
746d46db3d5SWu Fengguang 		.sync_mode	= WB_SYNC_NONE,
747d46db3d5SWu Fengguang 		.range_cyclic	= 1,
7480e175a18SCurt Wohlgemuth 		.reason		= reason,
749d46db3d5SWu Fengguang 	};
750edadfb10SChristoph Hellwig 
751f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
752424b351fSWu Fengguang 	if (list_empty(&wb->b_io))
753ad4e38ddSCurt Wohlgemuth 		queue_io(wb, &work);
754d46db3d5SWu Fengguang 	__writeback_inodes_wb(wb, &work);
755f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
756edadfb10SChristoph Hellwig 
757d46db3d5SWu Fengguang 	return nr_pages - work.nr_pages;
75866f3b8e2SJens Axboe }
75966f3b8e2SJens Axboe 
760b00949aaSWu Fengguang static bool over_bground_thresh(struct backing_dev_info *bdi)
76103ba3782SJens Axboe {
76203ba3782SJens Axboe 	unsigned long background_thresh, dirty_thresh;
76303ba3782SJens Axboe 
76416c4042fSWu Fengguang 	global_dirty_limits(&background_thresh, &dirty_thresh);
76503ba3782SJens Axboe 
766b00949aaSWu Fengguang 	if (global_page_state(NR_FILE_DIRTY) +
767b00949aaSWu Fengguang 	    global_page_state(NR_UNSTABLE_NFS) > background_thresh)
768b00949aaSWu Fengguang 		return true;
769b00949aaSWu Fengguang 
770b00949aaSWu Fengguang 	if (bdi_stat(bdi, BDI_RECLAIMABLE) >
771b00949aaSWu Fengguang 				bdi_dirty_limit(bdi, background_thresh))
772b00949aaSWu Fengguang 		return true;
773b00949aaSWu Fengguang 
774b00949aaSWu Fengguang 	return false;
77503ba3782SJens Axboe }
77603ba3782SJens Axboe 
77703ba3782SJens Axboe /*
778e98be2d5SWu Fengguang  * Called under wb->list_lock. If there are multiple wb per bdi,
779e98be2d5SWu Fengguang  * only the flusher working on the first wb should do it.
780e98be2d5SWu Fengguang  */
781e98be2d5SWu Fengguang static void wb_update_bandwidth(struct bdi_writeback *wb,
782e98be2d5SWu Fengguang 				unsigned long start_time)
783e98be2d5SWu Fengguang {
784af6a3113SWu Fengguang 	__bdi_update_bandwidth(wb->bdi, 0, 0, 0, 0, 0, start_time);
785e98be2d5SWu Fengguang }
786e98be2d5SWu Fengguang 
787e98be2d5SWu Fengguang /*
78803ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
78903ba3782SJens Axboe  *
79003ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
79103ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
79203ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
79303ba3782SJens Axboe  * older than a specific point in time.
79403ba3782SJens Axboe  *
79503ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
79603ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
79703ba3782SJens Axboe  * one-second gap.
79803ba3782SJens Axboe  *
79903ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
80003ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
80103ba3782SJens Axboe  */
802c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
80383ba7b07SChristoph Hellwig 			 struct wb_writeback_work *work)
80403ba3782SJens Axboe {
805e98be2d5SWu Fengguang 	unsigned long wb_start = jiffies;
806d46db3d5SWu Fengguang 	long nr_pages = work->nr_pages;
80703ba3782SJens Axboe 	unsigned long oldest_jif;
808a5989bdcSJan Kara 	struct inode *inode;
809d46db3d5SWu Fengguang 	long progress;
81003ba3782SJens Axboe 
811e185dda8SWu Fengguang 	oldest_jif = jiffies;
812d46db3d5SWu Fengguang 	work->older_than_this = &oldest_jif;
81303ba3782SJens Axboe 
814e8dfc305SWu Fengguang 	spin_lock(&wb->list_lock);
81503ba3782SJens Axboe 	for (;;) {
81603ba3782SJens Axboe 		/*
817d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
81803ba3782SJens Axboe 		 */
81983ba7b07SChristoph Hellwig 		if (work->nr_pages <= 0)
82003ba3782SJens Axboe 			break;
82103ba3782SJens Axboe 
82203ba3782SJens Axboe 		/*
823aa373cf5SJan Kara 		 * Background writeout and kupdate-style writeback may
824aa373cf5SJan Kara 		 * run forever. Stop them if there is other work to do
825aa373cf5SJan Kara 		 * so that e.g. sync can proceed. They'll be restarted
826aa373cf5SJan Kara 		 * after the other works are all done.
827aa373cf5SJan Kara 		 */
828aa373cf5SJan Kara 		if ((work->for_background || work->for_kupdate) &&
829aa373cf5SJan Kara 		    !list_empty(&wb->bdi->work_list))
830aa373cf5SJan Kara 			break;
831aa373cf5SJan Kara 
832aa373cf5SJan Kara 		/*
833d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
834d3ddec76SWu Fengguang 		 * background dirty threshold
83503ba3782SJens Axboe 		 */
836b00949aaSWu Fengguang 		if (work->for_background && !over_bground_thresh(wb->bdi))
83703ba3782SJens Axboe 			break;
83803ba3782SJens Axboe 
8391bc36b64SJan Kara 		/*
8401bc36b64SJan Kara 		 * Kupdate and background works are special and we want to
8411bc36b64SJan Kara 		 * include all inodes that need writing. Livelock avoidance is
8421bc36b64SJan Kara 		 * handled by these works yielding to any other work so we are
8431bc36b64SJan Kara 		 * safe.
8441bc36b64SJan Kara 		 */
845ba9aa839SWu Fengguang 		if (work->for_kupdate) {
846ba9aa839SWu Fengguang 			oldest_jif = jiffies -
847ba9aa839SWu Fengguang 				msecs_to_jiffies(dirty_expire_interval * 10);
8481bc36b64SJan Kara 		} else if (work->for_background)
8491bc36b64SJan Kara 			oldest_jif = jiffies;
850028c2dd1SDave Chinner 
851d46db3d5SWu Fengguang 		trace_writeback_start(wb->bdi, work);
852e8dfc305SWu Fengguang 		if (list_empty(&wb->b_io))
853ad4e38ddSCurt Wohlgemuth 			queue_io(wb, work);
85483ba7b07SChristoph Hellwig 		if (work->sb)
855d46db3d5SWu Fengguang 			progress = writeback_sb_inodes(work->sb, wb, work);
856edadfb10SChristoph Hellwig 		else
857d46db3d5SWu Fengguang 			progress = __writeback_inodes_wb(wb, work);
858d46db3d5SWu Fengguang 		trace_writeback_written(wb->bdi, work);
859028c2dd1SDave Chinner 
860e98be2d5SWu Fengguang 		wb_update_bandwidth(wb, wb_start);
86103ba3782SJens Axboe 
86203ba3782SJens Axboe 		/*
86371fd05a8SJens Axboe 		 * Did we write something? Try for more
864e6fb6da2SWu Fengguang 		 *
865e6fb6da2SWu Fengguang 		 * Dirty inodes are moved to b_io for writeback in batches.
866e6fb6da2SWu Fengguang 		 * The completion of the current batch does not necessarily
867e6fb6da2SWu Fengguang 		 * mean the overall work is done. So we keep looping as long
868e6fb6da2SWu Fengguang 		 * as made some progress on cleaning pages or inodes.
86971fd05a8SJens Axboe 		 */
870d46db3d5SWu Fengguang 		if (progress)
87103ba3782SJens Axboe 			continue;
872a5989bdcSJan Kara 		/*
873e6fb6da2SWu Fengguang 		 * No more inodes for IO, bail
874a5989bdcSJan Kara 		 */
875b7a2441fSWu Fengguang 		if (list_empty(&wb->b_more_io))
87603ba3782SJens Axboe 			break;
87703ba3782SJens Axboe 		/*
8788010c3b6SJens Axboe 		 * Nothing written. Wait for some inode to
8798010c3b6SJens Axboe 		 * become available for writeback. Otherwise
8808010c3b6SJens Axboe 		 * we'll just busyloop.
88103ba3782SJens Axboe 		 */
88203ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))  {
883d46db3d5SWu Fengguang 			trace_writeback_wait(wb->bdi, work);
88403ba3782SJens Axboe 			inode = wb_inode(wb->b_more_io.prev);
885250df6edSDave Chinner 			spin_lock(&inode->i_lock);
886f0d07b7fSJan Kara 			spin_unlock(&wb->list_lock);
887169ebd90SJan Kara 			/* This function drops i_lock... */
888169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
889f0d07b7fSJan Kara 			spin_lock(&wb->list_lock);
89003ba3782SJens Axboe 		}
89103ba3782SJens Axboe 	}
892e8dfc305SWu Fengguang 	spin_unlock(&wb->list_lock);
89303ba3782SJens Axboe 
894d46db3d5SWu Fengguang 	return nr_pages - work->nr_pages;
89503ba3782SJens Axboe }
89603ba3782SJens Axboe 
89703ba3782SJens Axboe /*
89883ba7b07SChristoph Hellwig  * Return the next wb_writeback_work struct that hasn't been processed yet.
89903ba3782SJens Axboe  */
90083ba7b07SChristoph Hellwig static struct wb_writeback_work *
90108852b6dSMinchan Kim get_next_work_item(struct backing_dev_info *bdi)
90203ba3782SJens Axboe {
90383ba7b07SChristoph Hellwig 	struct wb_writeback_work *work = NULL;
90403ba3782SJens Axboe 
9056467716aSArtem Bityutskiy 	spin_lock_bh(&bdi->wb_lock);
90683ba7b07SChristoph Hellwig 	if (!list_empty(&bdi->work_list)) {
90783ba7b07SChristoph Hellwig 		work = list_entry(bdi->work_list.next,
90883ba7b07SChristoph Hellwig 				  struct wb_writeback_work, list);
90983ba7b07SChristoph Hellwig 		list_del_init(&work->list);
91003ba3782SJens Axboe 	}
9116467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
91283ba7b07SChristoph Hellwig 	return work;
91303ba3782SJens Axboe }
91403ba3782SJens Axboe 
915cdf01dd5SLinus Torvalds /*
916cdf01dd5SLinus Torvalds  * Add in the number of potentially dirty inodes, because each inode
917cdf01dd5SLinus Torvalds  * write can dirty pagecache in the underlying blockdev.
918cdf01dd5SLinus Torvalds  */
919cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void)
920cdf01dd5SLinus Torvalds {
921cdf01dd5SLinus Torvalds 	return global_page_state(NR_FILE_DIRTY) +
922cdf01dd5SLinus Torvalds 		global_page_state(NR_UNSTABLE_NFS) +
923cdf01dd5SLinus Torvalds 		get_nr_dirty_inodes();
924cdf01dd5SLinus Torvalds }
925cdf01dd5SLinus Torvalds 
9266585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb)
9276585027aSJan Kara {
928b00949aaSWu Fengguang 	if (over_bground_thresh(wb->bdi)) {
9296585027aSJan Kara 
9306585027aSJan Kara 		struct wb_writeback_work work = {
9316585027aSJan Kara 			.nr_pages	= LONG_MAX,
9326585027aSJan Kara 			.sync_mode	= WB_SYNC_NONE,
9336585027aSJan Kara 			.for_background	= 1,
9346585027aSJan Kara 			.range_cyclic	= 1,
9350e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_BACKGROUND,
9366585027aSJan Kara 		};
9376585027aSJan Kara 
9386585027aSJan Kara 		return wb_writeback(wb, &work);
9396585027aSJan Kara 	}
9406585027aSJan Kara 
9416585027aSJan Kara 	return 0;
9426585027aSJan Kara }
9436585027aSJan Kara 
94403ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
94503ba3782SJens Axboe {
94603ba3782SJens Axboe 	unsigned long expired;
94703ba3782SJens Axboe 	long nr_pages;
94803ba3782SJens Axboe 
94969b62d01SJens Axboe 	/*
95069b62d01SJens Axboe 	 * When set to zero, disable periodic writeback
95169b62d01SJens Axboe 	 */
95269b62d01SJens Axboe 	if (!dirty_writeback_interval)
95369b62d01SJens Axboe 		return 0;
95469b62d01SJens Axboe 
95503ba3782SJens Axboe 	expired = wb->last_old_flush +
95603ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
95703ba3782SJens Axboe 	if (time_before(jiffies, expired))
95803ba3782SJens Axboe 		return 0;
95903ba3782SJens Axboe 
96003ba3782SJens Axboe 	wb->last_old_flush = jiffies;
961cdf01dd5SLinus Torvalds 	nr_pages = get_nr_dirty_pages();
96203ba3782SJens Axboe 
963c4a77a6cSJens Axboe 	if (nr_pages) {
96483ba7b07SChristoph Hellwig 		struct wb_writeback_work work = {
965c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
966c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
967c4a77a6cSJens Axboe 			.for_kupdate	= 1,
968c4a77a6cSJens Axboe 			.range_cyclic	= 1,
9690e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_PERIODIC,
970c4a77a6cSJens Axboe 		};
971c4a77a6cSJens Axboe 
97283ba7b07SChristoph Hellwig 		return wb_writeback(wb, &work);
973c4a77a6cSJens Axboe 	}
97403ba3782SJens Axboe 
97503ba3782SJens Axboe 	return 0;
97603ba3782SJens Axboe }
97703ba3782SJens Axboe 
97803ba3782SJens Axboe /*
97903ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
98003ba3782SJens Axboe  */
98103ba3782SJens Axboe long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
98203ba3782SJens Axboe {
98303ba3782SJens Axboe 	struct backing_dev_info *bdi = wb->bdi;
98483ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
985c4a77a6cSJens Axboe 	long wrote = 0;
98603ba3782SJens Axboe 
98781d73a32SJan Kara 	set_bit(BDI_writeback_running, &wb->bdi->state);
98808852b6dSMinchan Kim 	while ((work = get_next_work_item(bdi)) != NULL) {
98903ba3782SJens Axboe 		/*
99003ba3782SJens Axboe 		 * Override sync mode, in case we must wait for completion
99183ba7b07SChristoph Hellwig 		 * because this thread is exiting now.
99203ba3782SJens Axboe 		 */
99303ba3782SJens Axboe 		if (force_wait)
99483ba7b07SChristoph Hellwig 			work->sync_mode = WB_SYNC_ALL;
99583ba7b07SChristoph Hellwig 
996455b2864SDave Chinner 		trace_writeback_exec(bdi, work);
997455b2864SDave Chinner 
99883ba7b07SChristoph Hellwig 		wrote += wb_writeback(wb, work);
99903ba3782SJens Axboe 
100003ba3782SJens Axboe 		/*
100183ba7b07SChristoph Hellwig 		 * Notify the caller of completion if this is a synchronous
100283ba7b07SChristoph Hellwig 		 * work item, otherwise just free it.
100303ba3782SJens Axboe 		 */
100483ba7b07SChristoph Hellwig 		if (work->done)
100583ba7b07SChristoph Hellwig 			complete(work->done);
100683ba7b07SChristoph Hellwig 		else
100783ba7b07SChristoph Hellwig 			kfree(work);
100803ba3782SJens Axboe 	}
100903ba3782SJens Axboe 
101003ba3782SJens Axboe 	/*
101103ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
101203ba3782SJens Axboe 	 */
101303ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
10146585027aSJan Kara 	wrote += wb_check_background_flush(wb);
101581d73a32SJan Kara 	clear_bit(BDI_writeback_running, &wb->bdi->state);
101603ba3782SJens Axboe 
101703ba3782SJens Axboe 	return wrote;
101803ba3782SJens Axboe }
101903ba3782SJens Axboe 
102003ba3782SJens Axboe /*
102103ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
102203ba3782SJens Axboe  * wakes up periodically and does kupdated style flushing.
102303ba3782SJens Axboe  */
102408243900SChristoph Hellwig int bdi_writeback_thread(void *data)
102503ba3782SJens Axboe {
102608243900SChristoph Hellwig 	struct bdi_writeback *wb = data;
102708243900SChristoph Hellwig 	struct backing_dev_info *bdi = wb->bdi;
102803ba3782SJens Axboe 	long pages_written;
102903ba3782SJens Axboe 
1030766f9164SPeter Zijlstra 	current->flags |= PF_SWAPWRITE;
103108243900SChristoph Hellwig 	set_freezable();
1032ecd58403SArtem Bityutskiy 	wb->last_active = jiffies;
103303ba3782SJens Axboe 
103403ba3782SJens Axboe 	/*
103508243900SChristoph Hellwig 	 * Our parent may run at a different priority, just set us to normal
103603ba3782SJens Axboe 	 */
103708243900SChristoph Hellwig 	set_user_nice(current, 0);
103808243900SChristoph Hellwig 
1039455b2864SDave Chinner 	trace_writeback_thread_start(bdi);
1040455b2864SDave Chinner 
10418a32c441STejun Heo 	while (!kthread_freezable_should_stop(NULL)) {
10426467716aSArtem Bityutskiy 		/*
10436467716aSArtem Bityutskiy 		 * Remove own delayed wake-up timer, since we are already awake
10446467716aSArtem Bityutskiy 		 * and we'll take care of the preriodic write-back.
10456467716aSArtem Bityutskiy 		 */
10466467716aSArtem Bityutskiy 		del_timer(&wb->wakeup_timer);
10476467716aSArtem Bityutskiy 
104803ba3782SJens Axboe 		pages_written = wb_do_writeback(wb, 0);
104903ba3782SJens Axboe 
1050455b2864SDave Chinner 		trace_writeback_pages_written(pages_written);
1051455b2864SDave Chinner 
105203ba3782SJens Axboe 		if (pages_written)
1053ecd58403SArtem Bityutskiy 			wb->last_active = jiffies;
105403ba3782SJens Axboe 
1055297252c8SArtem Bityutskiy 		set_current_state(TASK_INTERRUPTIBLE);
1056b76b4014SJ. Bruce Fields 		if (!list_empty(&bdi->work_list) || kthread_should_stop()) {
1057297252c8SArtem Bityutskiy 			__set_current_state(TASK_RUNNING);
1058297252c8SArtem Bityutskiy 			continue;
105903ba3782SJens Axboe 		}
106003ba3782SJens Axboe 
1061253c34e9SArtem Bityutskiy 		if (wb_has_dirty_io(wb) && dirty_writeback_interval)
1062fff5b85aSArtem Bityutskiy 			schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10));
1063253c34e9SArtem Bityutskiy 		else {
1064253c34e9SArtem Bityutskiy 			/*
1065253c34e9SArtem Bityutskiy 			 * We have nothing to do, so can go sleep without any
1066253c34e9SArtem Bityutskiy 			 * timeout and save power. When a work is queued or
1067253c34e9SArtem Bityutskiy 			 * something is made dirty - we will be woken up.
1068253c34e9SArtem Bityutskiy 			 */
106969b62d01SJens Axboe 			schedule();
1070f9eadbbdSJens Axboe 		}
107103ba3782SJens Axboe 	}
107203ba3782SJens Axboe 
1073fff5b85aSArtem Bityutskiy 	/* Flush any work that raced with us exiting */
107408243900SChristoph Hellwig 	if (!list_empty(&bdi->work_list))
107508243900SChristoph Hellwig 		wb_do_writeback(wb, 1);
1076455b2864SDave Chinner 
1077455b2864SDave Chinner 	trace_writeback_thread_stop(bdi);
107803ba3782SJens Axboe 	return 0;
107903ba3782SJens Axboe }
108003ba3782SJens Axboe 
108108243900SChristoph Hellwig 
108203ba3782SJens Axboe /*
108303ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
108403ba3782SJens Axboe  * the whole world.
108503ba3782SJens Axboe  */
10860e175a18SCurt Wohlgemuth void wakeup_flusher_threads(long nr_pages, enum wb_reason reason)
108703ba3782SJens Axboe {
1088b8c2f347SChristoph Hellwig 	struct backing_dev_info *bdi;
1089b8c2f347SChristoph Hellwig 
109083ba7b07SChristoph Hellwig 	if (!nr_pages) {
109183ba7b07SChristoph Hellwig 		nr_pages = global_page_state(NR_FILE_DIRTY) +
109203ba3782SJens Axboe 				global_page_state(NR_UNSTABLE_NFS);
1093b8c2f347SChristoph Hellwig 	}
1094b8c2f347SChristoph Hellwig 
1095b8c2f347SChristoph Hellwig 	rcu_read_lock();
1096b8c2f347SChristoph Hellwig 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
1097b8c2f347SChristoph Hellwig 		if (!bdi_has_dirty_io(bdi))
1098b8c2f347SChristoph Hellwig 			continue;
10990e175a18SCurt Wohlgemuth 		__bdi_start_writeback(bdi, nr_pages, false, reason);
1100b8c2f347SChristoph Hellwig 	}
1101b8c2f347SChristoph Hellwig 	rcu_read_unlock();
110203ba3782SJens Axboe }
110303ba3782SJens Axboe 
110403ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
110503ba3782SJens Axboe {
110603ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
110703ba3782SJens Axboe 		struct dentry *dentry;
110803ba3782SJens Axboe 		const char *name = "?";
110903ba3782SJens Axboe 
111003ba3782SJens Axboe 		dentry = d_find_alias(inode);
111103ba3782SJens Axboe 		if (dentry) {
111203ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
111303ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
111403ba3782SJens Axboe 		}
111503ba3782SJens Axboe 		printk(KERN_DEBUG
111603ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
111703ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
111803ba3782SJens Axboe 		       name, inode->i_sb->s_id);
111903ba3782SJens Axboe 		if (dentry) {
112003ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
112103ba3782SJens Axboe 			dput(dentry);
112203ba3782SJens Axboe 		}
112303ba3782SJens Axboe 	}
112403ba3782SJens Axboe }
112503ba3782SJens Axboe 
112603ba3782SJens Axboe /**
112703ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
112803ba3782SJens Axboe  *	@inode: inode to mark
112903ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
113003ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
113103ba3782SJens Axboe  *  	mark_inode_dirty_sync.
113203ba3782SJens Axboe  *
113303ba3782SJens Axboe  * Put the inode on the super block's dirty list.
113403ba3782SJens Axboe  *
113503ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
113603ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
113703ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
113803ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
113903ba3782SJens Axboe  *
114003ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
114103ba3782SJens Axboe  * them dirty.
114203ba3782SJens Axboe  *
114303ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
114403ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
114503ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
114603ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
114703ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
114803ba3782SJens Axboe  * blockdev inode.
114903ba3782SJens Axboe  */
115003ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
115103ba3782SJens Axboe {
115203ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
1153253c34e9SArtem Bityutskiy 	struct backing_dev_info *bdi = NULL;
115403ba3782SJens Axboe 
115503ba3782SJens Axboe 	/*
115603ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
115703ba3782SJens Axboe 	 * dirty the inode itself
115803ba3782SJens Axboe 	 */
115903ba3782SJens Axboe 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
116003ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
1161aa385729SChristoph Hellwig 			sb->s_op->dirty_inode(inode, flags);
116203ba3782SJens Axboe 	}
116303ba3782SJens Axboe 
116403ba3782SJens Axboe 	/*
116503ba3782SJens Axboe 	 * make sure that changes are seen by all cpus before we test i_state
116603ba3782SJens Axboe 	 * -- mikulas
116703ba3782SJens Axboe 	 */
116803ba3782SJens Axboe 	smp_mb();
116903ba3782SJens Axboe 
117003ba3782SJens Axboe 	/* avoid the locking if we can */
117103ba3782SJens Axboe 	if ((inode->i_state & flags) == flags)
117203ba3782SJens Axboe 		return;
117303ba3782SJens Axboe 
117403ba3782SJens Axboe 	if (unlikely(block_dump))
117503ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
117603ba3782SJens Axboe 
1177250df6edSDave Chinner 	spin_lock(&inode->i_lock);
117803ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
117903ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
118003ba3782SJens Axboe 
118103ba3782SJens Axboe 		inode->i_state |= flags;
118203ba3782SJens Axboe 
118303ba3782SJens Axboe 		/*
118403ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
118503ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
118603ba3782SJens Axboe 		 * superblock list, based upon its state.
118703ba3782SJens Axboe 		 */
118803ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
1189250df6edSDave Chinner 			goto out_unlock_inode;
119003ba3782SJens Axboe 
119103ba3782SJens Axboe 		/*
119203ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
119303ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
119403ba3782SJens Axboe 		 */
119503ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
11961d3382cbSAl Viro 			if (inode_unhashed(inode))
1197250df6edSDave Chinner 				goto out_unlock_inode;
119803ba3782SJens Axboe 		}
1199a4ffdde6SAl Viro 		if (inode->i_state & I_FREEING)
1200250df6edSDave Chinner 			goto out_unlock_inode;
120103ba3782SJens Axboe 
120203ba3782SJens Axboe 		/*
120303ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
120403ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
120503ba3782SJens Axboe 		 */
120603ba3782SJens Axboe 		if (!was_dirty) {
1207a66979abSDave Chinner 			bool wakeup_bdi = false;
1208253c34e9SArtem Bityutskiy 			bdi = inode_to_bdi(inode);
1209500b067cSJens Axboe 
1210253c34e9SArtem Bityutskiy 			if (bdi_cap_writeback_dirty(bdi)) {
1211253c34e9SArtem Bityutskiy 				WARN(!test_bit(BDI_registered, &bdi->state),
1212253c34e9SArtem Bityutskiy 				     "bdi-%s not registered\n", bdi->name);
1213253c34e9SArtem Bityutskiy 
1214253c34e9SArtem Bityutskiy 				/*
1215253c34e9SArtem Bityutskiy 				 * If this is the first dirty inode for this
1216253c34e9SArtem Bityutskiy 				 * bdi, we have to wake-up the corresponding
1217253c34e9SArtem Bityutskiy 				 * bdi thread to make sure background
1218253c34e9SArtem Bityutskiy 				 * write-back happens later.
1219253c34e9SArtem Bityutskiy 				 */
1220253c34e9SArtem Bityutskiy 				if (!wb_has_dirty_io(&bdi->wb))
1221253c34e9SArtem Bityutskiy 					wakeup_bdi = true;
1222500b067cSJens Axboe 			}
122303ba3782SJens Axboe 
1224a66979abSDave Chinner 			spin_unlock(&inode->i_lock);
1225f758eeabSChristoph Hellwig 			spin_lock(&bdi->wb.list_lock);
122603ba3782SJens Axboe 			inode->dirtied_when = jiffies;
12277ccf19a8SNick Piggin 			list_move(&inode->i_wb_list, &bdi->wb.b_dirty);
1228f758eeabSChristoph Hellwig 			spin_unlock(&bdi->wb.list_lock);
1229253c34e9SArtem Bityutskiy 
1230253c34e9SArtem Bityutskiy 			if (wakeup_bdi)
12316467716aSArtem Bityutskiy 				bdi_wakeup_thread_delayed(bdi);
1232a66979abSDave Chinner 			return;
1233a66979abSDave Chinner 		}
1234a66979abSDave Chinner 	}
1235a66979abSDave Chinner out_unlock_inode:
1236a66979abSDave Chinner 	spin_unlock(&inode->i_lock);
1237a66979abSDave Chinner 
123803ba3782SJens Axboe }
123903ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
124003ba3782SJens Axboe 
1241b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
124266f3b8e2SJens Axboe {
124338f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
124438f21977SNick Piggin 
124503ba3782SJens Axboe 	/*
124603ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
124703ba3782SJens Axboe 	 * r/o to r/w or vice versa.
124803ba3782SJens Axboe 	 */
1249b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
125003ba3782SJens Axboe 
125155fa6091SDave Chinner 	spin_lock(&inode_sb_list_lock);
125266f3b8e2SJens Axboe 
125338f21977SNick Piggin 	/*
125438f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
125538f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
125638f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
125738f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
125838f21977SNick Piggin 	 * we still have to wait for that writeout.
125938f21977SNick Piggin 	 */
1260b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1261250df6edSDave Chinner 		struct address_space *mapping = inode->i_mapping;
126238f21977SNick Piggin 
1263250df6edSDave Chinner 		spin_lock(&inode->i_lock);
1264250df6edSDave Chinner 		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
1265250df6edSDave Chinner 		    (mapping->nrpages == 0)) {
1266250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
126738f21977SNick Piggin 			continue;
1268250df6edSDave Chinner 		}
126938f21977SNick Piggin 		__iget(inode);
1270250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
127155fa6091SDave Chinner 		spin_unlock(&inode_sb_list_lock);
127255fa6091SDave Chinner 
127338f21977SNick Piggin 		/*
127455fa6091SDave Chinner 		 * We hold a reference to 'inode' so it couldn't have been
127555fa6091SDave Chinner 		 * removed from s_inodes list while we dropped the
127655fa6091SDave Chinner 		 * inode_sb_list_lock.  We cannot iput the inode now as we can
127755fa6091SDave Chinner 		 * be holding the last reference and we cannot iput it under
127855fa6091SDave Chinner 		 * inode_sb_list_lock. So we keep the reference and iput it
127955fa6091SDave Chinner 		 * later.
128038f21977SNick Piggin 		 */
128138f21977SNick Piggin 		iput(old_inode);
128238f21977SNick Piggin 		old_inode = inode;
128338f21977SNick Piggin 
128438f21977SNick Piggin 		filemap_fdatawait(mapping);
128538f21977SNick Piggin 
128638f21977SNick Piggin 		cond_resched();
128738f21977SNick Piggin 
128855fa6091SDave Chinner 		spin_lock(&inode_sb_list_lock);
128938f21977SNick Piggin 	}
129055fa6091SDave Chinner 	spin_unlock(&inode_sb_list_lock);
129138f21977SNick Piggin 	iput(old_inode);
129266f3b8e2SJens Axboe }
12931da177e4SLinus Torvalds 
1294d8a8559cSJens Axboe /**
12953259f8beSChris Mason  * writeback_inodes_sb_nr -	writeback dirty inodes from given super_block
1296d8a8559cSJens Axboe  * @sb: the superblock
12973259f8beSChris Mason  * @nr: the number of pages to write
1298786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work initiated
12991da177e4SLinus Torvalds  *
1300d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
1301d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
13023259f8beSChris Mason  * for IO completion of submitted IO.
13031da177e4SLinus Torvalds  */
13040e175a18SCurt Wohlgemuth void writeback_inodes_sb_nr(struct super_block *sb,
13050e175a18SCurt Wohlgemuth 			    unsigned long nr,
13060e175a18SCurt Wohlgemuth 			    enum wb_reason reason)
13071da177e4SLinus Torvalds {
130883ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
130983ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
13103c4d7165SChristoph Hellwig 		.sb			= sb,
13113c4d7165SChristoph Hellwig 		.sync_mode		= WB_SYNC_NONE,
13126e6938b6SWu Fengguang 		.tagged_writepages	= 1,
131383ba7b07SChristoph Hellwig 		.done			= &done,
13143259f8beSChris Mason 		.nr_pages		= nr,
13150e175a18SCurt Wohlgemuth 		.reason			= reason,
13163c4d7165SChristoph Hellwig 	};
13170e3c9a22SJens Axboe 
1318cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
131983ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
132083ba7b07SChristoph Hellwig 	wait_for_completion(&done);
13211da177e4SLinus Torvalds }
13223259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr);
13233259f8beSChris Mason 
13243259f8beSChris Mason /**
13253259f8beSChris Mason  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
13263259f8beSChris Mason  * @sb: the superblock
1327786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
13283259f8beSChris Mason  *
13293259f8beSChris Mason  * Start writeback on some inodes on this super_block. No guarantees are made
13303259f8beSChris Mason  * on how many (if any) will be written, and this function does not wait
13313259f8beSChris Mason  * for IO completion of submitted IO.
13323259f8beSChris Mason  */
13330e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
13343259f8beSChris Mason {
13350e175a18SCurt Wohlgemuth 	return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
13363259f8beSChris Mason }
1337d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
1338d8a8559cSJens Axboe 
1339d8a8559cSJens Axboe /**
134017bd55d0SEric Sandeen  * writeback_inodes_sb_if_idle	-	start writeback if none underway
134117bd55d0SEric Sandeen  * @sb: the superblock
1342786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
134317bd55d0SEric Sandeen  *
134417bd55d0SEric Sandeen  * Invoke writeback_inodes_sb if no writeback is currently underway.
134517bd55d0SEric Sandeen  * Returns 1 if writeback was started, 0 if not.
134617bd55d0SEric Sandeen  */
13470e175a18SCurt Wohlgemuth int writeback_inodes_sb_if_idle(struct super_block *sb, enum wb_reason reason)
134817bd55d0SEric Sandeen {
134917bd55d0SEric Sandeen 	if (!writeback_in_progress(sb->s_bdi)) {
1350cf37e972SChristoph Hellwig 		down_read(&sb->s_umount);
13510e175a18SCurt Wohlgemuth 		writeback_inodes_sb(sb, reason);
1352cf37e972SChristoph Hellwig 		up_read(&sb->s_umount);
135317bd55d0SEric Sandeen 		return 1;
135417bd55d0SEric Sandeen 	} else
135517bd55d0SEric Sandeen 		return 0;
135617bd55d0SEric Sandeen }
135717bd55d0SEric Sandeen EXPORT_SYMBOL(writeback_inodes_sb_if_idle);
135817bd55d0SEric Sandeen 
135917bd55d0SEric Sandeen /**
1360c097b2caSFengguang Wu  * writeback_inodes_sb_nr_if_idle	-	start writeback if none underway
13613259f8beSChris Mason  * @sb: the superblock
13623259f8beSChris Mason  * @nr: the number of pages to write
1363786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
13643259f8beSChris Mason  *
13653259f8beSChris Mason  * Invoke writeback_inodes_sb if no writeback is currently underway.
13663259f8beSChris Mason  * Returns 1 if writeback was started, 0 if not.
13673259f8beSChris Mason  */
13683259f8beSChris Mason int writeback_inodes_sb_nr_if_idle(struct super_block *sb,
13690e175a18SCurt Wohlgemuth 				   unsigned long nr,
13700e175a18SCurt Wohlgemuth 				   enum wb_reason reason)
13713259f8beSChris Mason {
13723259f8beSChris Mason 	if (!writeback_in_progress(sb->s_bdi)) {
13733259f8beSChris Mason 		down_read(&sb->s_umount);
13740e175a18SCurt Wohlgemuth 		writeback_inodes_sb_nr(sb, nr, reason);
13753259f8beSChris Mason 		up_read(&sb->s_umount);
13763259f8beSChris Mason 		return 1;
13773259f8beSChris Mason 	} else
13783259f8beSChris Mason 		return 0;
13793259f8beSChris Mason }
13803259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr_if_idle);
13813259f8beSChris Mason 
13823259f8beSChris Mason /**
1383d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
1384d8a8559cSJens Axboe  * @sb: the superblock
1385d8a8559cSJens Axboe  *
1386d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
1387cb9ef8d5SStefan Hajnoczi  * super_block.
1388d8a8559cSJens Axboe  */
1389b6e51316SJens Axboe void sync_inodes_sb(struct super_block *sb)
1390d8a8559cSJens Axboe {
139183ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
139283ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
13933c4d7165SChristoph Hellwig 		.sb		= sb,
13943c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_ALL,
13953c4d7165SChristoph Hellwig 		.nr_pages	= LONG_MAX,
13963c4d7165SChristoph Hellwig 		.range_cyclic	= 0,
139783ba7b07SChristoph Hellwig 		.done		= &done,
13980e175a18SCurt Wohlgemuth 		.reason		= WB_REASON_SYNC,
13993c4d7165SChristoph Hellwig 	};
14003c4d7165SChristoph Hellwig 
1401cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1402cf37e972SChristoph Hellwig 
140383ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
140483ba7b07SChristoph Hellwig 	wait_for_completion(&done);
140583ba7b07SChristoph Hellwig 
1406b6e51316SJens Axboe 	wait_sb_inodes(sb);
1407d8a8559cSJens Axboe }
1408d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds /**
14111da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
14121da177e4SLinus Torvalds  * @inode: inode to write to disk
14131da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
14141da177e4SLinus Torvalds  *
14157f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
14167f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
14177f04c26dSAndrea Arcangeli  *
14187f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
14191da177e4SLinus Torvalds  */
14201da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
14211da177e4SLinus Torvalds {
1422f758eeabSChristoph Hellwig 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
14231da177e4SLinus Torvalds 	struct writeback_control wbc = {
14241da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
142518914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1426111ebb6eSOGAWA Hirofumi 		.range_start = 0,
1427111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
14281da177e4SLinus Torvalds 	};
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
143149364ce2SAndrew Morton 		wbc.nr_to_write = 0;
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	might_sleep();
14344f8ad655SJan Kara 	return writeback_single_inode(inode, wb, &wbc);
14351da177e4SLinus Torvalds }
14361da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds /**
14391da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
14401da177e4SLinus Torvalds  * @inode: the inode to sync
14411da177e4SLinus Torvalds  * @wbc: controls the writeback mode
14421da177e4SLinus Torvalds  *
14431da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
14441da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
14451da177e4SLinus Torvalds  * update inode->i_state.
14461da177e4SLinus Torvalds  *
14471da177e4SLinus Torvalds  * The caller must have a ref on the inode.
14481da177e4SLinus Torvalds  */
14491da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
14501da177e4SLinus Torvalds {
14514f8ad655SJan Kara 	return writeback_single_inode(inode, &inode_to_bdi(inode)->wb, wbc);
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
1454c3765016SChristoph Hellwig 
1455c3765016SChristoph Hellwig /**
1456c691b9d9SAndrew Morton  * sync_inode_metadata - write an inode to disk
1457c3765016SChristoph Hellwig  * @inode: the inode to sync
1458c3765016SChristoph Hellwig  * @wait: wait for I/O to complete.
1459c3765016SChristoph Hellwig  *
1460c691b9d9SAndrew Morton  * Write an inode to disk and adjust its dirty state after completion.
1461c3765016SChristoph Hellwig  *
1462c3765016SChristoph Hellwig  * Note: only writes the actual inode, no associated data or other metadata.
1463c3765016SChristoph Hellwig  */
1464c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait)
1465c3765016SChristoph Hellwig {
1466c3765016SChristoph Hellwig 	struct writeback_control wbc = {
1467c3765016SChristoph Hellwig 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
1468c3765016SChristoph Hellwig 		.nr_to_write = 0, /* metadata-only */
1469c3765016SChristoph Hellwig 	};
1470c3765016SChristoph Hellwig 
1471c3765016SChristoph Hellwig 	return sync_inode(inode, &wbc);
1472c3765016SChristoph Hellwig }
1473c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata);
1474