xref: /openbmc/linux/fs/fs-writeback.c (revision e1f8e874)
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);
1996610a0bcSAndrew Morton 		if (!time_after_eq(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 
223c986d1e2SAndrew Morton /*
2242c136579SFengguang Wu  * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
2252c136579SFengguang Wu  */
2262c136579SFengguang Wu static void move_expired_inodes(struct list_head *delaying_queue,
2272c136579SFengguang Wu 			       struct list_head *dispatch_queue,
2282c136579SFengguang Wu 				unsigned long *older_than_this)
2292c136579SFengguang Wu {
2302c136579SFengguang Wu 	while (!list_empty(delaying_queue)) {
2312c136579SFengguang Wu 		struct inode *inode = list_entry(delaying_queue->prev,
2322c136579SFengguang Wu 						struct inode, i_list);
2332c136579SFengguang Wu 		if (older_than_this &&
2342c136579SFengguang Wu 			time_after(inode->dirtied_when, *older_than_this))
2352c136579SFengguang Wu 			break;
2362c136579SFengguang Wu 		list_move(&inode->i_list, dispatch_queue);
2372c136579SFengguang Wu 	}
2382c136579SFengguang Wu }
2392c136579SFengguang Wu 
2402c136579SFengguang Wu /*
2412c136579SFengguang Wu  * Queue all expired dirty inodes for io, eldest first.
2422c136579SFengguang Wu  */
2432c136579SFengguang Wu static void queue_io(struct super_block *sb,
2442c136579SFengguang Wu 				unsigned long *older_than_this)
2452c136579SFengguang Wu {
2462c136579SFengguang Wu 	list_splice_init(&sb->s_more_io, sb->s_io.prev);
2472c136579SFengguang Wu 	move_expired_inodes(&sb->s_dirty, &sb->s_io, older_than_this);
2482c136579SFengguang Wu }
2492c136579SFengguang Wu 
25008d8e974SFengguang Wu int sb_has_dirty_inodes(struct super_block *sb)
25108d8e974SFengguang Wu {
25208d8e974SFengguang Wu 	return !list_empty(&sb->s_dirty) ||
25308d8e974SFengguang Wu 	       !list_empty(&sb->s_io) ||
25408d8e974SFengguang Wu 	       !list_empty(&sb->s_more_io);
25508d8e974SFengguang Wu }
25608d8e974SFengguang Wu EXPORT_SYMBOL(sb_has_dirty_inodes);
25708d8e974SFengguang Wu 
2582c136579SFengguang Wu /*
2591da177e4SLinus Torvalds  * Write a single inode's dirty pages and inode data out to disk.
2601da177e4SLinus Torvalds  * If `wait' is set, wait on the writeout.
2611da177e4SLinus Torvalds  *
2621da177e4SLinus Torvalds  * The whole writeout design is quite complex and fragile.  We want to avoid
2631da177e4SLinus Torvalds  * starvation of particular inodes when others are being redirtied, prevent
2641da177e4SLinus Torvalds  * livelocks, etc.
2651da177e4SLinus Torvalds  *
2661da177e4SLinus Torvalds  * Called under inode_lock.
2671da177e4SLinus Torvalds  */
2681da177e4SLinus Torvalds static int
2691da177e4SLinus Torvalds __sync_single_inode(struct inode *inode, struct writeback_control *wbc)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	unsigned dirty;
2721da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
2731da177e4SLinus Torvalds 	int wait = wbc->sync_mode == WB_SYNC_ALL;
2741da177e4SLinus Torvalds 	int ret;
2751da177e4SLinus Torvalds 
2761c0eeaf5SJoern Engel 	BUG_ON(inode->i_state & I_SYNC);
2771da177e4SLinus Torvalds 
2781c0eeaf5SJoern Engel 	/* Set I_SYNC, reset I_DIRTY */
2791da177e4SLinus Torvalds 	dirty = inode->i_state & I_DIRTY;
2801c0eeaf5SJoern Engel 	inode->i_state |= I_SYNC;
2811da177e4SLinus Torvalds 	inode->i_state &= ~I_DIRTY;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	ret = do_writepages(mapping, wbc);
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	/* Don't write the inode if only I_DIRTY_PAGES was set */
2881da177e4SLinus Torvalds 	if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
2891da177e4SLinus Torvalds 		int err = write_inode(inode, wait);
2901da177e4SLinus Torvalds 		if (ret == 0)
2911da177e4SLinus Torvalds 			ret = err;
2921da177e4SLinus Torvalds 	}
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	if (wait) {
2951da177e4SLinus Torvalds 		int err = filemap_fdatawait(mapping);
2961da177e4SLinus Torvalds 		if (ret == 0)
2971da177e4SLinus Torvalds 			ret = err;
2981da177e4SLinus Torvalds 	}
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	spin_lock(&inode_lock);
3011c0eeaf5SJoern Engel 	inode->i_state &= ~I_SYNC;
3021da177e4SLinus Torvalds 	if (!(inode->i_state & I_FREEING)) {
3031da177e4SLinus Torvalds 		if (!(inode->i_state & I_DIRTY) &&
3041da177e4SLinus Torvalds 		    mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
3051da177e4SLinus Torvalds 			/*
3061da177e4SLinus Torvalds 			 * We didn't write back all the pages.  nfs_writepages()
3071da177e4SLinus Torvalds 			 * sometimes bales out without doing anything. Redirty
3082c136579SFengguang Wu 			 * the inode; Move it from s_io onto s_more_io/s_dirty.
3091b43ef91SAndrew Morton 			 */
3101b43ef91SAndrew Morton 			/*
3111b43ef91SAndrew Morton 			 * akpm: if the caller was the kupdate function we put
3121b43ef91SAndrew Morton 			 * this inode at the head of s_dirty so it gets first
3131b43ef91SAndrew Morton 			 * consideration.  Otherwise, move it to the tail, for
3141b43ef91SAndrew Morton 			 * the reasons described there.  I'm not really sure
3151b43ef91SAndrew Morton 			 * how much sense this makes.  Presumably I had a good
3161b43ef91SAndrew Morton 			 * reasons for doing it this way, and I'd rather not
3171b43ef91SAndrew Morton 			 * muck with it at present.
3181da177e4SLinus Torvalds 			 */
3191da177e4SLinus Torvalds 			if (wbc->for_kupdate) {
3201da177e4SLinus Torvalds 				/*
3212c136579SFengguang Wu 				 * For the kupdate function we move the inode
3222c136579SFengguang Wu 				 * to s_more_io so it will get more writeout as
3232c136579SFengguang Wu 				 * soon as the queue becomes uncongested.
3241da177e4SLinus Torvalds 				 */
3251da177e4SLinus Torvalds 				inode->i_state |= I_DIRTY_PAGES;
3268bc3be27SFengguang Wu 				if (wbc->nr_to_write <= 0) {
3278bc3be27SFengguang Wu 					/*
3288bc3be27SFengguang Wu 					 * slice used up: queue for next turn
3298bc3be27SFengguang Wu 					 */
3300e0f4fc2SKen Chen 					requeue_io(inode);
3311da177e4SLinus Torvalds 				} else {
3321da177e4SLinus Torvalds 					/*
3338bc3be27SFengguang Wu 					 * somehow blocked: retry later
3348bc3be27SFengguang Wu 					 */
3358bc3be27SFengguang Wu 					redirty_tail(inode);
3368bc3be27SFengguang Wu 				}
3378bc3be27SFengguang Wu 			} else {
3388bc3be27SFengguang Wu 				/*
3391da177e4SLinus Torvalds 				 * Otherwise fully redirty the inode so that
3401da177e4SLinus Torvalds 				 * other inodes on this superblock will get some
3411da177e4SLinus Torvalds 				 * writeout.  Otherwise heavy writing to one
3421da177e4SLinus Torvalds 				 * file would indefinitely suspend writeout of
3431da177e4SLinus Torvalds 				 * all the other files.
3441da177e4SLinus Torvalds 				 */
3451da177e4SLinus Torvalds 				inode->i_state |= I_DIRTY_PAGES;
3461b43ef91SAndrew Morton 				redirty_tail(inode);
3471da177e4SLinus Torvalds 			}
3481da177e4SLinus Torvalds 		} else if (inode->i_state & I_DIRTY) {
3491da177e4SLinus Torvalds 			/*
3501da177e4SLinus Torvalds 			 * Someone redirtied the inode while were writing back
3511da177e4SLinus Torvalds 			 * the pages.
3521da177e4SLinus Torvalds 			 */
3536610a0bcSAndrew Morton 			redirty_tail(inode);
3541da177e4SLinus Torvalds 		} else if (atomic_read(&inode->i_count)) {
3551da177e4SLinus Torvalds 			/*
3561da177e4SLinus Torvalds 			 * The inode is clean, inuse
3571da177e4SLinus Torvalds 			 */
3581da177e4SLinus Torvalds 			list_move(&inode->i_list, &inode_in_use);
3591da177e4SLinus Torvalds 		} else {
3601da177e4SLinus Torvalds 			/*
3611da177e4SLinus Torvalds 			 * The inode is clean, unused
3621da177e4SLinus Torvalds 			 */
3631da177e4SLinus Torvalds 			list_move(&inode->i_list, &inode_unused);
3641da177e4SLinus Torvalds 		}
3651da177e4SLinus Torvalds 	}
3661c0eeaf5SJoern Engel 	inode_sync_complete(inode);
3671da177e4SLinus Torvalds 	return ret;
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds /*
3717f04c26dSAndrea Arcangeli  * Write out an inode's dirty pages.  Called under inode_lock.  Either the
3727f04c26dSAndrea Arcangeli  * caller has ref on the inode (either via __iget or via syscall against an fd)
3737f04c26dSAndrea Arcangeli  * or the inode has I_WILL_FREE set (via generic_forget_inode)
3741da177e4SLinus Torvalds  */
3751da177e4SLinus Torvalds static int
3767f04c26dSAndrea Arcangeli __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	wait_queue_head_t *wqh;
3791da177e4SLinus Torvalds 
3807f04c26dSAndrea Arcangeli 	if (!atomic_read(&inode->i_count))
381659603efSAndrea Arcangeli 		WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
3827f04c26dSAndrea Arcangeli 	else
3837f04c26dSAndrea Arcangeli 		WARN_ON(inode->i_state & I_WILL_FREE);
3847f04c26dSAndrea Arcangeli 
3851c0eeaf5SJoern Engel 	if ((wbc->sync_mode != WB_SYNC_ALL) && (inode->i_state & I_SYNC)) {
38665cb9b47SAndrew Morton 		/*
38765cb9b47SAndrew Morton 		 * We're skipping this inode because it's locked, and we're not
3882c136579SFengguang Wu 		 * doing writeback-for-data-integrity.  Move it to s_more_io so
3892c136579SFengguang Wu 		 * that writeback can proceed with the other inodes on s_io.
3902c136579SFengguang Wu 		 * We'll have another go at writing back this inode when we
3912c136579SFengguang Wu 		 * completed a full scan of s_io.
39265cb9b47SAndrew Morton 		 */
3930e0f4fc2SKen Chen 		requeue_io(inode);
3942d544564SQi Yong 		return 0;
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	/*
3981da177e4SLinus Torvalds 	 * It's a data-integrity sync.  We must wait.
3991da177e4SLinus Torvalds 	 */
4001c0eeaf5SJoern Engel 	if (inode->i_state & I_SYNC) {
4011c0eeaf5SJoern Engel 		DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
4021da177e4SLinus Torvalds 
4031c0eeaf5SJoern Engel 		wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
4041da177e4SLinus Torvalds 		do {
4051da177e4SLinus Torvalds 			spin_unlock(&inode_lock);
4061da177e4SLinus Torvalds 			__wait_on_bit(wqh, &wq, inode_wait,
4071da177e4SLinus Torvalds 							TASK_UNINTERRUPTIBLE);
4081da177e4SLinus Torvalds 			spin_lock(&inode_lock);
4091c0eeaf5SJoern Engel 		} while (inode->i_state & I_SYNC);
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds 	return __sync_single_inode(inode, wbc);
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds /*
4151da177e4SLinus Torvalds  * Write out a superblock's list of dirty inodes.  A wait will be performed
4161da177e4SLinus Torvalds  * upon no inodes, all inodes or the final one, depending upon sync_mode.
4171da177e4SLinus Torvalds  *
4181da177e4SLinus Torvalds  * If older_than_this is non-NULL, then only write out inodes which
4191da177e4SLinus Torvalds  * had their first dirtying at a time earlier than *older_than_this.
4201da177e4SLinus Torvalds  *
4211da177e4SLinus Torvalds  * If we're a pdlfush thread, then implement pdflush collision avoidance
4221da177e4SLinus Torvalds  * against the entire list.
4231da177e4SLinus Torvalds  *
4241da177e4SLinus Torvalds  * WB_SYNC_HOLD is a hack for sys_sync(): reattach the inode to sb->s_dirty so
4251da177e4SLinus Torvalds  * that it can be located for waiting on in __writeback_single_inode().
4261da177e4SLinus Torvalds  *
4271da177e4SLinus Torvalds  * If `bdi' is non-zero then we're being asked to writeback a specific queue.
4281da177e4SLinus Torvalds  * This function assumes that the blockdev superblock's inodes are backed by
4291da177e4SLinus Torvalds  * a variety of queues, so all inodes are searched.  For other superblocks,
4301da177e4SLinus Torvalds  * assume that all inodes are backed by the same queue.
4311da177e4SLinus Torvalds  *
4321da177e4SLinus Torvalds  * FIXME: this linear search could get expensive with many fileystems.  But
4331da177e4SLinus Torvalds  * how to fix?  We need to go from an address_space to all inodes which share
4341da177e4SLinus Torvalds  * a queue with that address_space.  (Easy: have a global "dirty superblocks"
4351da177e4SLinus Torvalds  * list).
4361da177e4SLinus Torvalds  *
4371da177e4SLinus Torvalds  * The inodes to be written are parked on sb->s_io.  They are moved back onto
4381da177e4SLinus Torvalds  * sb->s_dirty as they are selected for writing.  This way, none can be missed
4391da177e4SLinus Torvalds  * on the writer throttling path, and we get decent balancing between many
4401c0eeaf5SJoern Engel  * throttled threads: we don't want them all piling up on inode_sync_wait.
4411da177e4SLinus Torvalds  */
4424ee6afd3SArtem Bityutskiy void generic_sync_sb_inodes(struct super_block *sb,
4434ee6afd3SArtem Bityutskiy 				struct writeback_control *wbc)
4441da177e4SLinus Torvalds {
4451da177e4SLinus Torvalds 	const unsigned long start = jiffies;	/* livelock avoidance */
4461da177e4SLinus Torvalds 
447ae8547b0SHans Reiser 	spin_lock(&inode_lock);
4481da177e4SLinus Torvalds 	if (!wbc->for_kupdate || list_empty(&sb->s_io))
4492c136579SFengguang Wu 		queue_io(sb, wbc->older_than_this);
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	while (!list_empty(&sb->s_io)) {
4521da177e4SLinus Torvalds 		struct inode *inode = list_entry(sb->s_io.prev,
4531da177e4SLinus Torvalds 						struct inode, i_list);
4541da177e4SLinus Torvalds 		struct address_space *mapping = inode->i_mapping;
4551da177e4SLinus Torvalds 		struct backing_dev_info *bdi = mapping->backing_dev_info;
4561da177e4SLinus Torvalds 		long pages_skipped;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 		if (!bdi_cap_writeback_dirty(bdi)) {
4599852a0e7SAndrew Morton 			redirty_tail(inode);
4607b0de42dSDavid Howells 			if (sb_is_blkdev_sb(sb)) {
4611da177e4SLinus Torvalds 				/*
4621da177e4SLinus Torvalds 				 * Dirty memory-backed blockdev: the ramdisk
4631da177e4SLinus Torvalds 				 * driver does this.  Skip just this inode
4641da177e4SLinus Torvalds 				 */
4651da177e4SLinus Torvalds 				continue;
4661da177e4SLinus Torvalds 			}
4671da177e4SLinus Torvalds 			/*
4681da177e4SLinus Torvalds 			 * Dirty memory-backed inode against a filesystem other
4691da177e4SLinus Torvalds 			 * than the kernel-internal bdev filesystem.  Skip the
4701da177e4SLinus Torvalds 			 * entire superblock.
4711da177e4SLinus Torvalds 			 */
4721da177e4SLinus Torvalds 			break;
4731da177e4SLinus Torvalds 		}
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 		if (wbc->nonblocking && bdi_write_congested(bdi)) {
4761da177e4SLinus Torvalds 			wbc->encountered_congestion = 1;
4777b0de42dSDavid Howells 			if (!sb_is_blkdev_sb(sb))
4781da177e4SLinus Torvalds 				break;		/* Skip a congested fs */
4790e0f4fc2SKen Chen 			requeue_io(inode);
4801da177e4SLinus Torvalds 			continue;		/* Skip a congested blockdev */
4811da177e4SLinus Torvalds 		}
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 		if (wbc->bdi && bdi != wbc->bdi) {
4847b0de42dSDavid Howells 			if (!sb_is_blkdev_sb(sb))
4851da177e4SLinus Torvalds 				break;		/* fs has the wrong queue */
4860e0f4fc2SKen Chen 			requeue_io(inode);
4871da177e4SLinus Torvalds 			continue;		/* blockdev has wrong queue */
4881da177e4SLinus Torvalds 		}
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 		/* Was this inode dirtied after sync_sb_inodes was called? */
4911da177e4SLinus Torvalds 		if (time_after(inode->dirtied_when, start))
4921da177e4SLinus Torvalds 			break;
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 		/* Is another pdflush already flushing this queue? */
4951da177e4SLinus Torvalds 		if (current_is_pdflush() && !writeback_acquire(bdi))
4961da177e4SLinus Torvalds 			break;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 		BUG_ON(inode->i_state & I_FREEING);
4991da177e4SLinus Torvalds 		__iget(inode);
5001da177e4SLinus Torvalds 		pages_skipped = wbc->pages_skipped;
5011da177e4SLinus Torvalds 		__writeback_single_inode(inode, wbc);
5021da177e4SLinus Torvalds 		if (wbc->sync_mode == WB_SYNC_HOLD) {
5031da177e4SLinus Torvalds 			inode->dirtied_when = jiffies;
5041da177e4SLinus Torvalds 			list_move(&inode->i_list, &sb->s_dirty);
5051da177e4SLinus Torvalds 		}
5061da177e4SLinus Torvalds 		if (current_is_pdflush())
5071da177e4SLinus Torvalds 			writeback_release(bdi);
5081da177e4SLinus Torvalds 		if (wbc->pages_skipped != pages_skipped) {
5091da177e4SLinus Torvalds 			/*
5101da177e4SLinus Torvalds 			 * writeback is not making progress due to locked
5111da177e4SLinus Torvalds 			 * buffers.  Skip this inode for now.
5121da177e4SLinus Torvalds 			 */
513f57b9b7bSAndrew Morton 			redirty_tail(inode);
5141da177e4SLinus Torvalds 		}
5151da177e4SLinus Torvalds 		spin_unlock(&inode_lock);
5161da177e4SLinus Torvalds 		iput(inode);
5174ffc8444SOGAWA Hirofumi 		cond_resched();
5181da177e4SLinus Torvalds 		spin_lock(&inode_lock);
5198bc3be27SFengguang Wu 		if (wbc->nr_to_write <= 0) {
5208bc3be27SFengguang Wu 			wbc->more_io = 1;
5211da177e4SLinus Torvalds 			break;
5221da177e4SLinus Torvalds 		}
5238bc3be27SFengguang Wu 		if (!list_empty(&sb->s_more_io))
5248bc3be27SFengguang Wu 			wbc->more_io = 1;
5258bc3be27SFengguang Wu 	}
526ae8547b0SHans Reiser 	spin_unlock(&inode_lock);
5271da177e4SLinus Torvalds 	return;		/* Leave any unwritten inodes on s_io */
5281da177e4SLinus Torvalds }
5294ee6afd3SArtem Bityutskiy EXPORT_SYMBOL_GPL(generic_sync_sb_inodes);
5304ee6afd3SArtem Bityutskiy 
5314ee6afd3SArtem Bityutskiy static void sync_sb_inodes(struct super_block *sb,
5324ee6afd3SArtem Bityutskiy 				struct writeback_control *wbc)
5334ee6afd3SArtem Bityutskiy {
5344ee6afd3SArtem Bityutskiy 	generic_sync_sb_inodes(sb, wbc);
5354ee6afd3SArtem Bityutskiy }
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds /*
5381da177e4SLinus Torvalds  * Start writeback of dirty pagecache data against all unlocked inodes.
5391da177e4SLinus Torvalds  *
5401da177e4SLinus Torvalds  * Note:
5411da177e4SLinus Torvalds  * We don't need to grab a reference to superblock here. If it has non-empty
5421da177e4SLinus Torvalds  * ->s_dirty it's hadn't been killed yet and kill_super() won't proceed
5432c136579SFengguang Wu  * past sync_inodes_sb() until the ->s_dirty/s_io/s_more_io lists are all
5441da177e4SLinus Torvalds  * empty. Since __sync_single_inode() regains inode_lock before it finally moves
5451da177e4SLinus Torvalds  * inode from superblock lists we are OK.
5461da177e4SLinus Torvalds  *
5471da177e4SLinus Torvalds  * If `older_than_this' is non-zero then only flush inodes which have a
5481da177e4SLinus Torvalds  * flushtime older than *older_than_this.
5491da177e4SLinus Torvalds  *
5501da177e4SLinus Torvalds  * If `bdi' is non-zero then we will scan the first inode against each
5511da177e4SLinus Torvalds  * superblock until we find the matching ones.  One group will be the dirty
5521da177e4SLinus Torvalds  * inodes against a filesystem.  Then when we hit the dummy blockdev superblock,
5531da177e4SLinus Torvalds  * sync_sb_inodes will seekout the blockdev which matches `bdi'.  Maybe not
5541da177e4SLinus Torvalds  * super-efficient but we're about to do a ton of I/O...
5551da177e4SLinus Torvalds  */
5561da177e4SLinus Torvalds void
5571da177e4SLinus Torvalds writeback_inodes(struct writeback_control *wbc)
5581da177e4SLinus Torvalds {
5591da177e4SLinus Torvalds 	struct super_block *sb;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	might_sleep();
5621da177e4SLinus Torvalds 	spin_lock(&sb_lock);
5631da177e4SLinus Torvalds restart:
564797074e4SAkinobu Mita 	list_for_each_entry_reverse(sb, &super_blocks, s_list) {
56508d8e974SFengguang Wu 		if (sb_has_dirty_inodes(sb)) {
5661da177e4SLinus Torvalds 			/* we're making our own get_super here */
5671da177e4SLinus Torvalds 			sb->s_count++;
5681da177e4SLinus Torvalds 			spin_unlock(&sb_lock);
5691da177e4SLinus Torvalds 			/*
5701da177e4SLinus Torvalds 			 * If we can't get the readlock, there's no sense in
5711da177e4SLinus Torvalds 			 * waiting around, most of the time the FS is going to
5721da177e4SLinus Torvalds 			 * be unmounted by the time it is released.
5731da177e4SLinus Torvalds 			 */
5741da177e4SLinus Torvalds 			if (down_read_trylock(&sb->s_umount)) {
575ae8547b0SHans Reiser 				if (sb->s_root)
5761da177e4SLinus Torvalds 					sync_sb_inodes(sb, wbc);
5771da177e4SLinus Torvalds 				up_read(&sb->s_umount);
5781da177e4SLinus Torvalds 			}
5791da177e4SLinus Torvalds 			spin_lock(&sb_lock);
5801da177e4SLinus Torvalds 			if (__put_super_and_need_restart(sb))
5811da177e4SLinus Torvalds 				goto restart;
5821da177e4SLinus Torvalds 		}
5831da177e4SLinus Torvalds 		if (wbc->nr_to_write <= 0)
5841da177e4SLinus Torvalds 			break;
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds 	spin_unlock(&sb_lock);
5871da177e4SLinus Torvalds }
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds /*
5901da177e4SLinus Torvalds  * writeback and wait upon the filesystem's dirty inodes.  The caller will
5911da177e4SLinus Torvalds  * do this in two passes - one to write, and one to wait.  WB_SYNC_HOLD is
5921da177e4SLinus Torvalds  * used to park the written inodes on sb->s_dirty for the wait pass.
5931da177e4SLinus Torvalds  *
5941da177e4SLinus Torvalds  * A finite limit is set on the number of pages which will be written.
5951da177e4SLinus Torvalds  * To prevent infinite livelock of sys_sync().
5961da177e4SLinus Torvalds  *
5971da177e4SLinus Torvalds  * We add in the number of potentially dirty inodes, because each inode write
5981da177e4SLinus Torvalds  * can dirty pagecache in the underlying blockdev.
5991da177e4SLinus Torvalds  */
6001da177e4SLinus Torvalds void sync_inodes_sb(struct super_block *sb, int wait)
6011da177e4SLinus Torvalds {
6021da177e4SLinus Torvalds 	struct writeback_control wbc = {
6031da177e4SLinus Torvalds 		.sync_mode	= wait ? WB_SYNC_ALL : WB_SYNC_HOLD,
604111ebb6eSOGAWA Hirofumi 		.range_start	= 0,
605111ebb6eSOGAWA Hirofumi 		.range_end	= LLONG_MAX,
6061da177e4SLinus Torvalds 	};
607b1e7a8fdSChristoph Lameter 	unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
608fd39fc85SChristoph Lameter 	unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	wbc.nr_to_write = nr_dirty + nr_unstable +
6111da177e4SLinus Torvalds 			(inodes_stat.nr_inodes - inodes_stat.nr_unused) +
6121da177e4SLinus Torvalds 			nr_dirty + nr_unstable;
6131da177e4SLinus Torvalds 	wbc.nr_to_write += wbc.nr_to_write / 2;		/* Bit more for luck */
6141da177e4SLinus Torvalds 	sync_sb_inodes(sb, &wbc);
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds /*
6181da177e4SLinus Torvalds  * Rather lame livelock avoidance.
6191da177e4SLinus Torvalds  */
6201da177e4SLinus Torvalds static void set_sb_syncing(int val)
6211da177e4SLinus Torvalds {
6221da177e4SLinus Torvalds 	struct super_block *sb;
6231da177e4SLinus Torvalds 	spin_lock(&sb_lock);
624797074e4SAkinobu Mita 	list_for_each_entry_reverse(sb, &super_blocks, s_list)
6251da177e4SLinus Torvalds 		sb->s_syncing = val;
6261da177e4SLinus Torvalds 	spin_unlock(&sb_lock);
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds /**
63067be2dd1SMartin Waitz  * sync_inodes - writes all inodes to disk
63167be2dd1SMartin Waitz  * @wait: wait for completion
6321da177e4SLinus Torvalds  *
6331da177e4SLinus Torvalds  * sync_inodes() goes through each super block's dirty inode list, writes the
6341da177e4SLinus Torvalds  * inodes out, waits on the writeout and puts the inodes back on the normal
6351da177e4SLinus Torvalds  * list.
6361da177e4SLinus Torvalds  *
6371da177e4SLinus Torvalds  * This is for sys_sync().  fsync_dev() uses the same algorithm.  The subtle
6381da177e4SLinus Torvalds  * part of the sync functions is that the blockdev "superblock" is processed
6391da177e4SLinus Torvalds  * last.  This is because the write_inode() function of a typical fs will
6401da177e4SLinus Torvalds  * perform no I/O, but will mark buffers in the blockdev mapping as dirty.
6411da177e4SLinus Torvalds  * What we want to do is to perform all that dirtying first, and then write
6421da177e4SLinus Torvalds  * back all those inode blocks via the blockdev mapping in one sweep.  So the
6431da177e4SLinus Torvalds  * additional (somewhat redundant) sync_blockdev() calls here are to make
6441da177e4SLinus Torvalds  * sure that really happens.  Because if we call sync_inodes_sb(wait=1) with
6451da177e4SLinus Torvalds  * outstanding dirty inodes, the writeback goes block-at-a-time within the
6461da177e4SLinus Torvalds  * filesystem's write_inode().  This is extremely slow.
6471da177e4SLinus Torvalds  */
648618f0636SKirill Korotaev static void __sync_inodes(int wait)
6491da177e4SLinus Torvalds {
6501da177e4SLinus Torvalds 	struct super_block *sb;
6511da177e4SLinus Torvalds 
652618f0636SKirill Korotaev 	spin_lock(&sb_lock);
653618f0636SKirill Korotaev restart:
654618f0636SKirill Korotaev 	list_for_each_entry(sb, &super_blocks, s_list) {
655618f0636SKirill Korotaev 		if (sb->s_syncing)
656618f0636SKirill Korotaev 			continue;
657618f0636SKirill Korotaev 		sb->s_syncing = 1;
658618f0636SKirill Korotaev 		sb->s_count++;
659618f0636SKirill Korotaev 		spin_unlock(&sb_lock);
660618f0636SKirill Korotaev 		down_read(&sb->s_umount);
661618f0636SKirill Korotaev 		if (sb->s_root) {
662618f0636SKirill Korotaev 			sync_inodes_sb(sb, wait);
6631da177e4SLinus Torvalds 			sync_blockdev(sb->s_bdev);
6641da177e4SLinus Torvalds 		}
665618f0636SKirill Korotaev 		up_read(&sb->s_umount);
666618f0636SKirill Korotaev 		spin_lock(&sb_lock);
667618f0636SKirill Korotaev 		if (__put_super_and_need_restart(sb))
668618f0636SKirill Korotaev 			goto restart;
669618f0636SKirill Korotaev 	}
670618f0636SKirill Korotaev 	spin_unlock(&sb_lock);
671618f0636SKirill Korotaev }
672618f0636SKirill Korotaev 
673618f0636SKirill Korotaev void sync_inodes(int wait)
674618f0636SKirill Korotaev {
675618f0636SKirill Korotaev 	set_sb_syncing(0);
676618f0636SKirill Korotaev 	__sync_inodes(0);
677618f0636SKirill Korotaev 
6781da177e4SLinus Torvalds 	if (wait) {
6791da177e4SLinus Torvalds 		set_sb_syncing(0);
680618f0636SKirill Korotaev 		__sync_inodes(1);
6811da177e4SLinus Torvalds 	}
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds /**
6851da177e4SLinus Torvalds  * write_inode_now	-	write an inode to disk
6861da177e4SLinus Torvalds  * @inode: inode to write to disk
6871da177e4SLinus Torvalds  * @sync: whether the write should be synchronous or not
6881da177e4SLinus Torvalds  *
6897f04c26dSAndrea Arcangeli  * This function commits an inode to disk immediately if it is dirty. This is
6907f04c26dSAndrea Arcangeli  * primarily needed by knfsd.
6917f04c26dSAndrea Arcangeli  *
6927f04c26dSAndrea Arcangeli  * The caller must either have a ref on the inode or must have set I_WILL_FREE.
6931da177e4SLinus Torvalds  */
6941da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	int ret;
6971da177e4SLinus Torvalds 	struct writeback_control wbc = {
6981da177e4SLinus Torvalds 		.nr_to_write = LONG_MAX,
69918914b18SMike Galbraith 		.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
700111ebb6eSOGAWA Hirofumi 		.range_start = 0,
701111ebb6eSOGAWA Hirofumi 		.range_end = LLONG_MAX,
7021da177e4SLinus Torvalds 	};
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	if (!mapping_cap_writeback_dirty(inode->i_mapping))
70549364ce2SAndrew Morton 		wbc.nr_to_write = 0;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	might_sleep();
7081da177e4SLinus Torvalds 	spin_lock(&inode_lock);
7091da177e4SLinus Torvalds 	ret = __writeback_single_inode(inode, &wbc);
7101da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
7111da177e4SLinus Torvalds 	if (sync)
7121c0eeaf5SJoern Engel 		inode_sync_wait(inode);
7131da177e4SLinus Torvalds 	return ret;
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now);
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds /**
7181da177e4SLinus Torvalds  * sync_inode - write an inode and its pages to disk.
7191da177e4SLinus Torvalds  * @inode: the inode to sync
7201da177e4SLinus Torvalds  * @wbc: controls the writeback mode
7211da177e4SLinus Torvalds  *
7221da177e4SLinus Torvalds  * sync_inode() will write an inode and its pages to disk.  It will also
7231da177e4SLinus Torvalds  * correctly update the inode on its superblock's dirty inode lists and will
7241da177e4SLinus Torvalds  * update inode->i_state.
7251da177e4SLinus Torvalds  *
7261da177e4SLinus Torvalds  * The caller must have a ref on the inode.
7271da177e4SLinus Torvalds  */
7281da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	int ret;
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	spin_lock(&inode_lock);
7331da177e4SLinus Torvalds 	ret = __writeback_single_inode(inode, wbc);
7341da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
7351da177e4SLinus Torvalds 	return ret;
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode);
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds /**
7401da177e4SLinus Torvalds  * generic_osync_inode - flush all dirty data for a given inode to disk
7411da177e4SLinus Torvalds  * @inode: inode to write
74267be2dd1SMartin Waitz  * @mapping: the address_space that should be flushed
7431da177e4SLinus Torvalds  * @what:  what to write and wait upon
7441da177e4SLinus Torvalds  *
7451da177e4SLinus Torvalds  * This can be called by file_write functions for files which have the
7461da177e4SLinus Torvalds  * O_SYNC flag set, to flush dirty writes to disk.
7471da177e4SLinus Torvalds  *
7481da177e4SLinus Torvalds  * @what is a bitmask, specifying which part of the inode's data should be
749b8887e6eSRandy Dunlap  * written and waited upon.
7501da177e4SLinus Torvalds  *
7511da177e4SLinus Torvalds  *    OSYNC_DATA:     i_mapping's dirty data
7521da177e4SLinus Torvalds  *    OSYNC_METADATA: the buffers at i_mapping->private_list
7531da177e4SLinus Torvalds  *    OSYNC_INODE:    the inode itself
7541da177e4SLinus Torvalds  */
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds int generic_osync_inode(struct inode *inode, struct address_space *mapping, int what)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	int err = 0;
7591da177e4SLinus Torvalds 	int need_write_inode_now = 0;
7601da177e4SLinus Torvalds 	int err2;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	if (what & OSYNC_DATA)
7631da177e4SLinus Torvalds 		err = filemap_fdatawrite(mapping);
7641da177e4SLinus Torvalds 	if (what & (OSYNC_METADATA|OSYNC_DATA)) {
7651da177e4SLinus Torvalds 		err2 = sync_mapping_buffers(mapping);
7661da177e4SLinus Torvalds 		if (!err)
7671da177e4SLinus Torvalds 			err = err2;
7681da177e4SLinus Torvalds 	}
7691da177e4SLinus Torvalds 	if (what & OSYNC_DATA) {
7701da177e4SLinus Torvalds 		err2 = filemap_fdatawait(mapping);
7711da177e4SLinus Torvalds 		if (!err)
7721da177e4SLinus Torvalds 			err = err2;
7731da177e4SLinus Torvalds 	}
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	spin_lock(&inode_lock);
7761da177e4SLinus Torvalds 	if ((inode->i_state & I_DIRTY) &&
7771da177e4SLinus Torvalds 	    ((what & OSYNC_INODE) || (inode->i_state & I_DIRTY_DATASYNC)))
7781da177e4SLinus Torvalds 		need_write_inode_now = 1;
7791da177e4SLinus Torvalds 	spin_unlock(&inode_lock);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	if (need_write_inode_now) {
7821da177e4SLinus Torvalds 		err2 = write_inode_now(inode, 1);
7831da177e4SLinus Torvalds 		if (!err)
7841da177e4SLinus Torvalds 			err = err2;
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds 	else
7871c0eeaf5SJoern Engel 		inode_sync_wait(inode);
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	return err;
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds EXPORT_SYMBOL(generic_osync_inode);
792