xref: /openbmc/linux/fs/fs-writeback.c (revision d8a8559c)
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>
17f5ff8422SJens Axboe #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/spinlock.h>
191da177e4SLinus Torvalds #include <linux/sched.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/mm.h>
221da177e4SLinus Torvalds #include <linux/writeback.h>
231da177e4SLinus Torvalds #include <linux/blkdev.h>
241da177e4SLinus Torvalds #include <linux/backing-dev.h>
251da177e4SLinus Torvalds #include <linux/buffer_head.h>
2607f3f05cSDavid Howells #include "internal.h"
271da177e4SLinus Torvalds 
28f11b00f3SAdrian Bunk 
29f11b00f3SAdrian Bunk /**
30f11b00f3SAdrian Bunk  * writeback_acquire - attempt to get exclusive writeback access to a device
31f11b00f3SAdrian Bunk  * @bdi: the device's backing_dev_info structure
32f11b00f3SAdrian Bunk  *
33f11b00f3SAdrian Bunk  * It is a waste of resources to have more than one pdflush thread blocked on
34f11b00f3SAdrian Bunk  * a single request queue.  Exclusion at the request_queue level is obtained
35f11b00f3SAdrian Bunk  * via a flag in the request_queue's backing_dev_info.state.
36f11b00f3SAdrian Bunk  *
37f11b00f3SAdrian Bunk  * Non-request_queue-backed address_spaces will share default_backing_dev_info,
38f11b00f3SAdrian Bunk  * unless they implement their own.  Which is somewhat inefficient, as this
39f11b00f3SAdrian Bunk  * may prevent concurrent writeback against multiple devices.
40f11b00f3SAdrian Bunk  */
41f11b00f3SAdrian Bunk static int writeback_acquire(struct backing_dev_info *bdi)
42f11b00f3SAdrian Bunk {
43f11b00f3SAdrian Bunk 	return !test_and_set_bit(BDI_pdflush, &bdi->state);
44f11b00f3SAdrian Bunk }
45f11b00f3SAdrian Bunk 
46f11b00f3SAdrian Bunk /**
47f11b00f3SAdrian Bunk  * writeback_in_progress - determine whether there is writeback in progress
48f11b00f3SAdrian Bunk  * @bdi: the device's backing_dev_info structure.
49f11b00f3SAdrian Bunk  *
50f11b00f3SAdrian Bunk  * Determine whether there is writeback in progress against a backing device.
51f11b00f3SAdrian Bunk  */
52f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi)
53f11b00f3SAdrian Bunk {
54f11b00f3SAdrian Bunk 	return test_bit(BDI_pdflush, &bdi->state);
55f11b00f3SAdrian Bunk }
56f11b00f3SAdrian Bunk 
57f11b00f3SAdrian Bunk /**
58f11b00f3SAdrian Bunk  * writeback_release - relinquish exclusive writeback access against a device.
59f11b00f3SAdrian Bunk  * @bdi: the device's backing_dev_info structure
60f11b00f3SAdrian Bunk  */
61f11b00f3SAdrian Bunk static void writeback_release(struct backing_dev_info *bdi)
62f11b00f3SAdrian Bunk {
63f11b00f3SAdrian Bunk 	BUG_ON(!writeback_in_progress(bdi));
64f11b00f3SAdrian Bunk 	clear_bit(BDI_pdflush, &bdi->state);
65f11b00f3SAdrian Bunk }
66f11b00f3SAdrian Bunk 
674195f73dSNick Piggin static noinline void block_dump___mark_inode_dirty(struct inode *inode)
684195f73dSNick Piggin {
694195f73dSNick Piggin 	if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
704195f73dSNick Piggin 		struct dentry *dentry;
714195f73dSNick Piggin 		const char *name = "?";
724195f73dSNick Piggin 
734195f73dSNick Piggin 		dentry = d_find_alias(inode);
744195f73dSNick Piggin 		if (dentry) {
754195f73dSNick Piggin 			spin_lock(&dentry->d_lock);
764195f73dSNick Piggin 			name = (const char *) dentry->d_name.name;
774195f73dSNick Piggin 		}
784195f73dSNick Piggin 		printk(KERN_DEBUG
794195f73dSNick Piggin 		       "%s(%d): dirtied inode %lu (%s) on %s\n",
804195f73dSNick Piggin 		       current->comm, task_pid_nr(current), inode->i_ino,
814195f73dSNick Piggin 		       name, inode->i_sb->s_id);
824195f73dSNick Piggin 		if (dentry) {
834195f73dSNick Piggin 			spin_unlock(&dentry->d_lock);
844195f73dSNick Piggin 			dput(dentry);
854195f73dSNick Piggin 		}
864195f73dSNick Piggin 	}
874195f73dSNick Piggin }
884195f73dSNick Piggin 
891da177e4SLinus Torvalds /**
901da177e4SLinus Torvalds  *	__mark_inode_dirty -	internal function
911da177e4SLinus Torvalds  *	@inode: inode to mark
921da177e4SLinus Torvalds  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
931da177e4SLinus Torvalds  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
941da177e4SLinus Torvalds  *  	mark_inode_dirty_sync.
951da177e4SLinus Torvalds  *
961da177e4SLinus Torvalds  * Put the inode on the super block's dirty list.
971da177e4SLinus Torvalds  *
981da177e4SLinus Torvalds  * CAREFUL! We mark it dirty unconditionally, but move it onto the
991da177e4SLinus Torvalds  * dirty list only if it is hashed or if it refers to a blockdev.
1001da177e4SLinus Torvalds  * If it was not hashed, it will never be added to the dirty list
1011da177e4SLinus Torvalds  * even if it is later hashed, as it will have been marked dirty already.
1021da177e4SLinus Torvalds  *
1031da177e4SLinus Torvalds  * In short, make sure you hash any inodes _before_ you start marking
1041da177e4SLinus Torvalds  * them dirty.
1051da177e4SLinus Torvalds  *
1061da177e4SLinus Torvalds  * This function *must* be atomic for the I_DIRTY_PAGES case -
1071da177e4SLinus Torvalds  * set_page_dirty() is called under spinlock in several places.
1081da177e4SLinus Torvalds  *
1091da177e4SLinus Torvalds  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
1101da177e4SLinus Torvalds  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
1111da177e4SLinus Torvalds  * the kernel-internal blockdev inode represents the dirtying time of the
1121da177e4SLinus Torvalds  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
1131da177e4SLinus Torvalds  * page->mapping->host, so the page-dirtying time is recorded in the internal
1141da177e4SLinus Torvalds  * blockdev inode.
1151da177e4SLinus Torvalds  */
1161da177e4SLinus Torvalds void __mark_inode_dirty(struct inode *inode, int flags)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	/*
1211da177e4SLinus Torvalds 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
1221da177e4SLinus Torvalds 	 * dirty the inode itself
1231da177e4SLinus Torvalds 	 */
1241da177e4SLinus Torvalds 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
1251da177e4SLinus Torvalds 		if (sb->s_op->dirty_inode)
1261da177e4SLinus Torvalds 			sb->s_op->dirty_inode(inode);
1271da177e4SLinus Torvalds 	}
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	/*
1301da177e4SLinus Torvalds 	 * make sure that changes are seen by all cpus before we test i_state
1311da177e4SLinus Torvalds 	 * -- mikulas
1321da177e4SLinus Torvalds 	 */
1331da177e4SLinus Torvalds 	smp_mb();
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	/* avoid the locking if we can */
1361da177e4SLinus Torvalds 	if ((inode->i_state & flags) == flags)
1371da177e4SLinus Torvalds 		return;
1381da177e4SLinus Torvalds 
1394195f73dSNick Piggin 	if (unlikely(block_dump))
1404195f73dSNick Piggin 		block_dump___mark_inode_dirty(inode);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	spin_lock(&inode_lock);
1431da177e4SLinus Torvalds 	if ((inode->i_state & flags) != flags) {
1441da177e4SLinus Torvalds 		const int was_dirty = inode->i_state & I_DIRTY;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 		inode->i_state |= flags;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 		/*
1491c0eeaf5SJoern Engel 		 * If the inode is being synced, just update its dirty state.
1501da177e4SLinus Torvalds 		 * The unlocker will place the inode on the appropriate
1511da177e4SLinus Torvalds 		 * superblock list, based upon its state.
1521da177e4SLinus Torvalds 		 */
1531c0eeaf5SJoern Engel 		if (inode->i_state & I_SYNC)
1541da177e4SLinus Torvalds 			goto out;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 		/*
1571da177e4SLinus Torvalds 		 * Only add valid (hashed) inodes to the superblock's
1581da177e4SLinus Torvalds 		 * dirty list.  Add blockdev inodes as well.
1591da177e4SLinus Torvalds 		 */
1601da177e4SLinus Torvalds 		if (!S_ISBLK(inode->i_mode)) {
1611da177e4SLinus Torvalds 			if (hlist_unhashed(&inode->i_hash))
1621da177e4SLinus Torvalds 				goto out;
1631da177e4SLinus Torvalds 		}
1641da177e4SLinus Torvalds 		if (inode->i_state & (I_FREEING|I_CLEAR))
1651da177e4SLinus Torvalds 			goto out;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 		/*
1682c136579SFengguang Wu 		 * If the inode was already on s_dirty/s_io/s_more_io, don't
1691da177e4SLinus Torvalds 		 * reposition it (that would break s_dirty time-ordering).
1701da177e4SLinus Torvalds 		 */
1711da177e4SLinus Torvalds 		if (!was_dirty) {
1721da177e4SLinus Torvalds 			inode->dirtied_when = jiffies;
1731da177e4SLinus Torvalds 			list_move(&inode->i_list, &sb->s_dirty);
1741da177e4SLinus Torvalds 		}
1751da177e4SLinus Torvalds 	}
1761da177e4SLinus Torvalds out:
1771da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds EXPORT_SYMBOL(__mark_inode_dirty);
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds static int write_inode(struct inode *inode, int sync)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
1851da177e4SLinus Torvalds 		return inode->i_sb->s_op->write_inode(inode, sync);
1861da177e4SLinus Torvalds 	return 0;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds /*
1906610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
1916610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
1926610a0bcSAndrew Morton  *
1936610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
1946610a0bcSAndrew Morton  * already the most-recently-dirtied inode on the s_dirty list.  If that is
1956610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
1966610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
1976610a0bcSAndrew Morton  */
1986610a0bcSAndrew Morton static void redirty_tail(struct inode *inode)
1996610a0bcSAndrew Morton {
2006610a0bcSAndrew Morton 	struct super_block *sb = inode->i_sb;
2016610a0bcSAndrew Morton 
2026610a0bcSAndrew Morton 	if (!list_empty(&sb->s_dirty)) {
2036610a0bcSAndrew Morton 		struct inode *tail_inode;
2046610a0bcSAndrew Morton 
2056610a0bcSAndrew Morton 		tail_inode = list_entry(sb->s_dirty.next, struct inode, i_list);
206d2caa3c5SJeff Layton 		if (time_before(inode->dirtied_when,
2076610a0bcSAndrew Morton 				tail_inode->dirtied_when))
2086610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
2096610a0bcSAndrew Morton 	}
2106610a0bcSAndrew Morton 	list_move(&inode->i_list, &sb->s_dirty);
2116610a0bcSAndrew Morton }
2126610a0bcSAndrew Morton 
2136610a0bcSAndrew Morton /*
2140e0f4fc2SKen Chen  * requeue inode for re-scanning after sb->s_io list is exhausted.
215c986d1e2SAndrew Morton  */
2160e0f4fc2SKen Chen static void requeue_io(struct inode *inode)
217c986d1e2SAndrew Morton {
2180e0f4fc2SKen Chen 	list_move(&inode->i_list, &inode->i_sb->s_more_io);
219c986d1e2SAndrew Morton }
220c986d1e2SAndrew Morton 
2211c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
2221c0eeaf5SJoern Engel {
2231c0eeaf5SJoern Engel 	/*
2241c0eeaf5SJoern Engel 	 * Prevent speculative execution through spin_unlock(&inode_lock);
2251c0eeaf5SJoern Engel 	 */
2261c0eeaf5SJoern Engel 	smp_mb();
2271c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
2281c0eeaf5SJoern Engel }
2291c0eeaf5SJoern Engel 
230d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
231d2caa3c5SJeff Layton {
232d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
233d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
234d2caa3c5SJeff Layton 	/*
235d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
236d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
237d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
238d2caa3c5SJeff Layton 	 * from permanently stopping the whole pdflush writeback.
239d2caa3c5SJeff Layton 	 */
240d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
241d2caa3c5SJeff Layton #endif
242d2caa3c5SJeff Layton 	return ret;
243d2caa3c5SJeff Layton }
244d2caa3c5SJeff Layton 
245c986d1e2SAndrew Morton /*
2462c136579SFengguang Wu  * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
2472c136579SFengguang Wu  */
2482c136579SFengguang Wu static void move_expired_inodes(struct list_head *delaying_queue,
2492c136579SFengguang Wu 			       struct list_head *dispatch_queue,
2502c136579SFengguang Wu 				unsigned long *older_than_this)
2512c136579SFengguang Wu {
2522c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
2532c136579SFengguang Wu 		struct inode *inode = list_entry(delaying_queue->prev,
2542c136579SFengguang Wu 						struct inode, i_list);
2552c136579SFengguang Wu 		if (older_than_this &&
256d2caa3c5SJeff Layton 		    inode_dirtied_after(inode, *older_than_this))
2572c136579SFengguang Wu 			break;
2582c136579SFengguang Wu 		list_move(&inode->i_list, dispatch_queue);
2592c136579SFengguang Wu 	}
2602c136579SFengguang Wu }
2612c136579SFengguang Wu 
2622c136579SFengguang Wu /*
2632c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
2642c136579SFengguang Wu  */
2652c136579SFengguang Wu static void queue_io(struct super_block *sb,
2662c136579SFengguang Wu 				unsigned long *older_than_this)
2672c136579SFengguang Wu {
2682c136579SFengguang Wu 	list_splice_init(&sb->s_more_io, sb->s_io.prev);
2692c136579SFengguang Wu 	move_expired_inodes(&sb->s_dirty, &sb->s_io, older_than_this);
2702c136579SFengguang Wu }
2712c136579SFengguang Wu 
27208d8e974SFengguang Wu int sb_has_dirty_inodes(struct super_block *sb)
27308d8e974SFengguang Wu {
27408d8e974SFengguang Wu 	return !list_empty(&sb->s_dirty) ||
27508d8e974SFengguang Wu 	       !list_empty(&sb->s_io) ||
27608d8e974SFengguang Wu 	       !list_empty(&sb->s_more_io);
27708d8e974SFengguang Wu }
27808d8e974SFengguang Wu EXPORT_SYMBOL(sb_has_dirty_inodes);
27908d8e974SFengguang Wu 
2802c136579SFengguang Wu /*
28101c03194SChristoph Hellwig  * Wait for writeback on an inode to complete.
28201c03194SChristoph Hellwig  */
28301c03194SChristoph Hellwig static void inode_wait_for_writeback(struct inode *inode)
28401c03194SChristoph Hellwig {
28501c03194SChristoph Hellwig 	DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
28601c03194SChristoph Hellwig 	wait_queue_head_t *wqh;
28701c03194SChristoph Hellwig 
28801c03194SChristoph Hellwig 	wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
28901c03194SChristoph Hellwig 	do {
29001c03194SChristoph Hellwig 		spin_unlock(&inode_lock);
29101c03194SChristoph Hellwig 		__wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
29201c03194SChristoph Hellwig 		spin_lock(&inode_lock);
29301c03194SChristoph Hellwig 	} while (inode->i_state & I_SYNC);
29401c03194SChristoph Hellwig }
29501c03194SChristoph Hellwig 
29601c03194SChristoph Hellwig /*
29701c03194SChristoph Hellwig  * Write out an inode's dirty pages.  Called under inode_lock.  Either the
29801c03194SChristoph Hellwig  * caller has ref on the inode (either via __iget or via syscall against an fd)
29901c03194SChristoph Hellwig  * or the inode has I_WILL_FREE set (via generic_forget_inode)
30001c03194SChristoph Hellwig  *
3011da177e4SLinus Torvalds  * If `wait' is set, wait on the writeout.
3021da177e4SLinus Torvalds  *
3031da177e4SLinus Torvalds  * The whole writeout design is quite complex and fragile.  We want to avoid
3041da177e4SLinus Torvalds  * starvation of particular inodes when others are being redirtied, prevent
3051da177e4SLinus Torvalds  * livelocks, etc.
3061da177e4SLinus Torvalds  *
3071da177e4SLinus Torvalds  * Called under inode_lock.
3081da177e4SLinus Torvalds  */
3091da177e4SLinus Torvalds static int
31001c03194SChristoph Hellwig writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
3131da177e4SLinus Torvalds 	int wait = wbc->sync_mode == WB_SYNC_ALL;
31401c03194SChristoph Hellwig 	unsigned dirty;
3151da177e4SLinus Torvalds 	int ret;
3161da177e4SLinus Torvalds 
31701c03194SChristoph Hellwig 	if (!atomic_read(&inode->i_count))
31801c03194SChristoph Hellwig 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
31901c03194SChristoph Hellwig 	else
32001c03194SChristoph Hellwig 		WARN_ON(inode->i_state & I_WILL_FREE);
32101c03194SChristoph Hellwig 
32201c03194SChristoph Hellwig 	if (inode->i_state & I_SYNC) {
32301c03194SChristoph Hellwig 		/*
32401c03194SChristoph Hellwig 		 * If this inode is locked for writeback and we are not doing
32501c03194SChristoph Hellwig 		 * writeback-for-data-integrity, move it to s_more_io so that
32601c03194SChristoph Hellwig 		 * writeback can proceed with the other inodes on s_io.
32701c03194SChristoph Hellwig 		 *
32801c03194SChristoph Hellwig 		 * We'll have another go at writing back this inode when we
32901c03194SChristoph Hellwig 		 * completed a full scan of s_io.
33001c03194SChristoph Hellwig 		 */
33101c03194SChristoph Hellwig 		if (!wait) {
33201c03194SChristoph Hellwig 			requeue_io(inode);
33301c03194SChristoph Hellwig 			return 0;
33401c03194SChristoph Hellwig 		}
33501c03194SChristoph Hellwig 
33601c03194SChristoph Hellwig 		/*
33701c03194SChristoph Hellwig 		 * It's a data-integrity sync.  We must wait.
33801c03194SChristoph Hellwig 		 */
33901c03194SChristoph Hellwig 		inode_wait_for_writeback(inode);
34001c03194SChristoph Hellwig 	}
34101c03194SChristoph Hellwig 
3421c0eeaf5SJoern Engel 	BUG_ON(inode->i_state & I_SYNC);
3431da177e4SLinus Torvalds 
3441c0eeaf5SJoern Engel 	/* Set I_SYNC, reset I_DIRTY */
3451da177e4SLinus Torvalds 	dirty = inode->i_state & I_DIRTY;
3461c0eeaf5SJoern Engel 	inode->i_state |= I_SYNC;
3471da177e4SLinus Torvalds 	inode->i_state &= ~I_DIRTY;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	/* Don't write the inode if only I_DIRTY_PAGES was set */
3541da177e4SLinus Torvalds 	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
3551da177e4SLinus Torvalds 		int err = write_inode(inode, wait);
3561da177e4SLinus Torvalds 		if (ret == 0)
3571da177e4SLinus Torvalds 			ret = err;
3581da177e4SLinus Torvalds 	}
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	if (wait) {
3611da177e4SLinus Torvalds 		int err = filemap_fdatawait(mapping);
3621da177e4SLinus Torvalds 		if (ret == 0)
3631da177e4SLinus Torvalds 			ret = err;
3641da177e4SLinus Torvalds 	}
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	spin_lock(&inode_lock);
3671c0eeaf5SJoern Engel 	inode->i_state &= ~I_SYNC;
36884a89245SWu Fengguang 	if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
3691da177e4SLinus Torvalds 		if (!(inode->i_state & I_DIRTY) &&
3701da177e4SLinus Torvalds 		    mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
3711da177e4SLinus Torvalds 			/*
3721da177e4SLinus Torvalds 			 * We didn't write back all the pages.  nfs_writepages()
3731da177e4SLinus Torvalds 			 * sometimes bales out without doing anything. Redirty
3742c136579SFengguang Wu 			 * the inode; Move it from s_io onto s_more_io/s_dirty.
3751b43ef91SAndrew Morton 			 */
3761b43ef91SAndrew Morton 			/*
3771b43ef91SAndrew Morton 			 * akpm: if the caller was the kupdate function we put
3781b43ef91SAndrew Morton 			 * this inode at the head of s_dirty so it gets first
3791b43ef91SAndrew Morton 			 * consideration.  Otherwise, move it to the tail, for
3801b43ef91SAndrew Morton 			 * the reasons described there.  I'm not really sure
3811b43ef91SAndrew Morton 			 * how much sense this makes.  Presumably I had a good
3821b43ef91SAndrew Morton 			 * reasons for doing it this way, and I'd rather not
3831b43ef91SAndrew Morton 			 * muck with it at present.
3841da177e4SLinus Torvalds 			 */
3851da177e4SLinus Torvalds 			if (wbc->for_kupdate) {
3861da177e4SLinus Torvalds 				/*
3872c136579SFengguang Wu 				 * For the kupdate function we move the inode
3882c136579SFengguang Wu 				 * to s_more_io so it will get more writeout as
3892c136579SFengguang Wu 				 * soon as the queue becomes uncongested.
3901da177e4SLinus Torvalds 				 */
3911da177e4SLinus Torvalds 				inode->i_state |= I_DIRTY_PAGES;
3928bc3be27SFengguang Wu 				if (wbc->nr_to_write <= 0) {
3938bc3be27SFengguang Wu 					/*
3948bc3be27SFengguang Wu 					 * slice used up: queue for next turn
3958bc3be27SFengguang Wu 					 */
3960e0f4fc2SKen Chen 					requeue_io(inode);
3971da177e4SLinus Torvalds 				} else {
3981da177e4SLinus Torvalds 					/*
3998bc3be27SFengguang Wu 					 * somehow blocked: retry later
4008bc3be27SFengguang Wu 					 */
4018bc3be27SFengguang Wu 					redirty_tail(inode);
4028bc3be27SFengguang Wu 				}
4038bc3be27SFengguang Wu 			} else {
4048bc3be27SFengguang Wu 				/*
4051da177e4SLinus Torvalds 				 * Otherwise fully redirty the inode so that
4061da177e4SLinus Torvalds 				 * other inodes on this superblock will get some
4071da177e4SLinus Torvalds 				 * writeout.  Otherwise heavy writing to one
4081da177e4SLinus Torvalds 				 * file would indefinitely suspend writeout of
4091da177e4SLinus Torvalds 				 * all the other files.
4101da177e4SLinus Torvalds 				 */
4111da177e4SLinus Torvalds 				inode->i_state |= I_DIRTY_PAGES;
4121b43ef91SAndrew Morton 				redirty_tail(inode);
4131da177e4SLinus Torvalds 			}
4141da177e4SLinus Torvalds 		} else if (inode->i_state & I_DIRTY) {
4151da177e4SLinus Torvalds 			/*
4161da177e4SLinus Torvalds 			 * Someone redirtied the inode while were writing back
4171da177e4SLinus Torvalds 			 * the pages.
4181da177e4SLinus Torvalds 			 */
4196610a0bcSAndrew Morton 			redirty_tail(inode);
4201da177e4SLinus Torvalds 		} else if (atomic_read(&inode->i_count)) {
4211da177e4SLinus Torvalds 			/*
4221da177e4SLinus Torvalds 			 * The inode is clean, inuse
4231da177e4SLinus Torvalds 			 */
4241da177e4SLinus Torvalds 			list_move(&inode->i_list, &inode_in_use);
4251da177e4SLinus Torvalds 		} else {
4261da177e4SLinus Torvalds 			/*
4271da177e4SLinus Torvalds 			 * The inode is clean, unused
4281da177e4SLinus Torvalds 			 */
4291da177e4SLinus Torvalds 			list_move(&inode->i_list, &inode_unused);
4301da177e4SLinus Torvalds 		}
4311da177e4SLinus Torvalds 	}
4321c0eeaf5SJoern Engel 	inode_sync_complete(inode);
4331da177e4SLinus Torvalds 	return ret;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds /*
4371da177e4SLinus Torvalds  * Write out a superblock's list of dirty inodes.  A wait will be performed
4381da177e4SLinus Torvalds  * upon no inodes, all inodes or the final one, depending upon sync_mode.
4391da177e4SLinus Torvalds  *
4401da177e4SLinus Torvalds  * If older_than_this is non-NULL, then only write out inodes which
4411da177e4SLinus Torvalds  * had their first dirtying at a time earlier than *older_than_this.
4421da177e4SLinus Torvalds  *
4433e3cb64fSMasatake YAMATO  * If we're a pdflush thread, then implement pdflush collision avoidance
4441da177e4SLinus Torvalds  * against the entire list.
4451da177e4SLinus Torvalds  *
4461da177e4SLinus Torvalds  * If `bdi' is non-zero then we're being asked to writeback a specific queue.
4471da177e4SLinus Torvalds  * This function assumes that the blockdev superblock's inodes are backed by
4481da177e4SLinus Torvalds  * a variety of queues, so all inodes are searched.  For other superblocks,
4491da177e4SLinus Torvalds  * assume that all inodes are backed by the same queue.
4501da177e4SLinus Torvalds  *
4511da177e4SLinus Torvalds  * FIXME: this linear search could get expensive with many fileystems.  But
4521da177e4SLinus Torvalds  * how to fix?  We need to go from an address_space to all inodes which share
4531da177e4SLinus Torvalds  * a queue with that address_space.  (Easy: have a global "dirty superblocks"
4541da177e4SLinus Torvalds  * list).
4551da177e4SLinus Torvalds  *
4561da177e4SLinus Torvalds  * The inodes to be written are parked on sb->s_io.  They are moved back onto
4571da177e4SLinus Torvalds  * sb->s_dirty as they are selected for writing.  This way, none can be missed
4581da177e4SLinus Torvalds  * on the writer throttling path, and we get decent balancing between many
4591c0eeaf5SJoern Engel  * throttled threads: we don't want them all piling up on inode_sync_wait.
4601da177e4SLinus Torvalds  */
461d8a8559cSJens Axboe static void generic_sync_sb_inodes(struct super_block *sb,
4624ee6afd3SArtem Bityutskiy 				   struct writeback_control *wbc)
4631da177e4SLinus Torvalds {
4641da177e4SLinus Torvalds 	const unsigned long start = jiffies;	/* livelock avoidance */
46538f21977SNick Piggin 	int sync = wbc->sync_mode == WB_SYNC_ALL;
4661da177e4SLinus Torvalds 
467ae8547b0SHans Reiser 	spin_lock(&inode_lock);
4681da177e4SLinus Torvalds 	if (!wbc->for_kupdate || list_empty(&sb->s_io))
4692c136579SFengguang Wu 		queue_io(sb, wbc->older_than_this);
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	while (!list_empty(&sb->s_io)) {
4721da177e4SLinus Torvalds 		struct inode *inode = list_entry(sb->s_io.prev,
4731da177e4SLinus Torvalds 						struct inode, i_list);
4741da177e4SLinus Torvalds 		struct address_space *mapping = inode->i_mapping;
4751da177e4SLinus Torvalds 		struct backing_dev_info *bdi = mapping->backing_dev_info;
4761da177e4SLinus Torvalds 		long pages_skipped;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 		if (!bdi_cap_writeback_dirty(bdi)) {
4799852a0e7SAndrew Morton 			redirty_tail(inode);
4807b0de42dSDavid Howells 			if (sb_is_blkdev_sb(sb)) {
4811da177e4SLinus Torvalds 				/*
4821da177e4SLinus Torvalds 				 * Dirty memory-backed blockdev: the ramdisk
4831da177e4SLinus Torvalds 				 * driver does this.  Skip just this inode
4841da177e4SLinus Torvalds 				 */
4851da177e4SLinus Torvalds 				continue;
4861da177e4SLinus Torvalds 			}
4871da177e4SLinus Torvalds 			/*
4881da177e4SLinus Torvalds 			 * Dirty memory-backed inode against a filesystem other
4891da177e4SLinus Torvalds 			 * than the kernel-internal bdev filesystem.  Skip the
4901da177e4SLinus Torvalds 			 * entire superblock.
4911da177e4SLinus Torvalds 			 */
4921da177e4SLinus Torvalds 			break;
4931da177e4SLinus Torvalds 		}
4941da177e4SLinus Torvalds 
49584a89245SWu Fengguang 		if (inode->i_state & (I_NEW | I_WILL_FREE)) {
4967ef0d737SNick Piggin 			requeue_io(inode);
4977ef0d737SNick Piggin 			continue;
4987ef0d737SNick Piggin 		}
4997ef0d737SNick Piggin 
5001da177e4SLinus Torvalds 		if (wbc->nonblocking && bdi_write_congested(bdi)) {
5011da177e4SLinus Torvalds 			wbc->encountered_congestion = 1;
5027b0de42dSDavid Howells 			if (!sb_is_blkdev_sb(sb))
5031da177e4SLinus Torvalds 				break;		/* Skip a congested fs */
5040e0f4fc2SKen Chen 			requeue_io(inode);
5051da177e4SLinus Torvalds 			continue;		/* Skip a congested blockdev */
5061da177e4SLinus Torvalds 		}
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 		if (wbc->bdi && bdi != wbc->bdi) {
5097b0de42dSDavid Howells 			if (!sb_is_blkdev_sb(sb))
5101da177e4SLinus Torvalds 				break;		/* fs has the wrong queue */
5110e0f4fc2SKen Chen 			requeue_io(inode);
5121da177e4SLinus Torvalds 			continue;		/* blockdev has wrong queue */
5131da177e4SLinus Torvalds 		}
5141da177e4SLinus Torvalds 
515d2caa3c5SJeff Layton 		/*
516d2caa3c5SJeff Layton 		 * Was this inode dirtied after sync_sb_inodes was called?
517d2caa3c5SJeff Layton 		 * This keeps sync from extra jobs and livelock.
518d2caa3c5SJeff Layton 		 */
519d2caa3c5SJeff Layton 		if (inode_dirtied_after(inode, start))
5201da177e4SLinus Torvalds 			break;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 		/* Is another pdflush already flushing this queue? */
5231da177e4SLinus Torvalds 		if (current_is_pdflush() && !writeback_acquire(bdi))
5241da177e4SLinus Torvalds 			break;
5251da177e4SLinus Torvalds 
52684a89245SWu Fengguang 		BUG_ON(inode->i_state & (I_FREEING | I_CLEAR));
5271da177e4SLinus Torvalds 		__iget(inode);
5281da177e4SLinus Torvalds 		pages_skipped = wbc->pages_skipped;
52901c03194SChristoph Hellwig 		writeback_single_inode(inode, wbc);
5301da177e4SLinus Torvalds 		if (current_is_pdflush())
5311da177e4SLinus Torvalds 			writeback_release(bdi);
5321da177e4SLinus Torvalds 		if (wbc->pages_skipped != pages_skipped) {
5331da177e4SLinus Torvalds 			/*
5341da177e4SLinus Torvalds 			 * writeback is not making progress due to locked
5351da177e4SLinus Torvalds 			 * buffers.  Skip this inode for now.
5361da177e4SLinus Torvalds 			 */
537f57b9b7bSAndrew Morton 			redirty_tail(inode);
5381da177e4SLinus Torvalds 		}
5391da177e4SLinus Torvalds 		spin_unlock(&inode_lock);
5401da177e4SLinus Torvalds 		iput(inode);
5414ffc8444SOGAWA Hirofumi 		cond_resched();
5421da177e4SLinus Torvalds 		spin_lock(&inode_lock);
5438bc3be27SFengguang Wu 		if (wbc->nr_to_write <= 0) {
5448bc3be27SFengguang Wu 			wbc->more_io = 1;
5451da177e4SLinus Torvalds 			break;
5461da177e4SLinus Torvalds 		}
5478bc3be27SFengguang Wu 		if (!list_empty(&sb->s_more_io))
5488bc3be27SFengguang Wu 			wbc->more_io = 1;
5498bc3be27SFengguang Wu 	}
55038f21977SNick Piggin 
55138f21977SNick Piggin 	if (sync) {
55238f21977SNick Piggin 		struct inode *inode, *old_inode = NULL;
55338f21977SNick Piggin 
55438f21977SNick Piggin 		/*
55538f21977SNick Piggin 		 * Data integrity sync. Must wait for all pages under writeback,
55638f21977SNick Piggin 		 * because there may have been pages dirtied before our sync
55738f21977SNick Piggin 		 * call, but which had writeout started before we write it out.
55838f21977SNick Piggin 		 * In which case, the inode may not be on the dirty list, but
55938f21977SNick Piggin 		 * we still have to wait for that writeout.
56038f21977SNick Piggin 		 */
56138f21977SNick Piggin 		list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
56238f21977SNick Piggin 			struct address_space *mapping;
56338f21977SNick Piggin 
564b6fac63cSWu Fengguang 			if (inode->i_state &
565b6fac63cSWu Fengguang 					(I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
56638f21977SNick Piggin 				continue;
56738f21977SNick Piggin 			mapping = inode->i_mapping;
56838f21977SNick Piggin 			if (mapping->nrpages == 0)
56938f21977SNick Piggin 				continue;
57038f21977SNick Piggin 			__iget(inode);
571ae8547b0SHans Reiser 			spin_unlock(&inode_lock);
57238f21977SNick Piggin 			/*
57338f21977SNick Piggin 			 * We hold a reference to 'inode' so it couldn't have
57438f21977SNick Piggin 			 * been removed from s_inodes list while we dropped the
57538f21977SNick Piggin 			 * inode_lock.  We cannot iput the inode now as we can
57638f21977SNick Piggin 			 * be holding the last reference and we cannot iput it
57738f21977SNick Piggin 			 * under inode_lock. So we keep the reference and iput
57838f21977SNick Piggin 			 * it later.
57938f21977SNick Piggin 			 */
58038f21977SNick Piggin 			iput(old_inode);
58138f21977SNick Piggin 			old_inode = inode;
58238f21977SNick Piggin 
58338f21977SNick Piggin 			filemap_fdatawait(mapping);
58438f21977SNick Piggin 
58538f21977SNick Piggin 			cond_resched();
58638f21977SNick Piggin 
58738f21977SNick Piggin 			spin_lock(&inode_lock);
58838f21977SNick Piggin 		}
58938f21977SNick Piggin 		spin_unlock(&inode_lock);
59038f21977SNick Piggin 		iput(old_inode);
59138f21977SNick Piggin 	} else
59238f21977SNick Piggin 		spin_unlock(&inode_lock);
59338f21977SNick Piggin 
5941da177e4SLinus Torvalds 	return;		/* Leave any unwritten inodes on s_io */
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds /*
5981da177e4SLinus Torvalds  * Start writeback of dirty pagecache data against all unlocked inodes.
5991da177e4SLinus Torvalds  *
6001da177e4SLinus Torvalds  * Note:
6011da177e4SLinus Torvalds  * We don't need to grab a reference to superblock here. If it has non-empty
6021da177e4SLinus Torvalds  * ->s_dirty it's hadn't been killed yet and kill_super() won't proceed
6032c136579SFengguang Wu  * past sync_inodes_sb() until the ->s_dirty/s_io/s_more_io lists are all
6041da177e4SLinus Torvalds  * empty. Since __sync_single_inode() regains inode_lock before it finally moves
6051da177e4SLinus Torvalds  * inode from superblock lists we are OK.
6061da177e4SLinus Torvalds  *
6071da177e4SLinus Torvalds  * If `older_than_this' is non-zero then only flush inodes which have a
6081da177e4SLinus Torvalds  * flushtime older than *older_than_this.
6091da177e4SLinus Torvalds  *
6101da177e4SLinus Torvalds  * If `bdi' is non-zero then we will scan the first inode against each
6111da177e4SLinus Torvalds  * superblock until we find the matching ones.  One group will be the dirty
6121da177e4SLinus Torvalds  * inodes against a filesystem.  Then when we hit the dummy blockdev superblock,
6131da177e4SLinus Torvalds  * sync_sb_inodes will seekout the blockdev which matches `bdi'.  Maybe not
6141da177e4SLinus Torvalds  * super-efficient but we're about to do a ton of I/O...
6151da177e4SLinus Torvalds  */
6161da177e4SLinus Torvalds void
6171da177e4SLinus Torvalds writeback_inodes(struct writeback_control *wbc)
6181da177e4SLinus Torvalds {
6191da177e4SLinus Torvalds 	struct super_block *sb;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	might_sleep();
6221da177e4SLinus Torvalds 	spin_lock(&sb_lock);
6231da177e4SLinus Torvalds restart:
624797074e4SAkinobu Mita 	list_for_each_entry_reverse(sb, &super_blocks, s_list) {
62508d8e974SFengguang Wu 		if (sb_has_dirty_inodes(sb)) {
6261da177e4SLinus Torvalds 			/* we're making our own get_super here */
6271da177e4SLinus Torvalds 			sb->s_count++;
6281da177e4SLinus Torvalds 			spin_unlock(&sb_lock);
6291da177e4SLinus Torvalds 			/*
6301da177e4SLinus Torvalds 			 * If we can't get the readlock, there's no sense in
6311da177e4SLinus Torvalds 			 * waiting around, most of the time the FS is going to
6321da177e4SLinus Torvalds 			 * be unmounted by the time it is released.
6331da177e4SLinus Torvalds 			 */
6341da177e4SLinus Torvalds 			if (down_read_trylock(&sb->s_umount)) {
635ae8547b0SHans Reiser 				if (sb->s_root)
636d8a8559cSJens Axboe 					generic_sync_sb_inodes(sb, wbc);
6371da177e4SLinus Torvalds 				up_read(&sb->s_umount);
6381da177e4SLinus Torvalds 			}
6391da177e4SLinus Torvalds 			spin_lock(&sb_lock);
6401da177e4SLinus Torvalds 			if (__put_super_and_need_restart(sb))
6411da177e4SLinus Torvalds 				goto restart;
6421da177e4SLinus Torvalds 		}
6431da177e4SLinus Torvalds 		if (wbc->nr_to_write <= 0)
6441da177e4SLinus Torvalds 			break;
6451da177e4SLinus Torvalds 	}
6461da177e4SLinus Torvalds 	spin_unlock(&sb_lock);
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
649d8a8559cSJens Axboe /**
650d8a8559cSJens Axboe  * writeback_inodes_sb	-	writeback dirty inodes from given super_block
651d8a8559cSJens Axboe  * @sb: the superblock
6521da177e4SLinus Torvalds  *
653d8a8559cSJens Axboe  * Start writeback on some inodes on this super_block. No guarantees are made
654d8a8559cSJens Axboe  * on how many (if any) will be written, and this function does not wait
655d8a8559cSJens Axboe  * for IO completion of submitted IO. The number of pages submitted is
656d8a8559cSJens Axboe  * returned.
6571da177e4SLinus Torvalds  */
658d8a8559cSJens Axboe long writeback_inodes_sb(struct super_block *sb)
6591da177e4SLinus Torvalds {
6601da177e4SLinus Torvalds 	struct writeback_control wbc = {
661d8a8559cSJens Axboe 		.sync_mode	= WB_SYNC_NONE,
662111ebb6eSOGAWA Hirofumi 		.range_start	= 0,
663111ebb6eSOGAWA Hirofumi 		.range_end	= LLONG_MAX,
6641da177e4SLinus Torvalds 	};
665b1e7a8fdSChristoph Lameter 	unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
666fd39fc85SChristoph Lameter 	unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
667d8a8559cSJens Axboe 	long nr_to_write;
6681da177e4SLinus Torvalds 
669d8a8559cSJens Axboe 	nr_to_write = nr_dirty + nr_unstable +
67038f21977SNick Piggin 			(inodes_stat.nr_inodes - inodes_stat.nr_unused);
67138f21977SNick Piggin 
672d8a8559cSJens Axboe 	wbc.nr_to_write = nr_to_write;
673d8a8559cSJens Axboe 	generic_sync_sb_inodes(sb, &wbc);
674d8a8559cSJens Axboe 	return nr_to_write - wbc.nr_to_write;
6751da177e4SLinus Torvalds }
676d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb);
677d8a8559cSJens Axboe 
678d8a8559cSJens Axboe /**
679d8a8559cSJens Axboe  * sync_inodes_sb	-	sync sb inode pages
680d8a8559cSJens Axboe  * @sb: the superblock
681d8a8559cSJens Axboe  *
682d8a8559cSJens Axboe  * This function writes and waits on any dirty inode belonging to this
683d8a8559cSJens Axboe  * super_block. The number of pages synced is returned.
684d8a8559cSJens Axboe  */
685d8a8559cSJens Axboe long sync_inodes_sb(struct super_block *sb)
686d8a8559cSJens Axboe {
687d8a8559cSJens Axboe 	struct writeback_control wbc = {
688d8a8559cSJens Axboe 		.sync_mode	= WB_SYNC_ALL,
689d8a8559cSJens Axboe 		.range_start	= 0,
690d8a8559cSJens Axboe 		.range_end	= LLONG_MAX,
691d8a8559cSJens Axboe 	};
692d8a8559cSJens Axboe 	long nr_to_write = LONG_MAX; /* doesn't actually matter */
693d8a8559cSJens Axboe 
694d8a8559cSJens Axboe 	wbc.nr_to_write = nr_to_write;
695d8a8559cSJens Axboe 	generic_sync_sb_inodes(sb, &wbc);
696d8a8559cSJens Axboe 	return nr_to_write - wbc.nr_to_write;
697d8a8559cSJens Axboe }
698d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb);
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds /**
7011da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
7021da177e4SLinus Torvalds  * @inode: inode to write to disk
7031da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
7041da177e4SLinus Torvalds  *
7057f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
7067f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
7077f04c26dSAndrea Arcangeli  *
7087f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
7091da177e4SLinus Torvalds  */
7101da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
7111da177e4SLinus Torvalds {
7121da177e4SLinus Torvalds 	int ret;
7131da177e4SLinus Torvalds 	struct writeback_control wbc = {
7141da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
71518914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
716111ebb6eSOGAWA Hirofumi 		.range_start = 0,
717111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
7181da177e4SLinus Torvalds 	};
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
72149364ce2SAndrew Morton 		wbc.nr_to_write = 0;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	might_sleep();
7241da177e4SLinus Torvalds 	spin_lock(&inode_lock);
72501c03194SChristoph Hellwig 	ret = writeback_single_inode(inode, &wbc);
7261da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
7271da177e4SLinus Torvalds 	if (sync)
7281c0eeaf5SJoern Engel 		inode_sync_wait(inode);
7291da177e4SLinus Torvalds 	return ret;
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds /**
7341da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
7351da177e4SLinus Torvalds  * @inode: the inode to sync
7361da177e4SLinus Torvalds  * @wbc: controls the writeback mode
7371da177e4SLinus Torvalds  *
7381da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
7391da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
7401da177e4SLinus Torvalds  * update inode->i_state.
7411da177e4SLinus Torvalds  *
7421da177e4SLinus Torvalds  * The caller must have a ref on the inode.
7431da177e4SLinus Torvalds  */
7441da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
7451da177e4SLinus Torvalds {
7461da177e4SLinus Torvalds 	int ret;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	spin_lock(&inode_lock);
74901c03194SChristoph Hellwig 	ret = writeback_single_inode(inode, wbc);
7501da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
7511da177e4SLinus Torvalds 	return ret;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds /**
7561da177e4SLinus Torvalds  * generic_osync_inode - flush all dirty data for a given inode to disk
7571da177e4SLinus Torvalds  * @inode: inode to write
75867be2dd1SMartin Waitz  * @mapping: the address_space that should be flushed
7591da177e4SLinus Torvalds  * @what:  what to write and wait upon
7601da177e4SLinus Torvalds  *
7611da177e4SLinus Torvalds  * This can be called by file_write functions for files which have the
7621da177e4SLinus Torvalds  * O_SYNC flag set, to flush dirty writes to disk.
7631da177e4SLinus Torvalds  *
7641da177e4SLinus Torvalds  * @what is a bitmask, specifying which part of the inode's data should be
765b8887e6eSRandy Dunlap  * written and waited upon.
7661da177e4SLinus Torvalds  *
7671da177e4SLinus Torvalds  *    OSYNC_DATA:     i_mapping's dirty data
7681da177e4SLinus Torvalds  *    OSYNC_METADATA: the buffers at i_mapping->private_list
7691da177e4SLinus Torvalds  *    OSYNC_INODE:    the inode itself
7701da177e4SLinus Torvalds  */
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds int generic_osync_inode(struct inode *inode, struct address_space *mapping, int what)
7731da177e4SLinus Torvalds {
7741da177e4SLinus Torvalds 	int err = 0;
7751da177e4SLinus Torvalds 	int need_write_inode_now = 0;
7761da177e4SLinus Torvalds 	int err2;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	if (what & OSYNC_DATA)
7791da177e4SLinus Torvalds 		err = filemap_fdatawrite(mapping);
7801da177e4SLinus Torvalds 	if (what & (OSYNC_METADATA|OSYNC_DATA)) {
7811da177e4SLinus Torvalds 		err2 = sync_mapping_buffers(mapping);
7821da177e4SLinus Torvalds 		if (!err)
7831da177e4SLinus Torvalds 			err = err2;
7841da177e4SLinus Torvalds 	}
7851da177e4SLinus Torvalds 	if (what & OSYNC_DATA) {
7861da177e4SLinus Torvalds 		err2 = filemap_fdatawait(mapping);
7871da177e4SLinus Torvalds 		if (!err)
7881da177e4SLinus Torvalds 			err = err2;
7891da177e4SLinus Torvalds 	}
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	spin_lock(&inode_lock);
7921da177e4SLinus Torvalds 	if ((inode->i_state & I_DIRTY) &&
7931da177e4SLinus Torvalds 	    ((what & OSYNC_INODE) || (inode->i_state & I_DIRTY_DATASYNC)))
7941da177e4SLinus Torvalds 		need_write_inode_now = 1;
7951da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	if (need_write_inode_now) {
7981da177e4SLinus Torvalds 		err2 = write_inode_now(inode, 1);
7991da177e4SLinus Torvalds 		if (!err)
8001da177e4SLinus Torvalds 			err = err2;
8011da177e4SLinus Torvalds 	}
8021da177e4SLinus Torvalds 	else
8031c0eeaf5SJoern Engel 		inode_sync_wait(inode);
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	return err;
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds EXPORT_SYMBOL(generic_osync_inode);
808