xref: /openbmc/linux/fs/fs-writeback.c (revision d2caa3c5)
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 
671da177e4SLinus Torvalds /**
681da177e4SLinus Torvalds  *	__mark_inode_dirty -	internal function
691da177e4SLinus Torvalds  *	@inode: inode to mark
701da177e4SLinus Torvalds  *	@flags: what kind of dirty (i.e. I_DIRTY_SYNC)
711da177e4SLinus Torvalds  *	Mark an inode as dirty. Callers should use mark_inode_dirty or
721da177e4SLinus Torvalds  *  	mark_inode_dirty_sync.
731da177e4SLinus Torvalds  *
741da177e4SLinus Torvalds  * Put the inode on the super block's dirty list.
751da177e4SLinus Torvalds  *
761da177e4SLinus Torvalds  * CAREFUL! We mark it dirty unconditionally, but move it onto the
771da177e4SLinus Torvalds  * dirty list only if it is hashed or if it refers to a blockdev.
781da177e4SLinus Torvalds  * If it was not hashed, it will never be added to the dirty list
791da177e4SLinus Torvalds  * even if it is later hashed, as it will have been marked dirty already.
801da177e4SLinus Torvalds  *
811da177e4SLinus Torvalds  * In short, make sure you hash any inodes _before_ you start marking
821da177e4SLinus Torvalds  * them dirty.
831da177e4SLinus Torvalds  *
841da177e4SLinus Torvalds  * This function *must* be atomic for the I_DIRTY_PAGES case -
851da177e4SLinus Torvalds  * set_page_dirty() is called under spinlock in several places.
861da177e4SLinus Torvalds  *
871da177e4SLinus Torvalds  * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
881da177e4SLinus Torvalds  * the block-special inode (/dev/hda1) itself.  And the ->dirtied_when field of
891da177e4SLinus Torvalds  * the kernel-internal blockdev inode represents the dirtying time of the
901da177e4SLinus Torvalds  * blockdev's pages.  This is why for I_DIRTY_PAGES we always use
911da177e4SLinus Torvalds  * page->mapping->host, so the page-dirtying time is recorded in the internal
921da177e4SLinus Torvalds  * blockdev inode.
931da177e4SLinus Torvalds  */
941da177e4SLinus Torvalds void __mark_inode_dirty(struct inode *inode, int flags)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	/*
991da177e4SLinus Torvalds 	 * Don't do this for I_DIRTY_PAGES - that doesn't actually
1001da177e4SLinus Torvalds 	 * dirty the inode itself
1011da177e4SLinus Torvalds 	 */
1021da177e4SLinus Torvalds 	if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
1031da177e4SLinus Torvalds 		if (sb->s_op->dirty_inode)
1041da177e4SLinus Torvalds 			sb->s_op->dirty_inode(inode);
1051da177e4SLinus Torvalds 	}
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	/*
1081da177e4SLinus Torvalds 	 * make sure that changes are seen by all cpus before we test i_state
1091da177e4SLinus Torvalds 	 * -- mikulas
1101da177e4SLinus Torvalds 	 */
1111da177e4SLinus Torvalds 	smp_mb();
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	/* avoid the locking if we can */
1141da177e4SLinus Torvalds 	if ((inode->i_state & flags) == flags)
1151da177e4SLinus Torvalds 		return;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	if (unlikely(block_dump)) {
1181da177e4SLinus Torvalds 		struct dentry *dentry = NULL;
1191da177e4SLinus Torvalds 		const char *name = "?";
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 		if (!list_empty(&inode->i_dentry)) {
1221da177e4SLinus Torvalds 			dentry = list_entry(inode->i_dentry.next,
1231da177e4SLinus Torvalds 					    struct dentry, d_alias);
1241da177e4SLinus Torvalds 			if (dentry && dentry->d_name.name)
1251da177e4SLinus Torvalds 				name = (const char *) dentry->d_name.name;
1261da177e4SLinus Torvalds 		}
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 		if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev"))
1291da177e4SLinus Torvalds 			printk(KERN_DEBUG
1301da177e4SLinus Torvalds 			       "%s(%d): dirtied inode %lu (%s) on %s\n",
131ba25f9dcSPavel Emelyanov 			       current->comm, task_pid_nr(current), inode->i_ino,
1321da177e4SLinus Torvalds 			       name, inode->i_sb->s_id);
1331da177e4SLinus Torvalds 	}
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	spin_lock(&inode_lock);
1361da177e4SLinus Torvalds 	if ((inode->i_state & flags) != flags) {
1371da177e4SLinus Torvalds 		const int was_dirty = inode->i_state & I_DIRTY;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 		inode->i_state |= flags;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 		/*
1421c0eeaf5SJoern Engel 		 * If the inode is being synced, just update its dirty state.
1431da177e4SLinus Torvalds 		 * The unlocker will place the inode on the appropriate
1441da177e4SLinus Torvalds 		 * superblock list, based upon its state.
1451da177e4SLinus Torvalds 		 */
1461c0eeaf5SJoern Engel 		if (inode->i_state & I_SYNC)
1471da177e4SLinus Torvalds 			goto out;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 		/*
1501da177e4SLinus Torvalds 		 * Only add valid (hashed) inodes to the superblock's
1511da177e4SLinus Torvalds 		 * dirty list.  Add blockdev inodes as well.
1521da177e4SLinus Torvalds 		 */
1531da177e4SLinus Torvalds 		if (!S_ISBLK(inode->i_mode)) {
1541da177e4SLinus Torvalds 			if (hlist_unhashed(&inode->i_hash))
1551da177e4SLinus Torvalds 				goto out;
1561da177e4SLinus Torvalds 		}
1571da177e4SLinus Torvalds 		if (inode->i_state & (I_FREEING|I_CLEAR))
1581da177e4SLinus Torvalds 			goto out;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 		/*
1612c136579SFengguang Wu 		 * If the inode was already on s_dirty/s_io/s_more_io, don't
1621da177e4SLinus Torvalds 		 * reposition it (that would break s_dirty time-ordering).
1631da177e4SLinus Torvalds 		 */
1641da177e4SLinus Torvalds 		if (!was_dirty) {
1651da177e4SLinus Torvalds 			inode->dirtied_when = jiffies;
1661da177e4SLinus Torvalds 			list_move(&inode->i_list, &sb->s_dirty);
1671da177e4SLinus Torvalds 		}
1681da177e4SLinus Torvalds 	}
1691da177e4SLinus Torvalds out:
1701da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds EXPORT_SYMBOL(__mark_inode_dirty);
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds static int write_inode(struct inode *inode, int sync)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
1781da177e4SLinus Torvalds 		return inode->i_sb->s_op->write_inode(inode, sync);
1791da177e4SLinus Torvalds 	return 0;
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds /*
1836610a0bcSAndrew Morton  * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
1846610a0bcSAndrew Morton  * furthest end of its superblock's dirty-inode list.
1856610a0bcSAndrew Morton  *
1866610a0bcSAndrew Morton  * Before stamping the inode's ->dirtied_when, we check to see whether it is
1876610a0bcSAndrew Morton  * already the most-recently-dirtied inode on the s_dirty list.  If that is
1886610a0bcSAndrew Morton  * the case then the inode must have been redirtied while it was being written
1896610a0bcSAndrew Morton  * out and we don't reset its dirtied_when.
1906610a0bcSAndrew Morton  */
1916610a0bcSAndrew Morton static void redirty_tail(struct inode *inode)
1926610a0bcSAndrew Morton {
1936610a0bcSAndrew Morton 	struct super_block *sb = inode->i_sb;
1946610a0bcSAndrew Morton 
1956610a0bcSAndrew Morton 	if (!list_empty(&sb->s_dirty)) {
1966610a0bcSAndrew Morton 		struct inode *tail_inode;
1976610a0bcSAndrew Morton 
1986610a0bcSAndrew Morton 		tail_inode = list_entry(sb->s_dirty.next, struct inode, i_list);
199d2caa3c5SJeff Layton 		if (time_before(inode->dirtied_when,
2006610a0bcSAndrew Morton 				tail_inode->dirtied_when))
2016610a0bcSAndrew Morton 			inode->dirtied_when = jiffies;
2026610a0bcSAndrew Morton 	}
2036610a0bcSAndrew Morton 	list_move(&inode->i_list, &sb->s_dirty);
2046610a0bcSAndrew Morton }
2056610a0bcSAndrew Morton 
2066610a0bcSAndrew Morton /*
2070e0f4fc2SKen Chen  * requeue inode for re-scanning after sb->s_io list is exhausted.
208c986d1e2SAndrew Morton  */
2090e0f4fc2SKen Chen static void requeue_io(struct inode *inode)
210c986d1e2SAndrew Morton {
2110e0f4fc2SKen Chen 	list_move(&inode->i_list, &inode->i_sb->s_more_io);
212c986d1e2SAndrew Morton }
213c986d1e2SAndrew Morton 
2141c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode)
2151c0eeaf5SJoern Engel {
2161c0eeaf5SJoern Engel 	/*
2171c0eeaf5SJoern Engel 	 * Prevent speculative execution through spin_unlock(&inode_lock);
2181c0eeaf5SJoern Engel 	 */
2191c0eeaf5SJoern Engel 	smp_mb();
2201c0eeaf5SJoern Engel 	wake_up_bit(&inode->i_state, __I_SYNC);
2211c0eeaf5SJoern Engel }
2221c0eeaf5SJoern Engel 
223d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t)
224d2caa3c5SJeff Layton {
225d2caa3c5SJeff Layton 	bool ret = time_after(inode->dirtied_when, t);
226d2caa3c5SJeff Layton #ifndef CONFIG_64BIT
227d2caa3c5SJeff Layton 	/*
228d2caa3c5SJeff Layton 	 * For inodes being constantly redirtied, dirtied_when can get stuck.
229d2caa3c5SJeff Layton 	 * It _appears_ to be in the future, but is actually in distant past.
230d2caa3c5SJeff Layton 	 * This test is necessary to prevent such wrapped-around relative times
231d2caa3c5SJeff Layton 	 * from permanently stopping the whole pdflush writeback.
232d2caa3c5SJeff Layton 	 */
233d2caa3c5SJeff Layton 	ret = ret && time_before_eq(inode->dirtied_when, jiffies);
234d2caa3c5SJeff Layton #endif
235d2caa3c5SJeff Layton 	return ret;
236d2caa3c5SJeff Layton }
237d2caa3c5SJeff Layton 
238c986d1e2SAndrew Morton /*
2392c136579SFengguang Wu  * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
2402c136579SFengguang Wu  */
2412c136579SFengguang Wu static void move_expired_inodes(struct list_head *delaying_queue,
2422c136579SFengguang Wu 			       struct list_head *dispatch_queue,
2432c136579SFengguang Wu 				unsigned long *older_than_this)
2442c136579SFengguang Wu {
2452c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
2462c136579SFengguang Wu 		struct inode *inode = list_entry(delaying_queue->prev,
2472c136579SFengguang Wu 						struct inode, i_list);
2482c136579SFengguang Wu 		if (older_than_this &&
249d2caa3c5SJeff Layton 		    inode_dirtied_after(inode, *older_than_this))
2502c136579SFengguang Wu 			break;
2512c136579SFengguang Wu 		list_move(&inode->i_list, dispatch_queue);
2522c136579SFengguang Wu 	}
2532c136579SFengguang Wu }
2542c136579SFengguang Wu 
2552c136579SFengguang Wu /*
2562c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
2572c136579SFengguang Wu  */
2582c136579SFengguang Wu static void queue_io(struct super_block *sb,
2592c136579SFengguang Wu 				unsigned long *older_than_this)
2602c136579SFengguang Wu {
2612c136579SFengguang Wu 	list_splice_init(&sb->s_more_io, sb->s_io.prev);
2622c136579SFengguang Wu 	move_expired_inodes(&sb->s_dirty, &sb->s_io, older_than_this);
2632c136579SFengguang Wu }
2642c136579SFengguang Wu 
26508d8e974SFengguang Wu int sb_has_dirty_inodes(struct super_block *sb)
26608d8e974SFengguang Wu {
26708d8e974SFengguang Wu 	return !list_empty(&sb->s_dirty) ||
26808d8e974SFengguang Wu 	       !list_empty(&sb->s_io) ||
26908d8e974SFengguang Wu 	       !list_empty(&sb->s_more_io);
27008d8e974SFengguang Wu }
27108d8e974SFengguang Wu EXPORT_SYMBOL(sb_has_dirty_inodes);
27208d8e974SFengguang Wu 
2732c136579SFengguang Wu /*
2741da177e4SLinus Torvalds  * Write a single inode's dirty pages and inode data out to disk.
2751da177e4SLinus Torvalds  * If `wait' is set, wait on the writeout.
2761da177e4SLinus Torvalds  *
2771da177e4SLinus Torvalds  * The whole writeout design is quite complex and fragile.  We want to avoid
2781da177e4SLinus Torvalds  * starvation of particular inodes when others are being redirtied, prevent
2791da177e4SLinus Torvalds  * livelocks, etc.
2801da177e4SLinus Torvalds  *
2811da177e4SLinus Torvalds  * Called under inode_lock.
2821da177e4SLinus Torvalds  */
2831da177e4SLinus Torvalds static int
2841da177e4SLinus Torvalds __sync_single_inode(struct inode *inode, struct writeback_control *wbc)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	unsigned dirty;
2871da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
2881da177e4SLinus Torvalds 	int wait = wbc->sync_mode == WB_SYNC_ALL;
2891da177e4SLinus Torvalds 	int ret;
2901da177e4SLinus Torvalds 
2911c0eeaf5SJoern Engel 	BUG_ON(inode->i_state & I_SYNC);
2927ef0d737SNick Piggin 	WARN_ON(inode->i_state & I_NEW);
2931da177e4SLinus Torvalds 
2941c0eeaf5SJoern Engel 	/* Set I_SYNC, reset I_DIRTY */
2951da177e4SLinus Torvalds 	dirty = inode->i_state & I_DIRTY;
2961c0eeaf5SJoern Engel 	inode->i_state |= I_SYNC;
2971da177e4SLinus Torvalds 	inode->i_state &= ~I_DIRTY;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	/* Don't write the inode if only I_DIRTY_PAGES was set */
3041da177e4SLinus Torvalds 	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
3051da177e4SLinus Torvalds 		int err = write_inode(inode, wait);
3061da177e4SLinus Torvalds 		if (ret == 0)
3071da177e4SLinus Torvalds 			ret = err;
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	if (wait) {
3111da177e4SLinus Torvalds 		int err = filemap_fdatawait(mapping);
3121da177e4SLinus Torvalds 		if (ret == 0)
3131da177e4SLinus Torvalds 			ret = err;
3141da177e4SLinus Torvalds 	}
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	spin_lock(&inode_lock);
3177ef0d737SNick Piggin 	WARN_ON(inode->i_state & I_NEW);
3181c0eeaf5SJoern Engel 	inode->i_state &= ~I_SYNC;
3191da177e4SLinus Torvalds 	if (!(inode->i_state & I_FREEING)) {
3201da177e4SLinus Torvalds 		if (!(inode->i_state & I_DIRTY) &&
3211da177e4SLinus Torvalds 		    mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
3221da177e4SLinus Torvalds 			/*
3231da177e4SLinus Torvalds 			 * We didn't write back all the pages.  nfs_writepages()
3241da177e4SLinus Torvalds 			 * sometimes bales out without doing anything. Redirty
3252c136579SFengguang Wu 			 * the inode; Move it from s_io onto s_more_io/s_dirty.
3261b43ef91SAndrew Morton 			 */
3271b43ef91SAndrew Morton 			/*
3281b43ef91SAndrew Morton 			 * akpm: if the caller was the kupdate function we put
3291b43ef91SAndrew Morton 			 * this inode at the head of s_dirty so it gets first
3301b43ef91SAndrew Morton 			 * consideration.  Otherwise, move it to the tail, for
3311b43ef91SAndrew Morton 			 * the reasons described there.  I'm not really sure
3321b43ef91SAndrew Morton 			 * how much sense this makes.  Presumably I had a good
3331b43ef91SAndrew Morton 			 * reasons for doing it this way, and I'd rather not
3341b43ef91SAndrew Morton 			 * muck with it at present.
3351da177e4SLinus Torvalds 			 */
3361da177e4SLinus Torvalds 			if (wbc->for_kupdate) {
3371da177e4SLinus Torvalds 				/*
3382c136579SFengguang Wu 				 * For the kupdate function we move the inode
3392c136579SFengguang Wu 				 * to s_more_io so it will get more writeout as
3402c136579SFengguang Wu 				 * soon as the queue becomes uncongested.
3411da177e4SLinus Torvalds 				 */
3421da177e4SLinus Torvalds 				inode->i_state |= I_DIRTY_PAGES;
3438bc3be27SFengguang Wu 				if (wbc->nr_to_write <= 0) {
3448bc3be27SFengguang Wu 					/*
3458bc3be27SFengguang Wu 					 * slice used up: queue for next turn
3468bc3be27SFengguang Wu 					 */
3470e0f4fc2SKen Chen 					requeue_io(inode);
3481da177e4SLinus Torvalds 				} else {
3491da177e4SLinus Torvalds 					/*
3508bc3be27SFengguang Wu 					 * somehow blocked: retry later
3518bc3be27SFengguang Wu 					 */
3528bc3be27SFengguang Wu 					redirty_tail(inode);
3538bc3be27SFengguang Wu 				}
3548bc3be27SFengguang Wu 			} else {
3558bc3be27SFengguang Wu 				/*
3561da177e4SLinus Torvalds 				 * Otherwise fully redirty the inode so that
3571da177e4SLinus Torvalds 				 * other inodes on this superblock will get some
3581da177e4SLinus Torvalds 				 * writeout.  Otherwise heavy writing to one
3591da177e4SLinus Torvalds 				 * file would indefinitely suspend writeout of
3601da177e4SLinus Torvalds 				 * all the other files.
3611da177e4SLinus Torvalds 				 */
3621da177e4SLinus Torvalds 				inode->i_state |= I_DIRTY_PAGES;
3631b43ef91SAndrew Morton 				redirty_tail(inode);
3641da177e4SLinus Torvalds 			}
3651da177e4SLinus Torvalds 		} else if (inode->i_state & I_DIRTY) {
3661da177e4SLinus Torvalds 			/*
3671da177e4SLinus Torvalds 			 * Someone redirtied the inode while were writing back
3681da177e4SLinus Torvalds 			 * the pages.
3691da177e4SLinus Torvalds 			 */
3706610a0bcSAndrew Morton 			redirty_tail(inode);
3711da177e4SLinus Torvalds 		} else if (atomic_read(&inode->i_count)) {
3721da177e4SLinus Torvalds 			/*
3731da177e4SLinus Torvalds 			 * The inode is clean, inuse
3741da177e4SLinus Torvalds 			 */
3751da177e4SLinus Torvalds 			list_move(&inode->i_list, &inode_in_use);
3761da177e4SLinus Torvalds 		} else {
3771da177e4SLinus Torvalds 			/*
3781da177e4SLinus Torvalds 			 * The inode is clean, unused
3791da177e4SLinus Torvalds 			 */
3801da177e4SLinus Torvalds 			list_move(&inode->i_list, &inode_unused);
3811da177e4SLinus Torvalds 		}
3821da177e4SLinus Torvalds 	}
3831c0eeaf5SJoern Engel 	inode_sync_complete(inode);
3841da177e4SLinus Torvalds 	return ret;
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds /*
3887f04c26dSAndrea Arcangeli  * Write out an inode's dirty pages.  Called under inode_lock.  Either the
3897f04c26dSAndrea Arcangeli  * caller has ref on the inode (either via __iget or via syscall against an fd)
3907f04c26dSAndrea Arcangeli  * or the inode has I_WILL_FREE set (via generic_forget_inode)
3911da177e4SLinus Torvalds  */
3921da177e4SLinus Torvalds static int
3937f04c26dSAndrea Arcangeli __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	wait_queue_head_t *wqh;
3961da177e4SLinus Torvalds 
3977f04c26dSAndrea Arcangeli 	if (!atomic_read(&inode->i_count))
398659603efSAndrea Arcangeli 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
3997f04c26dSAndrea Arcangeli 	else
4007f04c26dSAndrea Arcangeli 		WARN_ON(inode->i_state & I_WILL_FREE);
4017f04c26dSAndrea Arcangeli 
4021c0eeaf5SJoern Engel 	if ((wbc->sync_mode != WB_SYNC_ALL) && (inode->i_state & I_SYNC)) {
40365cb9b47SAndrew Morton 		/*
40465cb9b47SAndrew Morton 		 * We're skipping this inode because it's locked, and we're not
4052c136579SFengguang Wu 		 * doing writeback-for-data-integrity.  Move it to s_more_io so
4062c136579SFengguang Wu 		 * that writeback can proceed with the other inodes on s_io.
4072c136579SFengguang Wu 		 * We'll have another go at writing back this inode when we
4082c136579SFengguang Wu 		 * completed a full scan of s_io.
40965cb9b47SAndrew Morton 		 */
4100e0f4fc2SKen Chen 		requeue_io(inode);
4112d544564SQi Yong 		return 0;
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	/*
4151da177e4SLinus Torvalds 	 * It's a data-integrity sync.  We must wait.
4161da177e4SLinus Torvalds 	 */
4171c0eeaf5SJoern Engel 	if (inode->i_state & I_SYNC) {
4181c0eeaf5SJoern Engel 		DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
4191da177e4SLinus Torvalds 
4201c0eeaf5SJoern Engel 		wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
4211da177e4SLinus Torvalds 		do {
4221da177e4SLinus Torvalds 			spin_unlock(&inode_lock);
4231da177e4SLinus Torvalds 			__wait_on_bit(wqh, &wq, inode_wait,
4241da177e4SLinus Torvalds 							TASK_UNINTERRUPTIBLE);
4251da177e4SLinus Torvalds 			spin_lock(&inode_lock);
4261c0eeaf5SJoern Engel 		} while (inode->i_state & I_SYNC);
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds 	return __sync_single_inode(inode, wbc);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds /*
4321da177e4SLinus Torvalds  * Write out a superblock's list of dirty inodes.  A wait will be performed
4331da177e4SLinus Torvalds  * upon no inodes, all inodes or the final one, depending upon sync_mode.
4341da177e4SLinus Torvalds  *
4351da177e4SLinus Torvalds  * If older_than_this is non-NULL, then only write out inodes which
4361da177e4SLinus Torvalds  * had their first dirtying at a time earlier than *older_than_this.
4371da177e4SLinus Torvalds  *
4381da177e4SLinus Torvalds  * If we're a pdlfush thread, then implement pdflush collision avoidance
4391da177e4SLinus Torvalds  * against the entire list.
4401da177e4SLinus Torvalds  *
4411da177e4SLinus Torvalds  * If `bdi' is non-zero then we're being asked to writeback a specific queue.
4421da177e4SLinus Torvalds  * This function assumes that the blockdev superblock's inodes are backed by
4431da177e4SLinus Torvalds  * a variety of queues, so all inodes are searched.  For other superblocks,
4441da177e4SLinus Torvalds  * assume that all inodes are backed by the same queue.
4451da177e4SLinus Torvalds  *
4461da177e4SLinus Torvalds  * FIXME: this linear search could get expensive with many fileystems.  But
4471da177e4SLinus Torvalds  * how to fix?  We need to go from an address_space to all inodes which share
4481da177e4SLinus Torvalds  * a queue with that address_space.  (Easy: have a global "dirty superblocks"
4491da177e4SLinus Torvalds  * list).
4501da177e4SLinus Torvalds  *
4511da177e4SLinus Torvalds  * The inodes to be written are parked on sb->s_io.  They are moved back onto
4521da177e4SLinus Torvalds  * sb->s_dirty as they are selected for writing.  This way, none can be missed
4531da177e4SLinus Torvalds  * on the writer throttling path, and we get decent balancing between many
4541c0eeaf5SJoern Engel  * throttled threads: we don't want them all piling up on inode_sync_wait.
4551da177e4SLinus Torvalds  */
4564ee6afd3SArtem Bityutskiy void generic_sync_sb_inodes(struct super_block *sb,
4574ee6afd3SArtem Bityutskiy 				struct writeback_control *wbc)
4581da177e4SLinus Torvalds {
4591da177e4SLinus Torvalds 	const unsigned long start = jiffies;	/* livelock avoidance */
46038f21977SNick Piggin 	int sync = wbc->sync_mode == WB_SYNC_ALL;
4611da177e4SLinus Torvalds 
462ae8547b0SHans Reiser 	spin_lock(&inode_lock);
4631da177e4SLinus Torvalds 	if (!wbc->for_kupdate || list_empty(&sb->s_io))
4642c136579SFengguang Wu 		queue_io(sb, wbc->older_than_this);
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	while (!list_empty(&sb->s_io)) {
4671da177e4SLinus Torvalds 		struct inode *inode = list_entry(sb->s_io.prev,
4681da177e4SLinus Torvalds 						struct inode, i_list);
4691da177e4SLinus Torvalds 		struct address_space *mapping = inode->i_mapping;
4701da177e4SLinus Torvalds 		struct backing_dev_info *bdi = mapping->backing_dev_info;
4711da177e4SLinus Torvalds 		long pages_skipped;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 		if (!bdi_cap_writeback_dirty(bdi)) {
4749852a0e7SAndrew Morton 			redirty_tail(inode);
4757b0de42dSDavid Howells 			if (sb_is_blkdev_sb(sb)) {
4761da177e4SLinus Torvalds 				/*
4771da177e4SLinus Torvalds 				 * Dirty memory-backed blockdev: the ramdisk
4781da177e4SLinus Torvalds 				 * driver does this.  Skip just this inode
4791da177e4SLinus Torvalds 				 */
4801da177e4SLinus Torvalds 				continue;
4811da177e4SLinus Torvalds 			}
4821da177e4SLinus Torvalds 			/*
4831da177e4SLinus Torvalds 			 * Dirty memory-backed inode against a filesystem other
4841da177e4SLinus Torvalds 			 * than the kernel-internal bdev filesystem.  Skip the
4851da177e4SLinus Torvalds 			 * entire superblock.
4861da177e4SLinus Torvalds 			 */
4871da177e4SLinus Torvalds 			break;
4881da177e4SLinus Torvalds 		}
4891da177e4SLinus Torvalds 
4907ef0d737SNick Piggin 		if (inode->i_state & I_NEW) {
4917ef0d737SNick Piggin 			requeue_io(inode);
4927ef0d737SNick Piggin 			continue;
4937ef0d737SNick Piggin 		}
4947ef0d737SNick Piggin 
4951da177e4SLinus Torvalds 		if (wbc->nonblocking && bdi_write_congested(bdi)) {
4961da177e4SLinus Torvalds 			wbc->encountered_congestion = 1;
4977b0de42dSDavid Howells 			if (!sb_is_blkdev_sb(sb))
4981da177e4SLinus Torvalds 				break;		/* Skip a congested fs */
4990e0f4fc2SKen Chen 			requeue_io(inode);
5001da177e4SLinus Torvalds 			continue;		/* Skip a congested blockdev */
5011da177e4SLinus Torvalds 		}
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 		if (wbc->bdi && bdi != wbc->bdi) {
5047b0de42dSDavid Howells 			if (!sb_is_blkdev_sb(sb))
5051da177e4SLinus Torvalds 				break;		/* fs has the wrong queue */
5060e0f4fc2SKen Chen 			requeue_io(inode);
5071da177e4SLinus Torvalds 			continue;		/* blockdev has wrong queue */
5081da177e4SLinus Torvalds 		}
5091da177e4SLinus Torvalds 
510d2caa3c5SJeff Layton 		/*
511d2caa3c5SJeff Layton 		 * Was this inode dirtied after sync_sb_inodes was called?
512d2caa3c5SJeff Layton 		 * This keeps sync from extra jobs and livelock.
513d2caa3c5SJeff Layton 		 */
514d2caa3c5SJeff Layton 		if (inode_dirtied_after(inode, start))
5151da177e4SLinus Torvalds 			break;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 		/* Is another pdflush already flushing this queue? */
5181da177e4SLinus Torvalds 		if (current_is_pdflush() && !writeback_acquire(bdi))
5191da177e4SLinus Torvalds 			break;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 		BUG_ON(inode->i_state & I_FREEING);
5221da177e4SLinus Torvalds 		__iget(inode);
5231da177e4SLinus Torvalds 		pages_skipped = wbc->pages_skipped;
5241da177e4SLinus Torvalds 		__writeback_single_inode(inode, wbc);
5251da177e4SLinus Torvalds 		if (current_is_pdflush())
5261da177e4SLinus Torvalds 			writeback_release(bdi);
5271da177e4SLinus Torvalds 		if (wbc->pages_skipped != pages_skipped) {
5281da177e4SLinus Torvalds 			/*
5291da177e4SLinus Torvalds 			 * writeback is not making progress due to locked
5301da177e4SLinus Torvalds 			 * buffers.  Skip this inode for now.
5311da177e4SLinus Torvalds 			 */
532f57b9b7bSAndrew Morton 			redirty_tail(inode);
5331da177e4SLinus Torvalds 		}
5341da177e4SLinus Torvalds 		spin_unlock(&inode_lock);
5351da177e4SLinus Torvalds 		iput(inode);
5364ffc8444SOGAWA Hirofumi 		cond_resched();
5371da177e4SLinus Torvalds 		spin_lock(&inode_lock);
5388bc3be27SFengguang Wu 		if (wbc->nr_to_write <= 0) {
5398bc3be27SFengguang Wu 			wbc->more_io = 1;
5401da177e4SLinus Torvalds 			break;
5411da177e4SLinus Torvalds 		}
5428bc3be27SFengguang Wu 		if (!list_empty(&sb->s_more_io))
5438bc3be27SFengguang Wu 			wbc->more_io = 1;
5448bc3be27SFengguang Wu 	}
54538f21977SNick Piggin 
54638f21977SNick Piggin 	if (sync) {
54738f21977SNick Piggin 		struct inode *inode, *old_inode = NULL;
54838f21977SNick Piggin 
54938f21977SNick Piggin 		/*
55038f21977SNick Piggin 		 * Data integrity sync. Must wait for all pages under writeback,
55138f21977SNick Piggin 		 * because there may have been pages dirtied before our sync
55238f21977SNick Piggin 		 * call, but which had writeout started before we write it out.
55338f21977SNick Piggin 		 * In which case, the inode may not be on the dirty list, but
55438f21977SNick Piggin 		 * we still have to wait for that writeout.
55538f21977SNick Piggin 		 */
55638f21977SNick Piggin 		list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
55738f21977SNick Piggin 			struct address_space *mapping;
55838f21977SNick Piggin 
559b6fac63cSWu Fengguang 			if (inode->i_state &
560b6fac63cSWu Fengguang 					(I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
56138f21977SNick Piggin 				continue;
56238f21977SNick Piggin 			mapping = inode->i_mapping;
56338f21977SNick Piggin 			if (mapping->nrpages == 0)
56438f21977SNick Piggin 				continue;
56538f21977SNick Piggin 			__iget(inode);
566ae8547b0SHans Reiser 			spin_unlock(&inode_lock);
56738f21977SNick Piggin 			/*
56838f21977SNick Piggin 			 * We hold a reference to 'inode' so it couldn't have
56938f21977SNick Piggin 			 * been removed from s_inodes list while we dropped the
57038f21977SNick Piggin 			 * inode_lock.  We cannot iput the inode now as we can
57138f21977SNick Piggin 			 * be holding the last reference and we cannot iput it
57238f21977SNick Piggin 			 * under inode_lock. So we keep the reference and iput
57338f21977SNick Piggin 			 * it later.
57438f21977SNick Piggin 			 */
57538f21977SNick Piggin 			iput(old_inode);
57638f21977SNick Piggin 			old_inode = inode;
57738f21977SNick Piggin 
57838f21977SNick Piggin 			filemap_fdatawait(mapping);
57938f21977SNick Piggin 
58038f21977SNick Piggin 			cond_resched();
58138f21977SNick Piggin 
58238f21977SNick Piggin 			spin_lock(&inode_lock);
58338f21977SNick Piggin 		}
58438f21977SNick Piggin 		spin_unlock(&inode_lock);
58538f21977SNick Piggin 		iput(old_inode);
58638f21977SNick Piggin 	} else
58738f21977SNick Piggin 		spin_unlock(&inode_lock);
58838f21977SNick Piggin 
5891da177e4SLinus Torvalds 	return;		/* Leave any unwritten inodes on s_io */
5901da177e4SLinus Torvalds }
5914ee6afd3SArtem Bityutskiy EXPORT_SYMBOL_GPL(generic_sync_sb_inodes);
5924ee6afd3SArtem Bityutskiy 
5934ee6afd3SArtem Bityutskiy static void sync_sb_inodes(struct super_block *sb,
5944ee6afd3SArtem Bityutskiy 				struct writeback_control *wbc)
5954ee6afd3SArtem Bityutskiy {
5964ee6afd3SArtem Bityutskiy 	generic_sync_sb_inodes(sb, wbc);
5974ee6afd3SArtem Bityutskiy }
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds /*
6001da177e4SLinus Torvalds  * Start writeback of dirty pagecache data against all unlocked inodes.
6011da177e4SLinus Torvalds  *
6021da177e4SLinus Torvalds  * Note:
6031da177e4SLinus Torvalds  * We don't need to grab a reference to superblock here. If it has non-empty
6041da177e4SLinus Torvalds  * ->s_dirty it's hadn't been killed yet and kill_super() won't proceed
6052c136579SFengguang Wu  * past sync_inodes_sb() until the ->s_dirty/s_io/s_more_io lists are all
6061da177e4SLinus Torvalds  * empty. Since __sync_single_inode() regains inode_lock before it finally moves
6071da177e4SLinus Torvalds  * inode from superblock lists we are OK.
6081da177e4SLinus Torvalds  *
6091da177e4SLinus Torvalds  * If `older_than_this' is non-zero then only flush inodes which have a
6101da177e4SLinus Torvalds  * flushtime older than *older_than_this.
6111da177e4SLinus Torvalds  *
6121da177e4SLinus Torvalds  * If `bdi' is non-zero then we will scan the first inode against each
6131da177e4SLinus Torvalds  * superblock until we find the matching ones.  One group will be the dirty
6141da177e4SLinus Torvalds  * inodes against a filesystem.  Then when we hit the dummy blockdev superblock,
6151da177e4SLinus Torvalds  * sync_sb_inodes will seekout the blockdev which matches `bdi'.  Maybe not
6161da177e4SLinus Torvalds  * super-efficient but we're about to do a ton of I/O...
6171da177e4SLinus Torvalds  */
6181da177e4SLinus Torvalds void
6191da177e4SLinus Torvalds writeback_inodes(struct writeback_control *wbc)
6201da177e4SLinus Torvalds {
6211da177e4SLinus Torvalds 	struct super_block *sb;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	might_sleep();
6241da177e4SLinus Torvalds 	spin_lock(&sb_lock);
6251da177e4SLinus Torvalds restart:
626797074e4SAkinobu Mita 	list_for_each_entry_reverse(sb, &super_blocks, s_list) {
62708d8e974SFengguang Wu 		if (sb_has_dirty_inodes(sb)) {
6281da177e4SLinus Torvalds 			/* we're making our own get_super here */
6291da177e4SLinus Torvalds 			sb->s_count++;
6301da177e4SLinus Torvalds 			spin_unlock(&sb_lock);
6311da177e4SLinus Torvalds 			/*
6321da177e4SLinus Torvalds 			 * If we can't get the readlock, there's no sense in
6331da177e4SLinus Torvalds 			 * waiting around, most of the time the FS is going to
6341da177e4SLinus Torvalds 			 * be unmounted by the time it is released.
6351da177e4SLinus Torvalds 			 */
6361da177e4SLinus Torvalds 			if (down_read_trylock(&sb->s_umount)) {
637ae8547b0SHans Reiser 				if (sb->s_root)
6381da177e4SLinus Torvalds 					sync_sb_inodes(sb, wbc);
6391da177e4SLinus Torvalds 				up_read(&sb->s_umount);
6401da177e4SLinus Torvalds 			}
6411da177e4SLinus Torvalds 			spin_lock(&sb_lock);
6421da177e4SLinus Torvalds 			if (__put_super_and_need_restart(sb))
6431da177e4SLinus Torvalds 				goto restart;
6441da177e4SLinus Torvalds 		}
6451da177e4SLinus Torvalds 		if (wbc->nr_to_write <= 0)
6461da177e4SLinus Torvalds 			break;
6471da177e4SLinus Torvalds 	}
6481da177e4SLinus Torvalds 	spin_unlock(&sb_lock);
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds /*
6521da177e4SLinus Torvalds  * writeback and wait upon the filesystem's dirty inodes.  The caller will
6534f5a99d6SNick Piggin  * do this in two passes - one to write, and one to wait.
6541da177e4SLinus Torvalds  *
6551da177e4SLinus Torvalds  * A finite limit is set on the number of pages which will be written.
6561da177e4SLinus Torvalds  * To prevent infinite livelock of sys_sync().
6571da177e4SLinus Torvalds  *
6581da177e4SLinus Torvalds  * We add in the number of potentially dirty inodes, because each inode write
6591da177e4SLinus Torvalds  * can dirty pagecache in the underlying blockdev.
6601da177e4SLinus Torvalds  */
6611da177e4SLinus Torvalds void sync_inodes_sb(struct super_block *sb, int wait)
6621da177e4SLinus Torvalds {
6631da177e4SLinus Torvalds 	struct writeback_control wbc = {
6644f5a99d6SNick Piggin 		.sync_mode	= wait ? WB_SYNC_ALL : WB_SYNC_NONE,
665111ebb6eSOGAWA Hirofumi 		.range_start	= 0,
666111ebb6eSOGAWA Hirofumi 		.range_end	= LLONG_MAX,
6671da177e4SLinus Torvalds 	};
66838f21977SNick Piggin 
66938f21977SNick Piggin 	if (!wait) {
670b1e7a8fdSChristoph Lameter 		unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
671fd39fc85SChristoph Lameter 		unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 		wbc.nr_to_write = nr_dirty + nr_unstable +
67438f21977SNick Piggin 			(inodes_stat.nr_inodes - inodes_stat.nr_unused);
67538f21977SNick Piggin 	} else
67638f21977SNick Piggin 		wbc.nr_to_write = LONG_MAX; /* doesn't actually matter */
67738f21977SNick Piggin 
6781da177e4SLinus Torvalds 	sync_sb_inodes(sb, &wbc);
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds /**
68267be2dd1SMartin Waitz  * sync_inodes - writes all inodes to disk
68367be2dd1SMartin Waitz  * @wait: wait for completion
6841da177e4SLinus Torvalds  *
6851da177e4SLinus Torvalds  * sync_inodes() goes through each super block's dirty inode list, writes the
6861da177e4SLinus Torvalds  * inodes out, waits on the writeout and puts the inodes back on the normal
6871da177e4SLinus Torvalds  * list.
6881da177e4SLinus Torvalds  *
6891da177e4SLinus Torvalds  * This is for sys_sync().  fsync_dev() uses the same algorithm.  The subtle
6901da177e4SLinus Torvalds  * part of the sync functions is that the blockdev "superblock" is processed
6911da177e4SLinus Torvalds  * last.  This is because the write_inode() function of a typical fs will
6921da177e4SLinus Torvalds  * perform no I/O, but will mark buffers in the blockdev mapping as dirty.
6931da177e4SLinus Torvalds  * What we want to do is to perform all that dirtying first, and then write
6941da177e4SLinus Torvalds  * back all those inode blocks via the blockdev mapping in one sweep.  So the
6951da177e4SLinus Torvalds  * additional (somewhat redundant) sync_blockdev() calls here are to make
6961da177e4SLinus Torvalds  * sure that really happens.  Because if we call sync_inodes_sb(wait=1) with
6971da177e4SLinus Torvalds  * outstanding dirty inodes, the writeback goes block-at-a-time within the
6981da177e4SLinus Torvalds  * filesystem's write_inode().  This is extremely slow.
6991da177e4SLinus Torvalds  */
700618f0636SKirill Korotaev static void __sync_inodes(int wait)
7011da177e4SLinus Torvalds {
7021da177e4SLinus Torvalds 	struct super_block *sb;
7031da177e4SLinus Torvalds 
704618f0636SKirill Korotaev 	spin_lock(&sb_lock);
705618f0636SKirill Korotaev restart:
706618f0636SKirill Korotaev 	list_for_each_entry(sb, &super_blocks, s_list) {
707618f0636SKirill Korotaev 		sb->s_count++;
708618f0636SKirill Korotaev 		spin_unlock(&sb_lock);
709618f0636SKirill Korotaev 		down_read(&sb->s_umount);
710618f0636SKirill Korotaev 		if (sb->s_root) {
711618f0636SKirill Korotaev 			sync_inodes_sb(sb, wait);
7121da177e4SLinus Torvalds 			sync_blockdev(sb->s_bdev);
7131da177e4SLinus Torvalds 		}
714618f0636SKirill Korotaev 		up_read(&sb->s_umount);
715618f0636SKirill Korotaev 		spin_lock(&sb_lock);
716618f0636SKirill Korotaev 		if (__put_super_and_need_restart(sb))
717618f0636SKirill Korotaev 			goto restart;
718618f0636SKirill Korotaev 	}
719618f0636SKirill Korotaev 	spin_unlock(&sb_lock);
720618f0636SKirill Korotaev }
721618f0636SKirill Korotaev 
722618f0636SKirill Korotaev void sync_inodes(int wait)
723618f0636SKirill Korotaev {
724618f0636SKirill Korotaev 	__sync_inodes(0);
725618f0636SKirill Korotaev 
726856bf4d7SNick Piggin 	if (wait)
727618f0636SKirill Korotaev 		__sync_inodes(1);
7281da177e4SLinus Torvalds }
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds /**
7311da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
7321da177e4SLinus Torvalds  * @inode: inode to write to disk
7331da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
7341da177e4SLinus Torvalds  *
7357f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
7367f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
7377f04c26dSAndrea Arcangeli  *
7387f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
7391da177e4SLinus Torvalds  */
7401da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
7411da177e4SLinus Torvalds {
7421da177e4SLinus Torvalds 	int ret;
7431da177e4SLinus Torvalds 	struct writeback_control wbc = {
7441da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
74518914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
746111ebb6eSOGAWA Hirofumi 		.range_start = 0,
747111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
7481da177e4SLinus Torvalds 	};
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
75149364ce2SAndrew Morton 		wbc.nr_to_write = 0;
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	might_sleep();
7541da177e4SLinus Torvalds 	spin_lock(&inode_lock);
7551da177e4SLinus Torvalds 	ret = __writeback_single_inode(inode, &wbc);
7561da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
7571da177e4SLinus Torvalds 	if (sync)
7581c0eeaf5SJoern Engel 		inode_sync_wait(inode);
7591da177e4SLinus Torvalds 	return ret;
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds /**
7641da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
7651da177e4SLinus Torvalds  * @inode: the inode to sync
7661da177e4SLinus Torvalds  * @wbc: controls the writeback mode
7671da177e4SLinus Torvalds  *
7681da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
7691da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
7701da177e4SLinus Torvalds  * update inode->i_state.
7711da177e4SLinus Torvalds  *
7721da177e4SLinus Torvalds  * The caller must have a ref on the inode.
7731da177e4SLinus Torvalds  */
7741da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
7751da177e4SLinus Torvalds {
7761da177e4SLinus Torvalds 	int ret;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	spin_lock(&inode_lock);
7791da177e4SLinus Torvalds 	ret = __writeback_single_inode(inode, wbc);
7801da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
7811da177e4SLinus Torvalds 	return ret;
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds /**
7861da177e4SLinus Torvalds  * generic_osync_inode - flush all dirty data for a given inode to disk
7871da177e4SLinus Torvalds  * @inode: inode to write
78867be2dd1SMartin Waitz  * @mapping: the address_space that should be flushed
7891da177e4SLinus Torvalds  * @what:  what to write and wait upon
7901da177e4SLinus Torvalds  *
7911da177e4SLinus Torvalds  * This can be called by file_write functions for files which have the
7921da177e4SLinus Torvalds  * O_SYNC flag set, to flush dirty writes to disk.
7931da177e4SLinus Torvalds  *
7941da177e4SLinus Torvalds  * @what is a bitmask, specifying which part of the inode's data should be
795b8887e6eSRandy Dunlap  * written and waited upon.
7961da177e4SLinus Torvalds  *
7971da177e4SLinus Torvalds  *    OSYNC_DATA:     i_mapping's dirty data
7981da177e4SLinus Torvalds  *    OSYNC_METADATA: the buffers at i_mapping->private_list
7991da177e4SLinus Torvalds  *    OSYNC_INODE:    the inode itself
8001da177e4SLinus Torvalds  */
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds int generic_osync_inode(struct inode *inode, struct address_space *mapping, int what)
8031da177e4SLinus Torvalds {
8041da177e4SLinus Torvalds 	int err = 0;
8051da177e4SLinus Torvalds 	int need_write_inode_now = 0;
8061da177e4SLinus Torvalds 	int err2;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	if (what & OSYNC_DATA)
8091da177e4SLinus Torvalds 		err = filemap_fdatawrite(mapping);
8101da177e4SLinus Torvalds 	if (what & (OSYNC_METADATA|OSYNC_DATA)) {
8111da177e4SLinus Torvalds 		err2 = sync_mapping_buffers(mapping);
8121da177e4SLinus Torvalds 		if (!err)
8131da177e4SLinus Torvalds 			err = err2;
8141da177e4SLinus Torvalds 	}
8151da177e4SLinus Torvalds 	if (what & OSYNC_DATA) {
8161da177e4SLinus Torvalds 		err2 = filemap_fdatawait(mapping);
8171da177e4SLinus Torvalds 		if (!err)
8181da177e4SLinus Torvalds 			err = err2;
8191da177e4SLinus Torvalds 	}
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	spin_lock(&inode_lock);
8221da177e4SLinus Torvalds 	if ((inode->i_state & I_DIRTY) &&
8231da177e4SLinus Torvalds 	    ((what & OSYNC_INODE) || (inode->i_state & I_DIRTY_DATASYNC)))
8241da177e4SLinus Torvalds 		need_write_inode_now = 1;
8251da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	if (need_write_inode_now) {
8281da177e4SLinus Torvalds 		err2 = write_inode_now(inode, 1);
8291da177e4SLinus Torvalds 		if (!err)
8301da177e4SLinus Torvalds 			err = err2;
8311da177e4SLinus Torvalds 	}
8321da177e4SLinus Torvalds 	else
8331c0eeaf5SJoern Engel 		inode_sync_wait(inode);
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	return err;
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds EXPORT_SYMBOL(generic_osync_inode);
838