xref: /openbmc/linux/fs/fs-writeback.c (revision f9b0e058)
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;
43c4a391b5SJan Kara 	/*
44c4a391b5SJan Kara 	 * Write only inodes dirtied before this time. Don't forget to set
45c4a391b5SJan Kara 	 * older_than_this_is_set when you set this.
46c4a391b5SJan Kara 	 */
47c4a391b5SJan Kara 	unsigned long older_than_this;
48c4a77a6cSJens Axboe 	enum writeback_sync_modes sync_mode;
496e6938b6SWu Fengguang 	unsigned int tagged_writepages:1;
5052957fe1SH Hartley Sweeten 	unsigned int for_kupdate:1;
5152957fe1SH Hartley Sweeten 	unsigned int range_cyclic:1;
5252957fe1SH Hartley Sweeten 	unsigned int for_background:1;
537747bd4bSDave Chinner 	unsigned int for_sync:1;	/* sync(2) WB_SYNC_ALL writeback */
54c4a391b5SJan Kara 	unsigned int older_than_this_is_set:1;
550e175a18SCurt Wohlgemuth 	enum wb_reason reason;		/* why was writeback initiated? */
56c4a77a6cSJens Axboe 
578010c3b6SJens Axboe 	struct list_head list;		/* pending work list */
5883ba7b07SChristoph Hellwig 	struct completion *done;	/* set if the caller waits */
5903ba3782SJens Axboe };
6003ba3782SJens Axboe 
61f11b00f3SAdrian Bunk /**
62f11b00f3SAdrian Bunk  * writeback_in_progress - determine whether there is writeback in progress
63f11b00f3SAdrian Bunk  * @bdi: the device's backing_dev_info structure.
64f11b00f3SAdrian Bunk  *
6503ba3782SJens Axboe  * Determine whether there is writeback waiting to be handled against a
6603ba3782SJens Axboe  * backing device.
67f11b00f3SAdrian Bunk  */
68f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi)
69f11b00f3SAdrian Bunk {
7081d73a32SJan Kara 	return test_bit(BDI_writeback_running, &bdi->state);
71f11b00f3SAdrian Bunk }
7200d4e736STheodore Ts'o EXPORT_SYMBOL(writeback_in_progress);
73f11b00f3SAdrian Bunk 
74692ebd17SJan Kara static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
75692ebd17SJan Kara {
76692ebd17SJan Kara 	struct super_block *sb = inode->i_sb;
77692ebd17SJan Kara 
78a8855990SJan Kara 	if (sb_is_blkdev_sb(sb))
79aaead25bSChristoph Hellwig 		return inode->i_mapping->backing_dev_info;
80aaead25bSChristoph Hellwig 
81692ebd17SJan Kara 	return sb->s_bdi;
82692ebd17SJan Kara }
83692ebd17SJan Kara 
847ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head)
857ccf19a8SNick Piggin {
867ccf19a8SNick Piggin 	return list_entry(head, struct inode, i_wb_list);
877ccf19a8SNick Piggin }
887ccf19a8SNick Piggin 
8915eb77a0SWu Fengguang /*
9015eb77a0SWu Fengguang  * Include the creation of the trace points after defining the
9115eb77a0SWu Fengguang  * wb_writeback_work structure and inline functions so that the definition
9215eb77a0SWu Fengguang  * remains local to this file.
9315eb77a0SWu Fengguang  */
9415eb77a0SWu Fengguang #define CREATE_TRACE_POINTS
9515eb77a0SWu Fengguang #include <trace/events/writeback.h>
9615eb77a0SWu Fengguang 
976585027aSJan Kara static void bdi_queue_work(struct backing_dev_info *bdi,
986585027aSJan Kara 			   struct wb_writeback_work *work)
996585027aSJan Kara {
1006585027aSJan Kara 	trace_writeback_queue(bdi, work);
1016585027aSJan Kara 
1026585027aSJan Kara 	spin_lock_bh(&bdi->wb_lock);
1036585027aSJan Kara 	list_add_tail(&work->list, &bdi->work_list);
1046467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
105839a8e86STejun Heo 
106839a8e86STejun Heo 	mod_delayed_work(bdi_wq, &bdi->wb.dwork, 0);
10703ba3782SJens Axboe }
1081da177e4SLinus Torvalds 
10983ba7b07SChristoph Hellwig static void
11083ba7b07SChristoph Hellwig __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
1110e175a18SCurt Wohlgemuth 		      bool range_cyclic, enum wb_reason reason)
1121da177e4SLinus Torvalds {
11383ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
11403ba3782SJens Axboe 
115bcddc3f0SJens Axboe 	/*
116bcddc3f0SJens Axboe 	 * This is WB_SYNC_NONE writeback, so if allocation fails just
117bcddc3f0SJens Axboe 	 * wakeup the thread for old dirty data writeback
118bcddc3f0SJens Axboe 	 */
11983ba7b07SChristoph Hellwig 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
12083ba7b07SChristoph Hellwig 	if (!work) {
121455b2864SDave Chinner 		trace_writeback_nowork(bdi);
122839a8e86STejun Heo 		mod_delayed_work(bdi_wq, &bdi->wb.dwork, 0);
12383ba7b07SChristoph Hellwig 		return;
12483ba7b07SChristoph Hellwig 	}
12583ba7b07SChristoph Hellwig 
12683ba7b07SChristoph Hellwig 	work->sync_mode	= WB_SYNC_NONE;
12783ba7b07SChristoph Hellwig 	work->nr_pages	= nr_pages;
12883ba7b07SChristoph Hellwig 	work->range_cyclic = range_cyclic;
1290e175a18SCurt Wohlgemuth 	work->reason	= reason;
13083ba7b07SChristoph Hellwig 
131f11fcae8SJens Axboe 	bdi_queue_work(bdi, work);
13203ba3782SJens Axboe }
133b6e51316SJens Axboe 
134b6e51316SJens Axboe /**
135b6e51316SJens Axboe  * bdi_start_writeback - start writeback
136b6e51316SJens Axboe  * @bdi: the backing device to write from
137b6e51316SJens Axboe  * @nr_pages: the number of pages to write
138786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
139b6e51316SJens Axboe  *
140b6e51316SJens Axboe  * Description:
141b6e51316SJens Axboe  *   This does WB_SYNC_NONE opportunistic writeback. The IO is only
14225985edcSLucas De Marchi  *   started when this function returns, we make no guarantees on
1430e3c9a22SJens Axboe  *   completion. Caller need not hold sb s_umount semaphore.
144b6e51316SJens Axboe  *
145b6e51316SJens Axboe  */
1460e175a18SCurt Wohlgemuth void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages,
1470e175a18SCurt Wohlgemuth 			enum wb_reason reason)
148b6e51316SJens Axboe {
1490e175a18SCurt Wohlgemuth 	__bdi_start_writeback(bdi, nr_pages, true, reason);
150d3ddec76SWu Fengguang }
151d3ddec76SWu Fengguang 
152c5444198SChristoph Hellwig /**
153c5444198SChristoph Hellwig  * bdi_start_background_writeback - start background writeback
154c5444198SChristoph Hellwig  * @bdi: the backing device to write from
155c5444198SChristoph Hellwig  *
156c5444198SChristoph Hellwig  * Description:
1576585027aSJan Kara  *   This makes sure WB_SYNC_NONE background writeback happens. When
1586585027aSJan Kara  *   this function returns, it is only guaranteed that for given BDI
1596585027aSJan Kara  *   some IO is happening if we are over background dirty threshold.
1606585027aSJan Kara  *   Caller need not hold sb s_umount semaphore.
161c5444198SChristoph Hellwig  */
162c5444198SChristoph Hellwig void bdi_start_background_writeback(struct backing_dev_info *bdi)
163c5444198SChristoph Hellwig {
1646585027aSJan Kara 	/*
1656585027aSJan Kara 	 * We just wake up the flusher thread. It will perform background
1666585027aSJan Kara 	 * writeback as soon as there is no other work to do.
1676585027aSJan Kara 	 */
16871927e84SWu Fengguang 	trace_writeback_wake_background(bdi);
169839a8e86STejun Heo 	mod_delayed_work(bdi_wq, &bdi->wb.dwork, 0);
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds /*
173a66979abSDave Chinner  * Remove the inode from the writeback list it is on.
174a66979abSDave Chinner  */
175a66979abSDave Chinner void inode_wb_list_del(struct inode *inode)
176a66979abSDave Chinner {
177f758eeabSChristoph Hellwig 	struct backing_dev_info *bdi = inode_to_bdi(inode);
178a66979abSDave Chinner 
179f758eeabSChristoph Hellwig 	spin_lock(&bdi->wb.list_lock);
180f758eeabSChristoph Hellwig 	list_del_init(&inode->i_wb_list);
181f758eeabSChristoph Hellwig 	spin_unlock(&bdi->wb.list_lock);
182f758eeabSChristoph Hellwig }
183a66979abSDave Chinner 
184a66979abSDave Chinner /*
1856610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
1866610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
1876610a0bcSAndrew Morton  *
1886610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
18966f3b8e2SJens Axboe  * already the most-recently-dirtied inode on the b_dirty list.  If that is
1906610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
1916610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
1926610a0bcSAndrew Morton  */
193f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
1946610a0bcSAndrew Morton {
195f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
19603ba3782SJens Axboe 	if (!list_empty(&wb->b_dirty)) {
19766f3b8e2SJens Axboe 		struct inode *tail;
1986610a0bcSAndrew Morton 
1997ccf19a8SNick Piggin 		tail = wb_inode(wb->b_dirty.next);
20066f3b8e2SJens Axboe 		if (time_before(inode->dirtied_when, tail->dirtied_when))
2016610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
2026610a0bcSAndrew Morton 	}
2037ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_dirty);
2046610a0bcSAndrew Morton }
2056610a0bcSAndrew Morton 
2066610a0bcSAndrew Morton /*
20766f3b8e2SJens Axboe  * requeue inode for re-scanning after bdi->b_io list is exhausted.
208c986d1e2SAndrew Morton  */
209f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
210c986d1e2SAndrew Morton {
211f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
2127ccf19a8SNick Piggin 	list_move(&inode->i_wb_list, &wb->b_more_io);
213c986d1e2SAndrew Morton }
214c986d1e2SAndrew Morton 
2151c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
2161c0eeaf5SJoern Engel {
217365b94aeSJan Kara 	inode->i_state &= ~I_SYNC;
2184eff96ddSJan Kara 	/* If inode is clean an unused, put it into LRU now... */
2194eff96ddSJan Kara 	inode_add_lru(inode);
220365b94aeSJan Kara 	/* Waiters must see I_SYNC cleared before being woken up */
2211c0eeaf5SJoern Engel 	smp_mb();
2221c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
2231c0eeaf5SJoern Engel }
2241c0eeaf5SJoern Engel 
225d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
226d2caa3c5SJeff Layton {
227d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
228d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
229d2caa3c5SJeff Layton 	/*
230d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
231d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
232d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
2335b0830cbSJens Axboe 	 * from permanently stopping the whole bdi writeback.
234d2caa3c5SJeff Layton 	 */
235d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
236d2caa3c5SJeff Layton #endif
237d2caa3c5SJeff Layton 	return ret;
238d2caa3c5SJeff Layton }
239d2caa3c5SJeff Layton 
240c986d1e2SAndrew Morton /*
2410e2f2b23SWang Sheng-Hui  * Move expired (dirtied before work->older_than_this) dirty inodes from
242697e6fedSJan Kara  * @delaying_queue to @dispatch_queue.
2432c136579SFengguang Wu  */
244e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue,
2452c136579SFengguang Wu 			       struct list_head *dispatch_queue,
246ad4e38ddSCurt Wohlgemuth 			       struct wb_writeback_work *work)
2472c136579SFengguang Wu {
2485c03449dSShaohua Li 	LIST_HEAD(tmp);
2495c03449dSShaohua Li 	struct list_head *pos, *node;
250cf137307SJens Axboe 	struct super_block *sb = NULL;
2515c03449dSShaohua Li 	struct inode *inode;
252cf137307SJens Axboe 	int do_sb_sort = 0;
253e84d0a4fSWu Fengguang 	int moved = 0;
2545c03449dSShaohua Li 
255c4a391b5SJan Kara 	WARN_ON_ONCE(!work->older_than_this_is_set);
2562c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
2577ccf19a8SNick Piggin 		inode = wb_inode(delaying_queue->prev);
258c4a391b5SJan Kara 		if (inode_dirtied_after(inode, work->older_than_this))
2592c136579SFengguang Wu 			break;
260a8855990SJan Kara 		list_move(&inode->i_wb_list, &tmp);
261a8855990SJan Kara 		moved++;
262a8855990SJan Kara 		if (sb_is_blkdev_sb(inode->i_sb))
263a8855990SJan Kara 			continue;
264cf137307SJens Axboe 		if (sb && sb != inode->i_sb)
265cf137307SJens Axboe 			do_sb_sort = 1;
266cf137307SJens Axboe 		sb = inode->i_sb;
2675c03449dSShaohua Li 	}
2685c03449dSShaohua Li 
269cf137307SJens Axboe 	/* just one sb in list, splice to dispatch_queue and we're done */
270cf137307SJens Axboe 	if (!do_sb_sort) {
271cf137307SJens Axboe 		list_splice(&tmp, dispatch_queue);
272e84d0a4fSWu Fengguang 		goto out;
273cf137307SJens Axboe 	}
274cf137307SJens Axboe 
2755c03449dSShaohua Li 	/* Move inodes from one superblock together */
2765c03449dSShaohua Li 	while (!list_empty(&tmp)) {
2777ccf19a8SNick Piggin 		sb = wb_inode(tmp.prev)->i_sb;
2785c03449dSShaohua Li 		list_for_each_prev_safe(pos, node, &tmp) {
2797ccf19a8SNick Piggin 			inode = wb_inode(pos);
2805c03449dSShaohua Li 			if (inode->i_sb == sb)
2817ccf19a8SNick Piggin 				list_move(&inode->i_wb_list, dispatch_queue);
2822c136579SFengguang Wu 		}
2832c136579SFengguang Wu 	}
284e84d0a4fSWu Fengguang out:
285e84d0a4fSWu Fengguang 	return moved;
2865c03449dSShaohua Li }
2872c136579SFengguang Wu 
2882c136579SFengguang Wu /*
2892c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
2904ea879b9SWu Fengguang  * Before
2914ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
2924ea879b9SWu Fengguang  *         =============>    gf         edc     BA
2934ea879b9SWu Fengguang  * After
2944ea879b9SWu Fengguang  *         newly dirtied     b_dirty    b_io    b_more_io
2954ea879b9SWu Fengguang  *         =============>    g          fBAedc
2964ea879b9SWu Fengguang  *                                           |
2974ea879b9SWu Fengguang  *                                           +--> dequeue for IO
2982c136579SFengguang Wu  */
299ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
3002c136579SFengguang Wu {
301e84d0a4fSWu Fengguang 	int moved;
302f758eeabSChristoph Hellwig 	assert_spin_locked(&wb->list_lock);
3034ea879b9SWu Fengguang 	list_splice_init(&wb->b_more_io, &wb->b_io);
304ad4e38ddSCurt Wohlgemuth 	moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, work);
305ad4e38ddSCurt Wohlgemuth 	trace_writeback_queue_io(wb, work, moved);
30666f3b8e2SJens Axboe }
30766f3b8e2SJens Axboe 
308a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc)
30966f3b8e2SJens Axboe {
3109fb0a7daSTejun Heo 	int ret;
3119fb0a7daSTejun Heo 
3129fb0a7daSTejun Heo 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) {
3139fb0a7daSTejun Heo 		trace_writeback_write_inode_start(inode, wbc);
3149fb0a7daSTejun Heo 		ret = inode->i_sb->s_op->write_inode(inode, wbc);
3159fb0a7daSTejun Heo 		trace_writeback_write_inode(inode, wbc);
3169fb0a7daSTejun Heo 		return ret;
3179fb0a7daSTejun Heo 	}
31803ba3782SJens Axboe 	return 0;
31966f3b8e2SJens Axboe }
32008d8e974SFengguang Wu 
3212c136579SFengguang Wu /*
322169ebd90SJan Kara  * Wait for writeback on an inode to complete. Called with i_lock held.
323169ebd90SJan Kara  * Caller must make sure inode cannot go away when we drop i_lock.
32401c03194SChristoph Hellwig  */
325169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode)
326169ebd90SJan Kara 	__releases(inode->i_lock)
327169ebd90SJan Kara 	__acquires(inode->i_lock)
32801c03194SChristoph Hellwig {
32901c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
33001c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
33101c03194SChristoph Hellwig 
33201c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
33358a9d3d8SRichard Kennedy 	while (inode->i_state & I_SYNC) {
334250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
33501c03194SChristoph Hellwig 		__wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
336250df6edSDave Chinner 		spin_lock(&inode->i_lock);
33758a9d3d8SRichard Kennedy 	}
33801c03194SChristoph Hellwig }
33901c03194SChristoph Hellwig 
34001c03194SChristoph Hellwig /*
341169ebd90SJan Kara  * Wait for writeback on an inode to complete. Caller must have inode pinned.
342169ebd90SJan Kara  */
343169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode)
344169ebd90SJan Kara {
345169ebd90SJan Kara 	spin_lock(&inode->i_lock);
346169ebd90SJan Kara 	__inode_wait_for_writeback(inode);
347169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
348169ebd90SJan Kara }
349169ebd90SJan Kara 
350169ebd90SJan Kara /*
351169ebd90SJan Kara  * Sleep until I_SYNC is cleared. This function must be called with i_lock
352169ebd90SJan Kara  * held and drops it. It is aimed for callers not holding any inode reference
353169ebd90SJan Kara  * so once i_lock is dropped, inode can go away.
354169ebd90SJan Kara  */
355169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode)
356169ebd90SJan Kara 	__releases(inode->i_lock)
357169ebd90SJan Kara {
358169ebd90SJan Kara 	DEFINE_WAIT(wait);
359169ebd90SJan Kara 	wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
360169ebd90SJan Kara 	int sleep;
361169ebd90SJan Kara 
362169ebd90SJan Kara 	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
363169ebd90SJan Kara 	sleep = inode->i_state & I_SYNC;
364169ebd90SJan Kara 	spin_unlock(&inode->i_lock);
365169ebd90SJan Kara 	if (sleep)
366169ebd90SJan Kara 		schedule();
367169ebd90SJan Kara 	finish_wait(wqh, &wait);
368169ebd90SJan Kara }
369169ebd90SJan Kara 
370169ebd90SJan Kara /*
371ccb26b5aSJan Kara  * Find proper writeback list for the inode depending on its current state and
372ccb26b5aSJan Kara  * possibly also change of its state while we were doing writeback.  Here we
373ccb26b5aSJan Kara  * handle things such as livelock prevention or fairness of writeback among
374ccb26b5aSJan Kara  * inodes. This function can be called only by flusher thread - noone else
375ccb26b5aSJan Kara  * processes all inodes in writeback lists and requeueing inodes behind flusher
376ccb26b5aSJan Kara  * thread's back can have unexpected consequences.
377ccb26b5aSJan Kara  */
378ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
379ccb26b5aSJan Kara 			  struct writeback_control *wbc)
380ccb26b5aSJan Kara {
381ccb26b5aSJan Kara 	if (inode->i_state & I_FREEING)
382ccb26b5aSJan Kara 		return;
383ccb26b5aSJan Kara 
384ccb26b5aSJan Kara 	/*
385ccb26b5aSJan Kara 	 * Sync livelock prevention. Each inode is tagged and synced in one
386ccb26b5aSJan Kara 	 * shot. If still dirty, it will be redirty_tail()'ed below.  Update
387ccb26b5aSJan Kara 	 * the dirty time to prevent enqueue and sync it again.
388ccb26b5aSJan Kara 	 */
389ccb26b5aSJan Kara 	if ((inode->i_state & I_DIRTY) &&
390ccb26b5aSJan Kara 	    (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
391ccb26b5aSJan Kara 		inode->dirtied_when = jiffies;
392ccb26b5aSJan Kara 
3934f8ad655SJan Kara 	if (wbc->pages_skipped) {
3944f8ad655SJan Kara 		/*
3954f8ad655SJan Kara 		 * writeback is not making progress due to locked
3964f8ad655SJan Kara 		 * buffers. Skip this inode for now.
3974f8ad655SJan Kara 		 */
3984f8ad655SJan Kara 		redirty_tail(inode, wb);
3994f8ad655SJan Kara 		return;
4004f8ad655SJan Kara 	}
4014f8ad655SJan Kara 
402ccb26b5aSJan Kara 	if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
403ccb26b5aSJan Kara 		/*
404ccb26b5aSJan Kara 		 * We didn't write back all the pages.  nfs_writepages()
405ccb26b5aSJan Kara 		 * sometimes bales out without doing anything.
406ccb26b5aSJan Kara 		 */
407ccb26b5aSJan Kara 		if (wbc->nr_to_write <= 0) {
408ccb26b5aSJan Kara 			/* Slice used up. Queue for next turn. */
409ccb26b5aSJan Kara 			requeue_io(inode, wb);
410ccb26b5aSJan Kara 		} else {
411ccb26b5aSJan Kara 			/*
412ccb26b5aSJan Kara 			 * Writeback blocked by something other than
413ccb26b5aSJan Kara 			 * congestion. Delay the inode for some time to
414ccb26b5aSJan Kara 			 * avoid spinning on the CPU (100% iowait)
415ccb26b5aSJan Kara 			 * retrying writeback of the dirty page/inode
416ccb26b5aSJan Kara 			 * that cannot be performed immediately.
417ccb26b5aSJan Kara 			 */
418ccb26b5aSJan Kara 			redirty_tail(inode, wb);
419ccb26b5aSJan Kara 		}
420ccb26b5aSJan Kara 	} else if (inode->i_state & I_DIRTY) {
421ccb26b5aSJan Kara 		/*
422ccb26b5aSJan Kara 		 * Filesystems can dirty the inode during writeback operations,
423ccb26b5aSJan Kara 		 * such as delayed allocation during submission or metadata
424ccb26b5aSJan Kara 		 * updates after data IO completion.
425ccb26b5aSJan Kara 		 */
426ccb26b5aSJan Kara 		redirty_tail(inode, wb);
427ccb26b5aSJan Kara 	} else {
428ccb26b5aSJan Kara 		/* The inode is clean. Remove from writeback lists. */
429ccb26b5aSJan Kara 		list_del_init(&inode->i_wb_list);
430ccb26b5aSJan Kara 	}
431ccb26b5aSJan Kara }
432ccb26b5aSJan Kara 
433ccb26b5aSJan Kara /*
4344f8ad655SJan Kara  * Write out an inode and its dirty pages. Do not update the writeback list
4354f8ad655SJan Kara  * linkage. That is left to the caller. The caller is also responsible for
4364f8ad655SJan Kara  * setting I_SYNC flag and calling inode_sync_complete() to clear it.
4371da177e4SLinus Torvalds  */
4381da177e4SLinus Torvalds static int
439cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
4401da177e4SLinus Torvalds {
4411da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
442251d6a47SWu Fengguang 	long nr_to_write = wbc->nr_to_write;
44301c03194SChristoph Hellwig 	unsigned dirty;
4441da177e4SLinus Torvalds 	int ret;
4451da177e4SLinus Torvalds 
4464f8ad655SJan Kara 	WARN_ON(!(inode->i_state & I_SYNC));
4471da177e4SLinus Torvalds 
4489fb0a7daSTejun Heo 	trace_writeback_single_inode_start(inode, wbc, nr_to_write);
4499fb0a7daSTejun Heo 
4501da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
4511da177e4SLinus Torvalds 
45226821ed4SChristoph Hellwig 	/*
45326821ed4SChristoph Hellwig 	 * Make sure to wait on the data before writing out the metadata.
45426821ed4SChristoph Hellwig 	 * This is important for filesystems that modify metadata on data
4557747bd4bSDave Chinner 	 * I/O completion. We don't do it for sync(2) writeback because it has a
4567747bd4bSDave Chinner 	 * separate, external IO completion path and ->sync_fs for guaranteeing
4577747bd4bSDave Chinner 	 * inode metadata is written back correctly.
45826821ed4SChristoph Hellwig 	 */
4597747bd4bSDave Chinner 	if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) {
46026821ed4SChristoph Hellwig 		int err = filemap_fdatawait(mapping);
4611da177e4SLinus Torvalds 		if (ret == 0)
4621da177e4SLinus Torvalds 			ret = err;
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds 
4655547e8aaSDmitry Monakhov 	/*
4665547e8aaSDmitry Monakhov 	 * Some filesystems may redirty the inode during the writeback
4675547e8aaSDmitry Monakhov 	 * due to delalloc, clear dirty metadata flags right before
4685547e8aaSDmitry Monakhov 	 * write_inode()
4695547e8aaSDmitry Monakhov 	 */
470250df6edSDave Chinner 	spin_lock(&inode->i_lock);
4716290be1cSJan Kara 	/* Clear I_DIRTY_PAGES if we've written out all dirty pages */
4726290be1cSJan Kara 	if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
4736290be1cSJan Kara 		inode->i_state &= ~I_DIRTY_PAGES;
4745547e8aaSDmitry Monakhov 	dirty = inode->i_state & I_DIRTY;
4755547e8aaSDmitry Monakhov 	inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC);
476250df6edSDave Chinner 	spin_unlock(&inode->i_lock);
47726821ed4SChristoph Hellwig 	/* Don't write the inode if only I_DIRTY_PAGES was set */
47826821ed4SChristoph Hellwig 	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
479a9185b41SChristoph Hellwig 		int err = write_inode(inode, wbc);
4801da177e4SLinus Torvalds 		if (ret == 0)
4811da177e4SLinus Torvalds 			ret = err;
4821da177e4SLinus Torvalds 	}
4834f8ad655SJan Kara 	trace_writeback_single_inode(inode, wbc, nr_to_write);
4844f8ad655SJan Kara 	return ret;
4854f8ad655SJan Kara }
4864f8ad655SJan Kara 
4874f8ad655SJan Kara /*
4884f8ad655SJan Kara  * Write out an inode's dirty pages. Either the caller has an active reference
4894f8ad655SJan Kara  * on the inode or the inode has I_WILL_FREE set.
4904f8ad655SJan Kara  *
4914f8ad655SJan Kara  * This function is designed to be called for writing back one inode which
4924f8ad655SJan Kara  * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode()
4934f8ad655SJan Kara  * and does more profound writeback list handling in writeback_sb_inodes().
4944f8ad655SJan Kara  */
4954f8ad655SJan Kara static int
4964f8ad655SJan Kara writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
4974f8ad655SJan Kara 		       struct writeback_control *wbc)
4984f8ad655SJan Kara {
4994f8ad655SJan Kara 	int ret = 0;
5004f8ad655SJan Kara 
5014f8ad655SJan Kara 	spin_lock(&inode->i_lock);
5024f8ad655SJan Kara 	if (!atomic_read(&inode->i_count))
5034f8ad655SJan Kara 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
5044f8ad655SJan Kara 	else
5054f8ad655SJan Kara 		WARN_ON(inode->i_state & I_WILL_FREE);
5064f8ad655SJan Kara 
5074f8ad655SJan Kara 	if (inode->i_state & I_SYNC) {
5084f8ad655SJan Kara 		if (wbc->sync_mode != WB_SYNC_ALL)
5094f8ad655SJan Kara 			goto out;
5104f8ad655SJan Kara 		/*
511169ebd90SJan Kara 		 * It's a data-integrity sync. We must wait. Since callers hold
512169ebd90SJan Kara 		 * inode reference or inode has I_WILL_FREE set, it cannot go
513169ebd90SJan Kara 		 * away under us.
5144f8ad655SJan Kara 		 */
515169ebd90SJan Kara 		__inode_wait_for_writeback(inode);
5164f8ad655SJan Kara 	}
5174f8ad655SJan Kara 	WARN_ON(inode->i_state & I_SYNC);
5184f8ad655SJan Kara 	/*
519f9b0e058SJan Kara 	 * Skip inode if it is clean and we have no outstanding writeback in
520f9b0e058SJan Kara 	 * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this
521f9b0e058SJan Kara 	 * function since flusher thread may be doing for example sync in
522f9b0e058SJan Kara 	 * parallel and if we move the inode, it could get skipped. So here we
523f9b0e058SJan Kara 	 * make sure inode is on some writeback list and leave it there unless
524f9b0e058SJan Kara 	 * we have completely cleaned the inode.
5254f8ad655SJan Kara 	 */
526f9b0e058SJan Kara 	if (!(inode->i_state & I_DIRTY) &&
527f9b0e058SJan Kara 	    (wbc->sync_mode != WB_SYNC_ALL ||
528f9b0e058SJan Kara 	     !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)))
5294f8ad655SJan Kara 		goto out;
5304f8ad655SJan Kara 	inode->i_state |= I_SYNC;
5314f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
5324f8ad655SJan Kara 
533cd8ed2a4SYan Hong 	ret = __writeback_single_inode(inode, wbc);
5341da177e4SLinus Torvalds 
535f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
536250df6edSDave Chinner 	spin_lock(&inode->i_lock);
5374f8ad655SJan Kara 	/*
5384f8ad655SJan Kara 	 * If inode is clean, remove it from writeback lists. Otherwise don't
5394f8ad655SJan Kara 	 * touch it. See comment above for explanation.
5404f8ad655SJan Kara 	 */
5414f8ad655SJan Kara 	if (!(inode->i_state & I_DIRTY))
5424f8ad655SJan Kara 		list_del_init(&inode->i_wb_list);
5434f8ad655SJan Kara 	spin_unlock(&wb->list_lock);
5441c0eeaf5SJoern Engel 	inode_sync_complete(inode);
5454f8ad655SJan Kara out:
5464f8ad655SJan Kara 	spin_unlock(&inode->i_lock);
5471da177e4SLinus Torvalds 	return ret;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501a12d8bdSWu Fengguang static long writeback_chunk_size(struct backing_dev_info *bdi,
5511a12d8bdSWu Fengguang 				 struct wb_writeback_work *work)
552d46db3d5SWu Fengguang {
553d46db3d5SWu Fengguang 	long pages;
554d46db3d5SWu Fengguang 
555d46db3d5SWu Fengguang 	/*
556d46db3d5SWu Fengguang 	 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
557d46db3d5SWu Fengguang 	 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
558d46db3d5SWu Fengguang 	 * here avoids calling into writeback_inodes_wb() more than once.
559d46db3d5SWu Fengguang 	 *
560d46db3d5SWu Fengguang 	 * The intended call sequence for WB_SYNC_ALL writeback is:
561d46db3d5SWu Fengguang 	 *
562d46db3d5SWu Fengguang 	 *      wb_writeback()
563d46db3d5SWu Fengguang 	 *          writeback_sb_inodes()       <== called only once
564d46db3d5SWu Fengguang 	 *              write_cache_pages()     <== called once for each inode
565d46db3d5SWu Fengguang 	 *                   (quickly) tag currently dirty pages
566d46db3d5SWu Fengguang 	 *                   (maybe slowly) sync all tagged pages
567d46db3d5SWu Fengguang 	 */
568d46db3d5SWu Fengguang 	if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
569d46db3d5SWu Fengguang 		pages = LONG_MAX;
5701a12d8bdSWu Fengguang 	else {
5711a12d8bdSWu Fengguang 		pages = min(bdi->avg_write_bandwidth / 2,
5721a12d8bdSWu Fengguang 			    global_dirty_limit / DIRTY_SCOPE);
5731a12d8bdSWu Fengguang 		pages = min(pages, work->nr_pages);
5741a12d8bdSWu Fengguang 		pages = round_down(pages + MIN_WRITEBACK_PAGES,
5751a12d8bdSWu Fengguang 				   MIN_WRITEBACK_PAGES);
5761a12d8bdSWu Fengguang 	}
577d46db3d5SWu Fengguang 
578d46db3d5SWu Fengguang 	return pages;
579d46db3d5SWu Fengguang }
580d46db3d5SWu Fengguang 
58103ba3782SJens Axboe /*
582f11c9c5cSEdward Shishkin  * Write a portion of b_io inodes which belong to @sb.
583edadfb10SChristoph Hellwig  *
584d46db3d5SWu Fengguang  * Return the number of pages and/or inodes written.
585f11c9c5cSEdward Shishkin  */
586d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb,
587d46db3d5SWu Fengguang 				struct bdi_writeback *wb,
588d46db3d5SWu Fengguang 				struct wb_writeback_work *work)
58903ba3782SJens Axboe {
590d46db3d5SWu Fengguang 	struct writeback_control wbc = {
591d46db3d5SWu Fengguang 		.sync_mode		= work->sync_mode,
592d46db3d5SWu Fengguang 		.tagged_writepages	= work->tagged_writepages,
593d46db3d5SWu Fengguang 		.for_kupdate		= work->for_kupdate,
594d46db3d5SWu Fengguang 		.for_background		= work->for_background,
5957747bd4bSDave Chinner 		.for_sync		= work->for_sync,
596d46db3d5SWu Fengguang 		.range_cyclic		= work->range_cyclic,
597d46db3d5SWu Fengguang 		.range_start		= 0,
598d46db3d5SWu Fengguang 		.range_end		= LLONG_MAX,
599d46db3d5SWu Fengguang 	};
600d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
601d46db3d5SWu Fengguang 	long write_chunk;
602d46db3d5SWu Fengguang 	long wrote = 0;  /* count both pages and inodes */
603d46db3d5SWu Fengguang 
60403ba3782SJens Axboe 	while (!list_empty(&wb->b_io)) {
6057ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
606edadfb10SChristoph Hellwig 
607edadfb10SChristoph Hellwig 		if (inode->i_sb != sb) {
608d46db3d5SWu Fengguang 			if (work->sb) {
609edadfb10SChristoph Hellwig 				/*
610edadfb10SChristoph Hellwig 				 * We only want to write back data for this
611edadfb10SChristoph Hellwig 				 * superblock, move all inodes not belonging
612edadfb10SChristoph Hellwig 				 * to it back onto the dirty list.
613edadfb10SChristoph Hellwig 				 */
614f758eeabSChristoph Hellwig 				redirty_tail(inode, wb);
61566f3b8e2SJens Axboe 				continue;
61666f3b8e2SJens Axboe 			}
617edadfb10SChristoph Hellwig 
618edadfb10SChristoph Hellwig 			/*
619edadfb10SChristoph Hellwig 			 * The inode belongs to a different superblock.
620edadfb10SChristoph Hellwig 			 * Bounce back to the caller to unpin this and
621edadfb10SChristoph Hellwig 			 * pin the next superblock.
622edadfb10SChristoph Hellwig 			 */
623d46db3d5SWu Fengguang 			break;
624edadfb10SChristoph Hellwig 		}
625edadfb10SChristoph Hellwig 
6269843b76aSChristoph Hellwig 		/*
627331cbdeeSWanpeng Li 		 * Don't bother with new inodes or inodes being freed, first
628331cbdeeSWanpeng Li 		 * kind does not need periodic writeout yet, and for the latter
6299843b76aSChristoph Hellwig 		 * kind writeout is handled by the freer.
6309843b76aSChristoph Hellwig 		 */
631250df6edSDave Chinner 		spin_lock(&inode->i_lock);
6329843b76aSChristoph Hellwig 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
633250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
634fcc5c222SWu Fengguang 			redirty_tail(inode, wb);
6357ef0d737SNick Piggin 			continue;
6367ef0d737SNick Piggin 		}
637cc1676d9SJan Kara 		if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) {
638cc1676d9SJan Kara 			/*
639cc1676d9SJan Kara 			 * If this inode is locked for writeback and we are not
640cc1676d9SJan Kara 			 * doing writeback-for-data-integrity, move it to
641cc1676d9SJan Kara 			 * b_more_io so that writeback can proceed with the
642cc1676d9SJan Kara 			 * other inodes on s_io.
643cc1676d9SJan Kara 			 *
644cc1676d9SJan Kara 			 * We'll have another go at writing back this inode
645cc1676d9SJan Kara 			 * when we completed a full scan of b_io.
646cc1676d9SJan Kara 			 */
647cc1676d9SJan Kara 			spin_unlock(&inode->i_lock);
648cc1676d9SJan Kara 			requeue_io(inode, wb);
649cc1676d9SJan Kara 			trace_writeback_sb_inodes_requeue(inode);
650cc1676d9SJan Kara 			continue;
651cc1676d9SJan Kara 		}
652f0d07b7fSJan Kara 		spin_unlock(&wb->list_lock);
653f0d07b7fSJan Kara 
6544f8ad655SJan Kara 		/*
6554f8ad655SJan Kara 		 * We already requeued the inode if it had I_SYNC set and we
6564f8ad655SJan Kara 		 * are doing WB_SYNC_NONE writeback. So this catches only the
6574f8ad655SJan Kara 		 * WB_SYNC_ALL case.
6584f8ad655SJan Kara 		 */
659169ebd90SJan Kara 		if (inode->i_state & I_SYNC) {
660169ebd90SJan Kara 			/* Wait for I_SYNC. This function drops i_lock... */
661169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
662169ebd90SJan Kara 			/* Inode may be gone, start again */
663ead188f9SJan Kara 			spin_lock(&wb->list_lock);
664169ebd90SJan Kara 			continue;
665169ebd90SJan Kara 		}
6664f8ad655SJan Kara 		inode->i_state |= I_SYNC;
6674f8ad655SJan Kara 		spin_unlock(&inode->i_lock);
668169ebd90SJan Kara 
6691a12d8bdSWu Fengguang 		write_chunk = writeback_chunk_size(wb->bdi, work);
670d46db3d5SWu Fengguang 		wbc.nr_to_write = write_chunk;
671d46db3d5SWu Fengguang 		wbc.pages_skipped = 0;
672250df6edSDave Chinner 
673169ebd90SJan Kara 		/*
674169ebd90SJan Kara 		 * We use I_SYNC to pin the inode in memory. While it is set
675169ebd90SJan Kara 		 * evict_inode() will wait so the inode cannot be freed.
676169ebd90SJan Kara 		 */
677cd8ed2a4SYan Hong 		__writeback_single_inode(inode, &wbc);
678d46db3d5SWu Fengguang 
679d46db3d5SWu Fengguang 		work->nr_pages -= write_chunk - wbc.nr_to_write;
680d46db3d5SWu Fengguang 		wrote += write_chunk - wbc.nr_to_write;
6814f8ad655SJan Kara 		spin_lock(&wb->list_lock);
6824f8ad655SJan Kara 		spin_lock(&inode->i_lock);
683d46db3d5SWu Fengguang 		if (!(inode->i_state & I_DIRTY))
684d46db3d5SWu Fengguang 			wrote++;
6854f8ad655SJan Kara 		requeue_inode(inode, wb, &wbc);
6864f8ad655SJan Kara 		inode_sync_complete(inode);
6870f1b1fd8SDave Chinner 		spin_unlock(&inode->i_lock);
688169ebd90SJan Kara 		cond_resched_lock(&wb->list_lock);
689d46db3d5SWu Fengguang 		/*
690d46db3d5SWu Fengguang 		 * bail out to wb_writeback() often enough to check
691d46db3d5SWu Fengguang 		 * background threshold and other termination conditions.
692d46db3d5SWu Fengguang 		 */
693d46db3d5SWu Fengguang 		if (wrote) {
694d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
695d46db3d5SWu Fengguang 				break;
696d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
697d46db3d5SWu Fengguang 				break;
6981da177e4SLinus Torvalds 		}
6998bc3be27SFengguang Wu 	}
700d46db3d5SWu Fengguang 	return wrote;
701f11c9c5cSEdward Shishkin }
70238f21977SNick Piggin 
703d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb,
704d46db3d5SWu Fengguang 				  struct wb_writeback_work *work)
705f11c9c5cSEdward Shishkin {
706d46db3d5SWu Fengguang 	unsigned long start_time = jiffies;
707d46db3d5SWu Fengguang 	long wrote = 0;
708f11c9c5cSEdward Shishkin 
709f11c9c5cSEdward Shishkin 	while (!list_empty(&wb->b_io)) {
7107ccf19a8SNick Piggin 		struct inode *inode = wb_inode(wb->b_io.prev);
711f11c9c5cSEdward Shishkin 		struct super_block *sb = inode->i_sb;
712f11c9c5cSEdward Shishkin 
71312ad3ab6SDave Chinner 		if (!grab_super_passive(sb)) {
7140e995816SWu Fengguang 			/*
7150e995816SWu Fengguang 			 * grab_super_passive() may fail consistently due to
7160e995816SWu Fengguang 			 * s_umount being grabbed by someone else. Don't use
7170e995816SWu Fengguang 			 * requeue_io() to avoid busy retrying the inode/sb.
7180e995816SWu Fengguang 			 */
7190e995816SWu Fengguang 			redirty_tail(inode, wb);
720d19de7edSChristoph Hellwig 			continue;
721334132aeSChristoph Hellwig 		}
722d46db3d5SWu Fengguang 		wrote += writeback_sb_inodes(sb, wb, work);
723d19de7edSChristoph Hellwig 		drop_super(sb);
724f11c9c5cSEdward Shishkin 
725d46db3d5SWu Fengguang 		/* refer to the same tests at the end of writeback_sb_inodes */
726d46db3d5SWu Fengguang 		if (wrote) {
727d46db3d5SWu Fengguang 			if (time_is_before_jiffies(start_time + HZ / 10UL))
728d46db3d5SWu Fengguang 				break;
729d46db3d5SWu Fengguang 			if (work->nr_pages <= 0)
730f11c9c5cSEdward Shishkin 				break;
731f11c9c5cSEdward Shishkin 		}
732d46db3d5SWu Fengguang 	}
73366f3b8e2SJens Axboe 	/* Leave any unwritten inodes on b_io */
734d46db3d5SWu Fengguang 	return wrote;
73566f3b8e2SJens Axboe }
73666f3b8e2SJens Axboe 
7377d9f073bSWanpeng Li static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
7380e175a18SCurt Wohlgemuth 				enum wb_reason reason)
739edadfb10SChristoph Hellwig {
740d46db3d5SWu Fengguang 	struct wb_writeback_work work = {
741d46db3d5SWu Fengguang 		.nr_pages	= nr_pages,
742d46db3d5SWu Fengguang 		.sync_mode	= WB_SYNC_NONE,
743d46db3d5SWu Fengguang 		.range_cyclic	= 1,
7440e175a18SCurt Wohlgemuth 		.reason		= reason,
745c4a391b5SJan Kara 		.older_than_this = jiffies,
746c4a391b5SJan Kara 		.older_than_this_is_set = 1,
747d46db3d5SWu Fengguang 	};
748edadfb10SChristoph Hellwig 
749f758eeabSChristoph Hellwig 	spin_lock(&wb->list_lock);
750424b351fSWu Fengguang 	if (list_empty(&wb->b_io))
751ad4e38ddSCurt Wohlgemuth 		queue_io(wb, &work);
752d46db3d5SWu Fengguang 	__writeback_inodes_wb(wb, &work);
753f758eeabSChristoph Hellwig 	spin_unlock(&wb->list_lock);
754edadfb10SChristoph Hellwig 
755d46db3d5SWu Fengguang 	return nr_pages - work.nr_pages;
75666f3b8e2SJens Axboe }
75766f3b8e2SJens Axboe 
758b00949aaSWu Fengguang static bool over_bground_thresh(struct backing_dev_info *bdi)
75903ba3782SJens Axboe {
76003ba3782SJens Axboe 	unsigned long background_thresh, dirty_thresh;
76103ba3782SJens Axboe 
76216c4042fSWu Fengguang 	global_dirty_limits(&background_thresh, &dirty_thresh);
76303ba3782SJens Axboe 
764b00949aaSWu Fengguang 	if (global_page_state(NR_FILE_DIRTY) +
765b00949aaSWu Fengguang 	    global_page_state(NR_UNSTABLE_NFS) > background_thresh)
766b00949aaSWu Fengguang 		return true;
767b00949aaSWu Fengguang 
768b00949aaSWu Fengguang 	if (bdi_stat(bdi, BDI_RECLAIMABLE) >
769b00949aaSWu Fengguang 				bdi_dirty_limit(bdi, background_thresh))
770b00949aaSWu Fengguang 		return true;
771b00949aaSWu Fengguang 
772b00949aaSWu Fengguang 	return false;
77303ba3782SJens Axboe }
77403ba3782SJens Axboe 
77503ba3782SJens Axboe /*
776e98be2d5SWu Fengguang  * Called under wb->list_lock. If there are multiple wb per bdi,
777e98be2d5SWu Fengguang  * only the flusher working on the first wb should do it.
778e98be2d5SWu Fengguang  */
779e98be2d5SWu Fengguang static void wb_update_bandwidth(struct bdi_writeback *wb,
780e98be2d5SWu Fengguang 				unsigned long start_time)
781e98be2d5SWu Fengguang {
782af6a3113SWu Fengguang 	__bdi_update_bandwidth(wb->bdi, 0, 0, 0, 0, 0, start_time);
783e98be2d5SWu Fengguang }
784e98be2d5SWu Fengguang 
785e98be2d5SWu Fengguang /*
78603ba3782SJens Axboe  * Explicit flushing or periodic writeback of "old" data.
78703ba3782SJens Axboe  *
78803ba3782SJens Axboe  * Define "old": the first time one of an inode's pages is dirtied, we mark the
78903ba3782SJens Axboe  * dirtying-time in the inode's address_space.  So this periodic writeback code
79003ba3782SJens Axboe  * just walks the superblock inode list, writing back any inodes which are
79103ba3782SJens Axboe  * older than a specific point in time.
79203ba3782SJens Axboe  *
79303ba3782SJens Axboe  * Try to run once per dirty_writeback_interval.  But if a writeback event
79403ba3782SJens Axboe  * takes longer than a dirty_writeback_interval interval, then leave a
79503ba3782SJens Axboe  * one-second gap.
79603ba3782SJens Axboe  *
79703ba3782SJens Axboe  * older_than_this takes precedence over nr_to_write.  So we'll only write back
79803ba3782SJens Axboe  * all dirty pages if they are all attached to "old" mappings.
79903ba3782SJens Axboe  */
800c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb,
80183ba7b07SChristoph Hellwig 			 struct wb_writeback_work *work)
80203ba3782SJens Axboe {
803e98be2d5SWu Fengguang 	unsigned long wb_start = jiffies;
804d46db3d5SWu Fengguang 	long nr_pages = work->nr_pages;
805a5989bdcSJan Kara 	struct inode *inode;
806d46db3d5SWu Fengguang 	long progress;
80703ba3782SJens Axboe 
808c4a391b5SJan Kara 	if (!work->older_than_this_is_set) {
809c4a391b5SJan Kara 		work->older_than_this = jiffies;
810c4a391b5SJan Kara 		work->older_than_this_is_set = 1;
811c4a391b5SJan Kara 	}
81203ba3782SJens Axboe 
813e8dfc305SWu Fengguang 	spin_lock(&wb->list_lock);
81403ba3782SJens Axboe 	for (;;) {
81503ba3782SJens Axboe 		/*
816d3ddec76SWu Fengguang 		 * Stop writeback when nr_pages has been consumed
81703ba3782SJens Axboe 		 */
81883ba7b07SChristoph Hellwig 		if (work->nr_pages <= 0)
81903ba3782SJens Axboe 			break;
82003ba3782SJens Axboe 
82103ba3782SJens Axboe 		/*
822aa373cf5SJan Kara 		 * Background writeout and kupdate-style writeback may
823aa373cf5SJan Kara 		 * run forever. Stop them if there is other work to do
824aa373cf5SJan Kara 		 * so that e.g. sync can proceed. They'll be restarted
825aa373cf5SJan Kara 		 * after the other works are all done.
826aa373cf5SJan Kara 		 */
827aa373cf5SJan Kara 		if ((work->for_background || work->for_kupdate) &&
828aa373cf5SJan Kara 		    !list_empty(&wb->bdi->work_list))
829aa373cf5SJan Kara 			break;
830aa373cf5SJan Kara 
831aa373cf5SJan Kara 		/*
832d3ddec76SWu Fengguang 		 * For background writeout, stop when we are below the
833d3ddec76SWu Fengguang 		 * background dirty threshold
83403ba3782SJens Axboe 		 */
835b00949aaSWu Fengguang 		if (work->for_background && !over_bground_thresh(wb->bdi))
83603ba3782SJens Axboe 			break;
83703ba3782SJens Axboe 
8381bc36b64SJan Kara 		/*
8391bc36b64SJan Kara 		 * Kupdate and background works are special and we want to
8401bc36b64SJan Kara 		 * include all inodes that need writing. Livelock avoidance is
8411bc36b64SJan Kara 		 * handled by these works yielding to any other work so we are
8421bc36b64SJan Kara 		 * safe.
8431bc36b64SJan Kara 		 */
844ba9aa839SWu Fengguang 		if (work->for_kupdate) {
845c4a391b5SJan Kara 			work->older_than_this = jiffies -
846ba9aa839SWu Fengguang 				msecs_to_jiffies(dirty_expire_interval * 10);
8471bc36b64SJan Kara 		} else if (work->for_background)
848c4a391b5SJan Kara 			work->older_than_this = jiffies;
849028c2dd1SDave Chinner 
850d46db3d5SWu Fengguang 		trace_writeback_start(wb->bdi, work);
851e8dfc305SWu Fengguang 		if (list_empty(&wb->b_io))
852ad4e38ddSCurt Wohlgemuth 			queue_io(wb, work);
85383ba7b07SChristoph Hellwig 		if (work->sb)
854d46db3d5SWu Fengguang 			progress = writeback_sb_inodes(work->sb, wb, work);
855edadfb10SChristoph Hellwig 		else
856d46db3d5SWu Fengguang 			progress = __writeback_inodes_wb(wb, work);
857d46db3d5SWu Fengguang 		trace_writeback_written(wb->bdi, work);
858028c2dd1SDave Chinner 
859e98be2d5SWu Fengguang 		wb_update_bandwidth(wb, wb_start);
86003ba3782SJens Axboe 
86103ba3782SJens Axboe 		/*
86271fd05a8SJens Axboe 		 * Did we write something? Try for more
863e6fb6da2SWu Fengguang 		 *
864e6fb6da2SWu Fengguang 		 * Dirty inodes are moved to b_io for writeback in batches.
865e6fb6da2SWu Fengguang 		 * The completion of the current batch does not necessarily
866e6fb6da2SWu Fengguang 		 * mean the overall work is done. So we keep looping as long
867e6fb6da2SWu Fengguang 		 * as made some progress on cleaning pages or inodes.
86871fd05a8SJens Axboe 		 */
869d46db3d5SWu Fengguang 		if (progress)
87003ba3782SJens Axboe 			continue;
871a5989bdcSJan Kara 		/*
872e6fb6da2SWu Fengguang 		 * No more inodes for IO, bail
873a5989bdcSJan Kara 		 */
874b7a2441fSWu Fengguang 		if (list_empty(&wb->b_more_io))
87503ba3782SJens Axboe 			break;
87603ba3782SJens Axboe 		/*
8778010c3b6SJens Axboe 		 * Nothing written. Wait for some inode to
8788010c3b6SJens Axboe 		 * become available for writeback. Otherwise
8798010c3b6SJens Axboe 		 * we'll just busyloop.
88003ba3782SJens Axboe 		 */
88103ba3782SJens Axboe 		if (!list_empty(&wb->b_more_io))  {
882d46db3d5SWu Fengguang 			trace_writeback_wait(wb->bdi, work);
88303ba3782SJens Axboe 			inode = wb_inode(wb->b_more_io.prev);
884250df6edSDave Chinner 			spin_lock(&inode->i_lock);
885f0d07b7fSJan Kara 			spin_unlock(&wb->list_lock);
886169ebd90SJan Kara 			/* This function drops i_lock... */
887169ebd90SJan Kara 			inode_sleep_on_writeback(inode);
888f0d07b7fSJan Kara 			spin_lock(&wb->list_lock);
88903ba3782SJens Axboe 		}
89003ba3782SJens Axboe 	}
891e8dfc305SWu Fengguang 	spin_unlock(&wb->list_lock);
89203ba3782SJens Axboe 
893d46db3d5SWu Fengguang 	return nr_pages - work->nr_pages;
89403ba3782SJens Axboe }
89503ba3782SJens Axboe 
89603ba3782SJens Axboe /*
89783ba7b07SChristoph Hellwig  * Return the next wb_writeback_work struct that hasn't been processed yet.
89803ba3782SJens Axboe  */
89983ba7b07SChristoph Hellwig static struct wb_writeback_work *
90008852b6dSMinchan Kim get_next_work_item(struct backing_dev_info *bdi)
90103ba3782SJens Axboe {
90283ba7b07SChristoph Hellwig 	struct wb_writeback_work *work = NULL;
90303ba3782SJens Axboe 
9046467716aSArtem Bityutskiy 	spin_lock_bh(&bdi->wb_lock);
90583ba7b07SChristoph Hellwig 	if (!list_empty(&bdi->work_list)) {
90683ba7b07SChristoph Hellwig 		work = list_entry(bdi->work_list.next,
90783ba7b07SChristoph Hellwig 				  struct wb_writeback_work, list);
90883ba7b07SChristoph Hellwig 		list_del_init(&work->list);
90903ba3782SJens Axboe 	}
9106467716aSArtem Bityutskiy 	spin_unlock_bh(&bdi->wb_lock);
91183ba7b07SChristoph Hellwig 	return work;
91203ba3782SJens Axboe }
91303ba3782SJens Axboe 
914cdf01dd5SLinus Torvalds /*
915cdf01dd5SLinus Torvalds  * Add in the number of potentially dirty inodes, because each inode
916cdf01dd5SLinus Torvalds  * write can dirty pagecache in the underlying blockdev.
917cdf01dd5SLinus Torvalds  */
918cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void)
919cdf01dd5SLinus Torvalds {
920cdf01dd5SLinus Torvalds 	return global_page_state(NR_FILE_DIRTY) +
921cdf01dd5SLinus Torvalds 		global_page_state(NR_UNSTABLE_NFS) +
922cdf01dd5SLinus Torvalds 		get_nr_dirty_inodes();
923cdf01dd5SLinus Torvalds }
924cdf01dd5SLinus Torvalds 
9256585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb)
9266585027aSJan Kara {
927b00949aaSWu Fengguang 	if (over_bground_thresh(wb->bdi)) {
9286585027aSJan Kara 
9296585027aSJan Kara 		struct wb_writeback_work work = {
9306585027aSJan Kara 			.nr_pages	= LONG_MAX,
9316585027aSJan Kara 			.sync_mode	= WB_SYNC_NONE,
9326585027aSJan Kara 			.for_background	= 1,
9336585027aSJan Kara 			.range_cyclic	= 1,
9340e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_BACKGROUND,
9356585027aSJan Kara 		};
9366585027aSJan Kara 
9376585027aSJan Kara 		return wb_writeback(wb, &work);
9386585027aSJan Kara 	}
9396585027aSJan Kara 
9406585027aSJan Kara 	return 0;
9416585027aSJan Kara }
9426585027aSJan Kara 
94303ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb)
94403ba3782SJens Axboe {
94503ba3782SJens Axboe 	unsigned long expired;
94603ba3782SJens Axboe 	long nr_pages;
94703ba3782SJens Axboe 
94869b62d01SJens Axboe 	/*
94969b62d01SJens Axboe 	 * When set to zero, disable periodic writeback
95069b62d01SJens Axboe 	 */
95169b62d01SJens Axboe 	if (!dirty_writeback_interval)
95269b62d01SJens Axboe 		return 0;
95369b62d01SJens Axboe 
95403ba3782SJens Axboe 	expired = wb->last_old_flush +
95503ba3782SJens Axboe 			msecs_to_jiffies(dirty_writeback_interval * 10);
95603ba3782SJens Axboe 	if (time_before(jiffies, expired))
95703ba3782SJens Axboe 		return 0;
95803ba3782SJens Axboe 
95903ba3782SJens Axboe 	wb->last_old_flush = jiffies;
960cdf01dd5SLinus Torvalds 	nr_pages = get_nr_dirty_pages();
96103ba3782SJens Axboe 
962c4a77a6cSJens Axboe 	if (nr_pages) {
96383ba7b07SChristoph Hellwig 		struct wb_writeback_work work = {
964c4a77a6cSJens Axboe 			.nr_pages	= nr_pages,
965c4a77a6cSJens Axboe 			.sync_mode	= WB_SYNC_NONE,
966c4a77a6cSJens Axboe 			.for_kupdate	= 1,
967c4a77a6cSJens Axboe 			.range_cyclic	= 1,
9680e175a18SCurt Wohlgemuth 			.reason		= WB_REASON_PERIODIC,
969c4a77a6cSJens Axboe 		};
970c4a77a6cSJens Axboe 
97183ba7b07SChristoph Hellwig 		return wb_writeback(wb, &work);
972c4a77a6cSJens Axboe 	}
97303ba3782SJens Axboe 
97403ba3782SJens Axboe 	return 0;
97503ba3782SJens Axboe }
97603ba3782SJens Axboe 
97703ba3782SJens Axboe /*
97803ba3782SJens Axboe  * Retrieve work items and do the writeback they describe
97903ba3782SJens Axboe  */
98025d130baSWanpeng Li static long wb_do_writeback(struct bdi_writeback *wb)
98103ba3782SJens Axboe {
98203ba3782SJens Axboe 	struct backing_dev_info *bdi = wb->bdi;
98383ba7b07SChristoph Hellwig 	struct wb_writeback_work *work;
984c4a77a6cSJens Axboe 	long wrote = 0;
98503ba3782SJens Axboe 
98681d73a32SJan Kara 	set_bit(BDI_writeback_running, &wb->bdi->state);
98708852b6dSMinchan Kim 	while ((work = get_next_work_item(bdi)) != NULL) {
98883ba7b07SChristoph Hellwig 
989455b2864SDave Chinner 		trace_writeback_exec(bdi, work);
990455b2864SDave Chinner 
99183ba7b07SChristoph Hellwig 		wrote += wb_writeback(wb, work);
99203ba3782SJens Axboe 
99303ba3782SJens Axboe 		/*
99483ba7b07SChristoph Hellwig 		 * Notify the caller of completion if this is a synchronous
99583ba7b07SChristoph Hellwig 		 * work item, otherwise just free it.
99603ba3782SJens Axboe 		 */
99783ba7b07SChristoph Hellwig 		if (work->done)
99883ba7b07SChristoph Hellwig 			complete(work->done);
99983ba7b07SChristoph Hellwig 		else
100083ba7b07SChristoph Hellwig 			kfree(work);
100103ba3782SJens Axboe 	}
100203ba3782SJens Axboe 
100303ba3782SJens Axboe 	/*
100403ba3782SJens Axboe 	 * Check for periodic writeback, kupdated() style
100503ba3782SJens Axboe 	 */
100603ba3782SJens Axboe 	wrote += wb_check_old_data_flush(wb);
10076585027aSJan Kara 	wrote += wb_check_background_flush(wb);
100881d73a32SJan Kara 	clear_bit(BDI_writeback_running, &wb->bdi->state);
100903ba3782SJens Axboe 
101003ba3782SJens Axboe 	return wrote;
101103ba3782SJens Axboe }
101203ba3782SJens Axboe 
101303ba3782SJens Axboe /*
101403ba3782SJens Axboe  * Handle writeback of dirty data for the device backed by this bdi. Also
1015839a8e86STejun Heo  * reschedules periodically and does kupdated style flushing.
101603ba3782SJens Axboe  */
1017839a8e86STejun Heo void bdi_writeback_workfn(struct work_struct *work)
101803ba3782SJens Axboe {
1019839a8e86STejun Heo 	struct bdi_writeback *wb = container_of(to_delayed_work(work),
1020839a8e86STejun Heo 						struct bdi_writeback, dwork);
102108243900SChristoph Hellwig 	struct backing_dev_info *bdi = wb->bdi;
102203ba3782SJens Axboe 	long pages_written;
102303ba3782SJens Axboe 
1024ef3b1019STejun Heo 	set_worker_desc("flush-%s", dev_name(bdi->dev));
1025766f9164SPeter Zijlstra 	current->flags |= PF_SWAPWRITE;
102603ba3782SJens Axboe 
1027839a8e86STejun Heo 	if (likely(!current_is_workqueue_rescuer() ||
1028839a8e86STejun Heo 		   list_empty(&bdi->bdi_list))) {
102903ba3782SJens Axboe 		/*
1030839a8e86STejun Heo 		 * The normal path.  Keep writing back @bdi until its
1031839a8e86STejun Heo 		 * work_list is empty.  Note that this path is also taken
1032839a8e86STejun Heo 		 * if @bdi is shutting down even when we're running off the
1033839a8e86STejun Heo 		 * rescuer as work_list needs to be drained.
103403ba3782SJens Axboe 		 */
1035839a8e86STejun Heo 		do {
103625d130baSWanpeng Li 			pages_written = wb_do_writeback(wb);
1037455b2864SDave Chinner 			trace_writeback_pages_written(pages_written);
1038839a8e86STejun Heo 		} while (!list_empty(&bdi->work_list));
1039839a8e86STejun Heo 	} else {
1040253c34e9SArtem Bityutskiy 		/*
1041839a8e86STejun Heo 		 * bdi_wq can't get enough workers and we're running off
1042839a8e86STejun Heo 		 * the emergency worker.  Don't hog it.  Hopefully, 1024 is
1043839a8e86STejun Heo 		 * enough for efficient IO.
1044253c34e9SArtem Bityutskiy 		 */
1045839a8e86STejun Heo 		pages_written = writeback_inodes_wb(&bdi->wb, 1024,
1046839a8e86STejun Heo 						    WB_REASON_FORKER_THREAD);
1047839a8e86STejun Heo 		trace_writeback_pages_written(pages_written);
104803ba3782SJens Axboe 	}
104903ba3782SJens Axboe 
1050839a8e86STejun Heo 	if (!list_empty(&bdi->work_list) ||
1051839a8e86STejun Heo 	    (wb_has_dirty_io(wb) && dirty_writeback_interval))
1052839a8e86STejun Heo 		queue_delayed_work(bdi_wq, &wb->dwork,
1053839a8e86STejun Heo 			msecs_to_jiffies(dirty_writeback_interval * 10));
1054455b2864SDave Chinner 
1055839a8e86STejun Heo 	current->flags &= ~PF_SWAPWRITE;
105603ba3782SJens Axboe }
105703ba3782SJens Axboe 
105803ba3782SJens Axboe /*
105903ba3782SJens Axboe  * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
106003ba3782SJens Axboe  * the whole world.
106103ba3782SJens Axboe  */
10620e175a18SCurt Wohlgemuth void wakeup_flusher_threads(long nr_pages, enum wb_reason reason)
106303ba3782SJens Axboe {
1064b8c2f347SChristoph Hellwig 	struct backing_dev_info *bdi;
1065b8c2f347SChristoph Hellwig 
106647df3ddeSJan Kara 	if (!nr_pages)
106747df3ddeSJan Kara 		nr_pages = get_nr_dirty_pages();
1068b8c2f347SChristoph Hellwig 
1069b8c2f347SChristoph Hellwig 	rcu_read_lock();
1070b8c2f347SChristoph Hellwig 	list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
1071b8c2f347SChristoph Hellwig 		if (!bdi_has_dirty_io(bdi))
1072b8c2f347SChristoph Hellwig 			continue;
10730e175a18SCurt Wohlgemuth 		__bdi_start_writeback(bdi, nr_pages, false, reason);
1074b8c2f347SChristoph Hellwig 	}
1075b8c2f347SChristoph Hellwig 	rcu_read_unlock();
107603ba3782SJens Axboe }
107703ba3782SJens Axboe 
107803ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode)
107903ba3782SJens Axboe {
108003ba3782SJens Axboe 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
108103ba3782SJens Axboe 		struct dentry *dentry;
108203ba3782SJens Axboe 		const char *name = "?";
108303ba3782SJens Axboe 
108403ba3782SJens Axboe 		dentry = d_find_alias(inode);
108503ba3782SJens Axboe 		if (dentry) {
108603ba3782SJens Axboe 			spin_lock(&dentry->d_lock);
108703ba3782SJens Axboe 			name = (const char *) dentry->d_name.name;
108803ba3782SJens Axboe 		}
108903ba3782SJens Axboe 		printk(KERN_DEBUG
109003ba3782SJens Axboe 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
109103ba3782SJens Axboe 		       current->comm, task_pid_nr(current), inode->i_ino,
109203ba3782SJens Axboe 		       name, inode->i_sb->s_id);
109303ba3782SJens Axboe 		if (dentry) {
109403ba3782SJens Axboe 			spin_unlock(&dentry->d_lock);
109503ba3782SJens Axboe 			dput(dentry);
109603ba3782SJens Axboe 		}
109703ba3782SJens Axboe 	}
109803ba3782SJens Axboe }
109903ba3782SJens Axboe 
110003ba3782SJens Axboe /**
110103ba3782SJens Axboe  *	__mark_inode_dirty -	internal function
110203ba3782SJens Axboe  *	@inode: inode to mark
110303ba3782SJens Axboe  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
110403ba3782SJens Axboe  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
110503ba3782SJens Axboe  *  	mark_inode_dirty_sync.
110603ba3782SJens Axboe  *
110703ba3782SJens Axboe  * Put the inode on the super block's dirty list.
110803ba3782SJens Axboe  *
110903ba3782SJens Axboe  * CAREFUL! We mark it dirty unconditionally, but move it onto the
111003ba3782SJens Axboe  * dirty list only if it is hashed or if it refers to a blockdev.
111103ba3782SJens Axboe  * If it was not hashed, it will never be added to the dirty list
111203ba3782SJens Axboe  * even if it is later hashed, as it will have been marked dirty already.
111303ba3782SJens Axboe  *
111403ba3782SJens Axboe  * In short, make sure you hash any inodes _before_ you start marking
111503ba3782SJens Axboe  * them dirty.
111603ba3782SJens Axboe  *
111703ba3782SJens Axboe  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
111803ba3782SJens Axboe  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
111903ba3782SJens Axboe  * the kernel-internal blockdev inode represents the dirtying time of the
112003ba3782SJens Axboe  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
112103ba3782SJens Axboe  * page->mapping->host, so the page-dirtying time is recorded in the internal
112203ba3782SJens Axboe  * blockdev inode.
112303ba3782SJens Axboe  */
112403ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags)
112503ba3782SJens Axboe {
112603ba3782SJens Axboe 	struct super_block *sb = inode->i_sb;
1127253c34e9SArtem Bityutskiy 	struct backing_dev_info *bdi = NULL;
112803ba3782SJens Axboe 
112903ba3782SJens Axboe 	/*
113003ba3782SJens Axboe 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
113103ba3782SJens Axboe 	 * dirty the inode itself
113203ba3782SJens Axboe 	 */
113303ba3782SJens Axboe 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
11349fb0a7daSTejun Heo 		trace_writeback_dirty_inode_start(inode, flags);
11359fb0a7daSTejun Heo 
113603ba3782SJens Axboe 		if (sb->s_op->dirty_inode)
1137aa385729SChristoph Hellwig 			sb->s_op->dirty_inode(inode, flags);
11389fb0a7daSTejun Heo 
11399fb0a7daSTejun Heo 		trace_writeback_dirty_inode(inode, flags);
114003ba3782SJens Axboe 	}
114103ba3782SJens Axboe 
114203ba3782SJens Axboe 	/*
114303ba3782SJens Axboe 	 * make sure that changes are seen by all cpus before we test i_state
114403ba3782SJens Axboe 	 * -- mikulas
114503ba3782SJens Axboe 	 */
114603ba3782SJens Axboe 	smp_mb();
114703ba3782SJens Axboe 
114803ba3782SJens Axboe 	/* avoid the locking if we can */
114903ba3782SJens Axboe 	if ((inode->i_state & flags) == flags)
115003ba3782SJens Axboe 		return;
115103ba3782SJens Axboe 
115203ba3782SJens Axboe 	if (unlikely(block_dump))
115303ba3782SJens Axboe 		block_dump___mark_inode_dirty(inode);
115403ba3782SJens Axboe 
1155250df6edSDave Chinner 	spin_lock(&inode->i_lock);
115603ba3782SJens Axboe 	if ((inode->i_state & flags) != flags) {
115703ba3782SJens Axboe 		const int was_dirty = inode->i_state & I_DIRTY;
115803ba3782SJens Axboe 
115903ba3782SJens Axboe 		inode->i_state |= flags;
116003ba3782SJens Axboe 
116103ba3782SJens Axboe 		/*
116203ba3782SJens Axboe 		 * If the inode is being synced, just update its dirty state.
116303ba3782SJens Axboe 		 * The unlocker will place the inode on the appropriate
116403ba3782SJens Axboe 		 * superblock list, based upon its state.
116503ba3782SJens Axboe 		 */
116603ba3782SJens Axboe 		if (inode->i_state & I_SYNC)
1167250df6edSDave Chinner 			goto out_unlock_inode;
116803ba3782SJens Axboe 
116903ba3782SJens Axboe 		/*
117003ba3782SJens Axboe 		 * Only add valid (hashed) inodes to the superblock's
117103ba3782SJens Axboe 		 * dirty list.  Add blockdev inodes as well.
117203ba3782SJens Axboe 		 */
117303ba3782SJens Axboe 		if (!S_ISBLK(inode->i_mode)) {
11741d3382cbSAl Viro 			if (inode_unhashed(inode))
1175250df6edSDave Chinner 				goto out_unlock_inode;
117603ba3782SJens Axboe 		}
1177a4ffdde6SAl Viro 		if (inode->i_state & I_FREEING)
1178250df6edSDave Chinner 			goto out_unlock_inode;
117903ba3782SJens Axboe 
118003ba3782SJens Axboe 		/*
118103ba3782SJens Axboe 		 * If the inode was already on b_dirty/b_io/b_more_io, don't
118203ba3782SJens Axboe 		 * reposition it (that would break b_dirty time-ordering).
118303ba3782SJens Axboe 		 */
118403ba3782SJens Axboe 		if (!was_dirty) {
1185a66979abSDave Chinner 			bool wakeup_bdi = false;
1186253c34e9SArtem Bityutskiy 			bdi = inode_to_bdi(inode);
1187500b067cSJens Axboe 
1188146d7009SJunxiao Bi 			spin_unlock(&inode->i_lock);
1189146d7009SJunxiao Bi 			spin_lock(&bdi->wb.list_lock);
1190253c34e9SArtem Bityutskiy 			if (bdi_cap_writeback_dirty(bdi)) {
1191253c34e9SArtem Bityutskiy 				WARN(!test_bit(BDI_registered, &bdi->state),
1192253c34e9SArtem Bityutskiy 				     "bdi-%s not registered\n", bdi->name);
1193253c34e9SArtem Bityutskiy 
1194253c34e9SArtem Bityutskiy 				/*
1195253c34e9SArtem Bityutskiy 				 * If this is the first dirty inode for this
1196253c34e9SArtem Bityutskiy 				 * bdi, we have to wake-up the corresponding
1197253c34e9SArtem Bityutskiy 				 * bdi thread to make sure background
1198253c34e9SArtem Bityutskiy 				 * write-back happens later.
1199253c34e9SArtem Bityutskiy 				 */
1200253c34e9SArtem Bityutskiy 				if (!wb_has_dirty_io(&bdi->wb))
1201253c34e9SArtem Bityutskiy 					wakeup_bdi = true;
1202500b067cSJens Axboe 			}
120303ba3782SJens Axboe 
120403ba3782SJens Axboe 			inode->dirtied_when = jiffies;
12057ccf19a8SNick Piggin 			list_move(&inode->i_wb_list, &bdi->wb.b_dirty);
1206f758eeabSChristoph Hellwig 			spin_unlock(&bdi->wb.list_lock);
1207253c34e9SArtem Bityutskiy 
1208253c34e9SArtem Bityutskiy 			if (wakeup_bdi)
12096467716aSArtem Bityutskiy 				bdi_wakeup_thread_delayed(bdi);
1210a66979abSDave Chinner 			return;
1211a66979abSDave Chinner 		}
1212a66979abSDave Chinner 	}
1213a66979abSDave Chinner out_unlock_inode:
1214a66979abSDave Chinner 	spin_unlock(&inode->i_lock);
1215a66979abSDave Chinner 
121603ba3782SJens Axboe }
121703ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty);
121803ba3782SJens Axboe 
1219b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb)
122066f3b8e2SJens Axboe {
122138f21977SNick Piggin 	struct inode *inode, *old_inode = NULL;
122238f21977SNick Piggin 
122303ba3782SJens Axboe 	/*
122403ba3782SJens Axboe 	 * We need to be protected against the filesystem going from
122503ba3782SJens Axboe 	 * r/o to r/w or vice versa.
122603ba3782SJens Axboe 	 */
1227b6e51316SJens Axboe 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
122803ba3782SJens Axboe 
122955fa6091SDave Chinner 	spin_lock(&inode_sb_list_lock);
123066f3b8e2SJens Axboe 
123138f21977SNick Piggin 	/*
123238f21977SNick Piggin 	 * Data integrity sync. Must wait for all pages under writeback,
123338f21977SNick Piggin 	 * because there may have been pages dirtied before our sync
123438f21977SNick Piggin 	 * call, but which had writeout started before we write it out.
123538f21977SNick Piggin 	 * In which case, the inode may not be on the dirty list, but
123638f21977SNick Piggin 	 * we still have to wait for that writeout.
123738f21977SNick Piggin 	 */
1238b6e51316SJens Axboe 	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1239250df6edSDave Chinner 		struct address_space *mapping = inode->i_mapping;
124038f21977SNick Piggin 
1241250df6edSDave Chinner 		spin_lock(&inode->i_lock);
1242250df6edSDave Chinner 		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
1243250df6edSDave Chinner 		    (mapping->nrpages == 0)) {
1244250df6edSDave Chinner 			spin_unlock(&inode->i_lock);
124538f21977SNick Piggin 			continue;
1246250df6edSDave Chinner 		}
124738f21977SNick Piggin 		__iget(inode);
1248250df6edSDave Chinner 		spin_unlock(&inode->i_lock);
124955fa6091SDave Chinner 		spin_unlock(&inode_sb_list_lock);
125055fa6091SDave Chinner 
125138f21977SNick Piggin 		/*
125255fa6091SDave Chinner 		 * We hold a reference to 'inode' so it couldn't have been
125355fa6091SDave Chinner 		 * removed from s_inodes list while we dropped the
125455fa6091SDave Chinner 		 * inode_sb_list_lock.  We cannot iput the inode now as we can
125555fa6091SDave Chinner 		 * be holding the last reference and we cannot iput it under
125655fa6091SDave Chinner 		 * inode_sb_list_lock. So we keep the reference and iput it
125755fa6091SDave Chinner 		 * later.
125838f21977SNick Piggin 		 */
125938f21977SNick Piggin 		iput(old_inode);
126038f21977SNick Piggin 		old_inode = inode;
126138f21977SNick Piggin 
126238f21977SNick Piggin 		filemap_fdatawait(mapping);
126338f21977SNick Piggin 
126438f21977SNick Piggin 		cond_resched();
126538f21977SNick Piggin 
126655fa6091SDave Chinner 		spin_lock(&inode_sb_list_lock);
126738f21977SNick Piggin 	}
126855fa6091SDave Chinner 	spin_unlock(&inode_sb_list_lock);
126938f21977SNick Piggin 	iput(old_inode);
127066f3b8e2SJens Axboe }
12711da177e4SLinus Torvalds 
1272d8a8559cSJens Axboe /**
12733259f8beSChris Mason  * writeback_inodes_sb_nr -	writeback dirty inodes from given super_block
1274d8a8559cSJens Axboe  * @sb: the superblock
12753259f8beSChris Mason  * @nr: the number of pages to write
1276786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work initiated
12771da177e4SLinus Torvalds  *
1278d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
1279d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
12803259f8beSChris Mason  * for IO completion of submitted IO.
12811da177e4SLinus Torvalds  */
12820e175a18SCurt Wohlgemuth void writeback_inodes_sb_nr(struct super_block *sb,
12830e175a18SCurt Wohlgemuth 			    unsigned long nr,
12840e175a18SCurt Wohlgemuth 			    enum wb_reason reason)
12851da177e4SLinus Torvalds {
128683ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
128783ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
12883c4d7165SChristoph Hellwig 		.sb			= sb,
12893c4d7165SChristoph Hellwig 		.sync_mode		= WB_SYNC_NONE,
12906e6938b6SWu Fengguang 		.tagged_writepages	= 1,
129183ba7b07SChristoph Hellwig 		.done			= &done,
12923259f8beSChris Mason 		.nr_pages		= nr,
12930e175a18SCurt Wohlgemuth 		.reason			= reason,
12943c4d7165SChristoph Hellwig 	};
12950e3c9a22SJens Axboe 
12966eedc701SJan Kara 	if (sb->s_bdi == &noop_backing_dev_info)
12976eedc701SJan Kara 		return;
1298cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
129983ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
130083ba7b07SChristoph Hellwig 	wait_for_completion(&done);
13011da177e4SLinus Torvalds }
13023259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr);
13033259f8beSChris Mason 
13043259f8beSChris Mason /**
13053259f8beSChris Mason  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
13063259f8beSChris Mason  * @sb: the superblock
1307786228abSMarcos Paulo de Souza  * @reason: reason why some writeback work was initiated
13083259f8beSChris Mason  *
13093259f8beSChris Mason  * Start writeback on some inodes on this super_block. No guarantees are made
13103259f8beSChris Mason  * on how many (if any) will be written, and this function does not wait
13113259f8beSChris Mason  * for IO completion of submitted IO.
13123259f8beSChris Mason  */
13130e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
13143259f8beSChris Mason {
13150e175a18SCurt Wohlgemuth 	return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
13163259f8beSChris Mason }
1317d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
1318d8a8559cSJens Axboe 
1319d8a8559cSJens Axboe /**
132010ee27a0SMiao Xie  * try_to_writeback_inodes_sb_nr - try to start writeback if none underway
13213259f8beSChris Mason  * @sb: the superblock
13223259f8beSChris Mason  * @nr: the number of pages to write
132310ee27a0SMiao Xie  * @reason: the reason of writeback
13243259f8beSChris Mason  *
132510ee27a0SMiao Xie  * Invoke writeback_inodes_sb_nr if no writeback is currently underway.
13263259f8beSChris Mason  * Returns 1 if writeback was started, 0 if not.
13273259f8beSChris Mason  */
132810ee27a0SMiao Xie int try_to_writeback_inodes_sb_nr(struct super_block *sb,
13290e175a18SCurt Wohlgemuth 				  unsigned long nr,
13300e175a18SCurt Wohlgemuth 				  enum wb_reason reason)
13313259f8beSChris Mason {
133210ee27a0SMiao Xie 	if (writeback_in_progress(sb->s_bdi))
133310ee27a0SMiao Xie 		return 1;
133410ee27a0SMiao Xie 
133510ee27a0SMiao Xie 	if (!down_read_trylock(&sb->s_umount))
133610ee27a0SMiao Xie 		return 0;
133710ee27a0SMiao Xie 
13380e175a18SCurt Wohlgemuth 	writeback_inodes_sb_nr(sb, nr, reason);
13393259f8beSChris Mason 	up_read(&sb->s_umount);
13403259f8beSChris Mason 	return 1;
13413259f8beSChris Mason }
134210ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb_nr);
134310ee27a0SMiao Xie 
134410ee27a0SMiao Xie /**
134510ee27a0SMiao Xie  * try_to_writeback_inodes_sb - try to start writeback if none underway
134610ee27a0SMiao Xie  * @sb: the superblock
134710ee27a0SMiao Xie  * @reason: reason why some writeback work was initiated
134810ee27a0SMiao Xie  *
134910ee27a0SMiao Xie  * Implement by try_to_writeback_inodes_sb_nr()
135010ee27a0SMiao Xie  * Returns 1 if writeback was started, 0 if not.
135110ee27a0SMiao Xie  */
135210ee27a0SMiao Xie int try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
135310ee27a0SMiao Xie {
135410ee27a0SMiao Xie 	return try_to_writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
135510ee27a0SMiao Xie }
135610ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb);
13573259f8beSChris Mason 
13583259f8beSChris Mason /**
1359d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
1360d8a8559cSJens Axboe  * @sb:			the superblock
1361c4a391b5SJan Kara  * @older_than_this:	timestamp
1362d8a8559cSJens Axboe  *
1363d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
1364c4a391b5SJan Kara  * superblock that has been dirtied before given timestamp.
1365d8a8559cSJens Axboe  */
1366c4a391b5SJan Kara void sync_inodes_sb(struct super_block *sb, unsigned long older_than_this)
1367d8a8559cSJens Axboe {
136883ba7b07SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(done);
136983ba7b07SChristoph Hellwig 	struct wb_writeback_work work = {
13703c4d7165SChristoph Hellwig 		.sb		= sb,
13713c4d7165SChristoph Hellwig 		.sync_mode	= WB_SYNC_ALL,
13723c4d7165SChristoph Hellwig 		.nr_pages	= LONG_MAX,
1373c4a391b5SJan Kara 		.older_than_this = older_than_this,
1374c4a391b5SJan Kara 		.older_than_this_is_set = 1,
13753c4d7165SChristoph Hellwig 		.range_cyclic	= 0,
137683ba7b07SChristoph Hellwig 		.done		= &done,
13770e175a18SCurt Wohlgemuth 		.reason		= WB_REASON_SYNC,
13787747bd4bSDave Chinner 		.for_sync	= 1,
13793c4d7165SChristoph Hellwig 	};
13803c4d7165SChristoph Hellwig 
13816eedc701SJan Kara 	/* Nothing to do? */
13826eedc701SJan Kara 	if (sb->s_bdi == &noop_backing_dev_info)
13836eedc701SJan Kara 		return;
1384cf37e972SChristoph Hellwig 	WARN_ON(!rwsem_is_locked(&sb->s_umount));
1385cf37e972SChristoph Hellwig 
138683ba7b07SChristoph Hellwig 	bdi_queue_work(sb->s_bdi, &work);
138783ba7b07SChristoph Hellwig 	wait_for_completion(&done);
138883ba7b07SChristoph Hellwig 
1389b6e51316SJens Axboe 	wait_sb_inodes(sb);
1390d8a8559cSJens Axboe }
1391d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds /**
13941da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
13951da177e4SLinus Torvalds  * @inode: inode to write to disk
13961da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
13971da177e4SLinus Torvalds  *
13987f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
13997f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
14007f04c26dSAndrea Arcangeli  *
14017f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
14021da177e4SLinus Torvalds  */
14031da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
14041da177e4SLinus Torvalds {
1405f758eeabSChristoph Hellwig 	struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
14061da177e4SLinus Torvalds 	struct writeback_control wbc = {
14071da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
140818914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1409111ebb6eSOGAWA Hirofumi 		.range_start = 0,
1410111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
14111da177e4SLinus Torvalds 	};
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
141449364ce2SAndrew Morton 		wbc.nr_to_write = 0;
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 	might_sleep();
14174f8ad655SJan Kara 	return writeback_single_inode(inode, wb, &wbc);
14181da177e4SLinus Torvalds }
14191da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds /**
14221da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
14231da177e4SLinus Torvalds  * @inode: the inode to sync
14241da177e4SLinus Torvalds  * @wbc: controls the writeback mode
14251da177e4SLinus Torvalds  *
14261da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
14271da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
14281da177e4SLinus Torvalds  * update inode->i_state.
14291da177e4SLinus Torvalds  *
14301da177e4SLinus Torvalds  * The caller must have a ref on the inode.
14311da177e4SLinus Torvalds  */
14321da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
14331da177e4SLinus Torvalds {
14344f8ad655SJan Kara 	return writeback_single_inode(inode, &inode_to_bdi(inode)->wb, wbc);
14351da177e4SLinus Torvalds }
14361da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
1437c3765016SChristoph Hellwig 
1438c3765016SChristoph Hellwig /**
1439c691b9d9SAndrew Morton  * sync_inode_metadata - write an inode to disk
1440c3765016SChristoph Hellwig  * @inode: the inode to sync
1441c3765016SChristoph Hellwig  * @wait: wait for I/O to complete.
1442c3765016SChristoph Hellwig  *
1443c691b9d9SAndrew Morton  * Write an inode to disk and adjust its dirty state after completion.
1444c3765016SChristoph Hellwig  *
1445c3765016SChristoph Hellwig  * Note: only writes the actual inode, no associated data or other metadata.
1446c3765016SChristoph Hellwig  */
1447c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait)
1448c3765016SChristoph Hellwig {
1449c3765016SChristoph Hellwig 	struct writeback_control wbc = {
1450c3765016SChristoph Hellwig 		.sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
1451c3765016SChristoph Hellwig 		.nr_to_write = 0, /* metadata-only */
1452c3765016SChristoph Hellwig 	};
1453c3765016SChristoph Hellwig 
1454c3765016SChristoph Hellwig 	return sync_inode(inode, &wbc);
1455c3765016SChristoph Hellwig }
1456c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata);
1457