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 * If `bdi' is non-zero then we're being asked to writeback a specific queue. 4251da177e4SLinus Torvalds * This function assumes that the blockdev superblock's inodes are backed by 4261da177e4SLinus Torvalds * a variety of queues, so all inodes are searched. For other superblocks, 4271da177e4SLinus Torvalds * assume that all inodes are backed by the same queue. 4281da177e4SLinus Torvalds * 4291da177e4SLinus Torvalds * FIXME: this linear search could get expensive with many fileystems. But 4301da177e4SLinus Torvalds * how to fix? We need to go from an address_space to all inodes which share 4311da177e4SLinus Torvalds * a queue with that address_space. (Easy: have a global "dirty superblocks" 4321da177e4SLinus Torvalds * list). 4331da177e4SLinus Torvalds * 4341da177e4SLinus Torvalds * The inodes to be written are parked on sb->s_io. They are moved back onto 4351da177e4SLinus Torvalds * sb->s_dirty as they are selected for writing. This way, none can be missed 4361da177e4SLinus Torvalds * on the writer throttling path, and we get decent balancing between many 4371c0eeaf5SJoern Engel * throttled threads: we don't want them all piling up on inode_sync_wait. 4381da177e4SLinus Torvalds */ 4394ee6afd3SArtem Bityutskiy void generic_sync_sb_inodes(struct super_block *sb, 4404ee6afd3SArtem Bityutskiy struct writeback_control *wbc) 4411da177e4SLinus Torvalds { 4421da177e4SLinus Torvalds const unsigned long start = jiffies; /* livelock avoidance */ 4431da177e4SLinus Torvalds 444ae8547b0SHans Reiser spin_lock(&inode_lock); 4451da177e4SLinus Torvalds if (!wbc->for_kupdate || list_empty(&sb->s_io)) 4462c136579SFengguang Wu queue_io(sb, wbc->older_than_this); 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds while (!list_empty(&sb->s_io)) { 4491da177e4SLinus Torvalds struct inode *inode = list_entry(sb->s_io.prev, 4501da177e4SLinus Torvalds struct inode, i_list); 4511da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 4521da177e4SLinus Torvalds struct backing_dev_info *bdi = mapping->backing_dev_info; 4531da177e4SLinus Torvalds long pages_skipped; 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds if (!bdi_cap_writeback_dirty(bdi)) { 4569852a0e7SAndrew Morton redirty_tail(inode); 4577b0de42dSDavid Howells if (sb_is_blkdev_sb(sb)) { 4581da177e4SLinus Torvalds /* 4591da177e4SLinus Torvalds * Dirty memory-backed blockdev: the ramdisk 4601da177e4SLinus Torvalds * driver does this. Skip just this inode 4611da177e4SLinus Torvalds */ 4621da177e4SLinus Torvalds continue; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds /* 4651da177e4SLinus Torvalds * Dirty memory-backed inode against a filesystem other 4661da177e4SLinus Torvalds * than the kernel-internal bdev filesystem. Skip the 4671da177e4SLinus Torvalds * entire superblock. 4681da177e4SLinus Torvalds */ 4691da177e4SLinus Torvalds break; 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds if (wbc->nonblocking && bdi_write_congested(bdi)) { 4731da177e4SLinus Torvalds wbc->encountered_congestion = 1; 4747b0de42dSDavid Howells if (!sb_is_blkdev_sb(sb)) 4751da177e4SLinus Torvalds break; /* Skip a congested fs */ 4760e0f4fc2SKen Chen requeue_io(inode); 4771da177e4SLinus Torvalds continue; /* Skip a congested blockdev */ 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds if (wbc->bdi && bdi != wbc->bdi) { 4817b0de42dSDavid Howells if (!sb_is_blkdev_sb(sb)) 4821da177e4SLinus Torvalds break; /* fs has the wrong queue */ 4830e0f4fc2SKen Chen requeue_io(inode); 4841da177e4SLinus Torvalds continue; /* blockdev has wrong queue */ 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds /* Was this inode dirtied after sync_sb_inodes was called? */ 4881da177e4SLinus Torvalds if (time_after(inode->dirtied_when, start)) 4891da177e4SLinus Torvalds break; 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /* Is another pdflush already flushing this queue? */ 4921da177e4SLinus Torvalds if (current_is_pdflush() && !writeback_acquire(bdi)) 4931da177e4SLinus Torvalds break; 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds BUG_ON(inode->i_state & I_FREEING); 4961da177e4SLinus Torvalds __iget(inode); 4971da177e4SLinus Torvalds pages_skipped = wbc->pages_skipped; 4981da177e4SLinus Torvalds __writeback_single_inode(inode, wbc); 4991da177e4SLinus Torvalds if (current_is_pdflush()) 5001da177e4SLinus Torvalds writeback_release(bdi); 5011da177e4SLinus Torvalds if (wbc->pages_skipped != pages_skipped) { 5021da177e4SLinus Torvalds /* 5031da177e4SLinus Torvalds * writeback is not making progress due to locked 5041da177e4SLinus Torvalds * buffers. Skip this inode for now. 5051da177e4SLinus Torvalds */ 506f57b9b7bSAndrew Morton redirty_tail(inode); 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds spin_unlock(&inode_lock); 5091da177e4SLinus Torvalds iput(inode); 5104ffc8444SOGAWA Hirofumi cond_resched(); 5111da177e4SLinus Torvalds spin_lock(&inode_lock); 5128bc3be27SFengguang Wu if (wbc->nr_to_write <= 0) { 5138bc3be27SFengguang Wu wbc->more_io = 1; 5141da177e4SLinus Torvalds break; 5151da177e4SLinus Torvalds } 5168bc3be27SFengguang Wu if (!list_empty(&sb->s_more_io)) 5178bc3be27SFengguang Wu wbc->more_io = 1; 5188bc3be27SFengguang Wu } 519ae8547b0SHans Reiser spin_unlock(&inode_lock); 5201da177e4SLinus Torvalds return; /* Leave any unwritten inodes on s_io */ 5211da177e4SLinus Torvalds } 5224ee6afd3SArtem Bityutskiy EXPORT_SYMBOL_GPL(generic_sync_sb_inodes); 5234ee6afd3SArtem Bityutskiy 5244ee6afd3SArtem Bityutskiy static void sync_sb_inodes(struct super_block *sb, 5254ee6afd3SArtem Bityutskiy struct writeback_control *wbc) 5264ee6afd3SArtem Bityutskiy { 5274ee6afd3SArtem Bityutskiy generic_sync_sb_inodes(sb, wbc); 5284ee6afd3SArtem Bityutskiy } 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds /* 5311da177e4SLinus Torvalds * Start writeback of dirty pagecache data against all unlocked inodes. 5321da177e4SLinus Torvalds * 5331da177e4SLinus Torvalds * Note: 5341da177e4SLinus Torvalds * We don't need to grab a reference to superblock here. If it has non-empty 5351da177e4SLinus Torvalds * ->s_dirty it's hadn't been killed yet and kill_super() won't proceed 5362c136579SFengguang Wu * past sync_inodes_sb() until the ->s_dirty/s_io/s_more_io lists are all 5371da177e4SLinus Torvalds * empty. Since __sync_single_inode() regains inode_lock before it finally moves 5381da177e4SLinus Torvalds * inode from superblock lists we are OK. 5391da177e4SLinus Torvalds * 5401da177e4SLinus Torvalds * If `older_than_this' is non-zero then only flush inodes which have a 5411da177e4SLinus Torvalds * flushtime older than *older_than_this. 5421da177e4SLinus Torvalds * 5431da177e4SLinus Torvalds * If `bdi' is non-zero then we will scan the first inode against each 5441da177e4SLinus Torvalds * superblock until we find the matching ones. One group will be the dirty 5451da177e4SLinus Torvalds * inodes against a filesystem. Then when we hit the dummy blockdev superblock, 5461da177e4SLinus Torvalds * sync_sb_inodes will seekout the blockdev which matches `bdi'. Maybe not 5471da177e4SLinus Torvalds * super-efficient but we're about to do a ton of I/O... 5481da177e4SLinus Torvalds */ 5491da177e4SLinus Torvalds void 5501da177e4SLinus Torvalds writeback_inodes(struct writeback_control *wbc) 5511da177e4SLinus Torvalds { 5521da177e4SLinus Torvalds struct super_block *sb; 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds might_sleep(); 5551da177e4SLinus Torvalds spin_lock(&sb_lock); 5561da177e4SLinus Torvalds restart: 557797074e4SAkinobu Mita list_for_each_entry_reverse(sb, &super_blocks, s_list) { 55808d8e974SFengguang Wu if (sb_has_dirty_inodes(sb)) { 5591da177e4SLinus Torvalds /* we're making our own get_super here */ 5601da177e4SLinus Torvalds sb->s_count++; 5611da177e4SLinus Torvalds spin_unlock(&sb_lock); 5621da177e4SLinus Torvalds /* 5631da177e4SLinus Torvalds * If we can't get the readlock, there's no sense in 5641da177e4SLinus Torvalds * waiting around, most of the time the FS is going to 5651da177e4SLinus Torvalds * be unmounted by the time it is released. 5661da177e4SLinus Torvalds */ 5671da177e4SLinus Torvalds if (down_read_trylock(&sb->s_umount)) { 568ae8547b0SHans Reiser if (sb->s_root) 5691da177e4SLinus Torvalds sync_sb_inodes(sb, wbc); 5701da177e4SLinus Torvalds up_read(&sb->s_umount); 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds spin_lock(&sb_lock); 5731da177e4SLinus Torvalds if (__put_super_and_need_restart(sb)) 5741da177e4SLinus Torvalds goto restart; 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds if (wbc->nr_to_write <= 0) 5771da177e4SLinus Torvalds break; 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds spin_unlock(&sb_lock); 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* 5831da177e4SLinus Torvalds * writeback and wait upon the filesystem's dirty inodes. The caller will 5844f5a99d6SNick Piggin * do this in two passes - one to write, and one to wait. 5851da177e4SLinus Torvalds * 5861da177e4SLinus Torvalds * A finite limit is set on the number of pages which will be written. 5871da177e4SLinus Torvalds * To prevent infinite livelock of sys_sync(). 5881da177e4SLinus Torvalds * 5891da177e4SLinus Torvalds * We add in the number of potentially dirty inodes, because each inode write 5901da177e4SLinus Torvalds * can dirty pagecache in the underlying blockdev. 5911da177e4SLinus Torvalds */ 5921da177e4SLinus Torvalds void sync_inodes_sb(struct super_block *sb, int wait) 5931da177e4SLinus Torvalds { 5941da177e4SLinus Torvalds struct writeback_control wbc = { 5954f5a99d6SNick Piggin .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, 596111ebb6eSOGAWA Hirofumi .range_start = 0, 597111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 5981da177e4SLinus Torvalds }; 599b1e7a8fdSChristoph Lameter unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY); 600fd39fc85SChristoph Lameter unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS); 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds wbc.nr_to_write = nr_dirty + nr_unstable + 6031da177e4SLinus Torvalds (inodes_stat.nr_inodes - inodes_stat.nr_unused) + 6041da177e4SLinus Torvalds nr_dirty + nr_unstable; 6051da177e4SLinus Torvalds wbc.nr_to_write += wbc.nr_to_write / 2; /* Bit more for luck */ 6061da177e4SLinus Torvalds sync_sb_inodes(sb, &wbc); 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds /* 6101da177e4SLinus Torvalds * Rather lame livelock avoidance. 6111da177e4SLinus Torvalds */ 6121da177e4SLinus Torvalds static void set_sb_syncing(int val) 6131da177e4SLinus Torvalds { 6141da177e4SLinus Torvalds struct super_block *sb; 6151da177e4SLinus Torvalds spin_lock(&sb_lock); 616797074e4SAkinobu Mita list_for_each_entry_reverse(sb, &super_blocks, s_list) 6171da177e4SLinus Torvalds sb->s_syncing = val; 6181da177e4SLinus Torvalds spin_unlock(&sb_lock); 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds /** 62267be2dd1SMartin Waitz * sync_inodes - writes all inodes to disk 62367be2dd1SMartin Waitz * @wait: wait for completion 6241da177e4SLinus Torvalds * 6251da177e4SLinus Torvalds * sync_inodes() goes through each super block's dirty inode list, writes the 6261da177e4SLinus Torvalds * inodes out, waits on the writeout and puts the inodes back on the normal 6271da177e4SLinus Torvalds * list. 6281da177e4SLinus Torvalds * 6291da177e4SLinus Torvalds * This is for sys_sync(). fsync_dev() uses the same algorithm. The subtle 6301da177e4SLinus Torvalds * part of the sync functions is that the blockdev "superblock" is processed 6311da177e4SLinus Torvalds * last. This is because the write_inode() function of a typical fs will 6321da177e4SLinus Torvalds * perform no I/O, but will mark buffers in the blockdev mapping as dirty. 6331da177e4SLinus Torvalds * What we want to do is to perform all that dirtying first, and then write 6341da177e4SLinus Torvalds * back all those inode blocks via the blockdev mapping in one sweep. So the 6351da177e4SLinus Torvalds * additional (somewhat redundant) sync_blockdev() calls here are to make 6361da177e4SLinus Torvalds * sure that really happens. Because if we call sync_inodes_sb(wait=1) with 6371da177e4SLinus Torvalds * outstanding dirty inodes, the writeback goes block-at-a-time within the 6381da177e4SLinus Torvalds * filesystem's write_inode(). This is extremely slow. 6391da177e4SLinus Torvalds */ 640618f0636SKirill Korotaev static void __sync_inodes(int wait) 6411da177e4SLinus Torvalds { 6421da177e4SLinus Torvalds struct super_block *sb; 6431da177e4SLinus Torvalds 644618f0636SKirill Korotaev spin_lock(&sb_lock); 645618f0636SKirill Korotaev restart: 646618f0636SKirill Korotaev list_for_each_entry(sb, &super_blocks, s_list) { 647618f0636SKirill Korotaev if (sb->s_syncing) 648618f0636SKirill Korotaev continue; 649618f0636SKirill Korotaev sb->s_syncing = 1; 650618f0636SKirill Korotaev sb->s_count++; 651618f0636SKirill Korotaev spin_unlock(&sb_lock); 652618f0636SKirill Korotaev down_read(&sb->s_umount); 653618f0636SKirill Korotaev if (sb->s_root) { 654618f0636SKirill Korotaev sync_inodes_sb(sb, wait); 6551da177e4SLinus Torvalds sync_blockdev(sb->s_bdev); 6561da177e4SLinus Torvalds } 657618f0636SKirill Korotaev up_read(&sb->s_umount); 658618f0636SKirill Korotaev spin_lock(&sb_lock); 659618f0636SKirill Korotaev if (__put_super_and_need_restart(sb)) 660618f0636SKirill Korotaev goto restart; 661618f0636SKirill Korotaev } 662618f0636SKirill Korotaev spin_unlock(&sb_lock); 663618f0636SKirill Korotaev } 664618f0636SKirill Korotaev 665618f0636SKirill Korotaev void sync_inodes(int wait) 666618f0636SKirill Korotaev { 667618f0636SKirill Korotaev set_sb_syncing(0); 668618f0636SKirill Korotaev __sync_inodes(0); 669618f0636SKirill Korotaev 6701da177e4SLinus Torvalds if (wait) { 6711da177e4SLinus Torvalds set_sb_syncing(0); 672618f0636SKirill Korotaev __sync_inodes(1); 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /** 6771da177e4SLinus Torvalds * write_inode_now - write an inode to disk 6781da177e4SLinus Torvalds * @inode: inode to write to disk 6791da177e4SLinus Torvalds * @sync: whether the write should be synchronous or not 6801da177e4SLinus Torvalds * 6817f04c26dSAndrea Arcangeli * This function commits an inode to disk immediately if it is dirty. This is 6827f04c26dSAndrea Arcangeli * primarily needed by knfsd. 6837f04c26dSAndrea Arcangeli * 6847f04c26dSAndrea Arcangeli * The caller must either have a ref on the inode or must have set I_WILL_FREE. 6851da177e4SLinus Torvalds */ 6861da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync) 6871da177e4SLinus Torvalds { 6881da177e4SLinus Torvalds int ret; 6891da177e4SLinus Torvalds struct writeback_control wbc = { 6901da177e4SLinus Torvalds .nr_to_write = LONG_MAX, 69118914b18SMike Galbraith .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 692111ebb6eSOGAWA Hirofumi .range_start = 0, 693111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 6941da177e4SLinus Torvalds }; 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds if (!mapping_cap_writeback_dirty(inode->i_mapping)) 69749364ce2SAndrew Morton wbc.nr_to_write = 0; 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds might_sleep(); 7001da177e4SLinus Torvalds spin_lock(&inode_lock); 7011da177e4SLinus Torvalds ret = __writeback_single_inode(inode, &wbc); 7021da177e4SLinus Torvalds spin_unlock(&inode_lock); 7031da177e4SLinus Torvalds if (sync) 7041c0eeaf5SJoern Engel inode_sync_wait(inode); 7051da177e4SLinus Torvalds return ret; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now); 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds /** 7101da177e4SLinus Torvalds * sync_inode - write an inode and its pages to disk. 7111da177e4SLinus Torvalds * @inode: the inode to sync 7121da177e4SLinus Torvalds * @wbc: controls the writeback mode 7131da177e4SLinus Torvalds * 7141da177e4SLinus Torvalds * sync_inode() will write an inode and its pages to disk. It will also 7151da177e4SLinus Torvalds * correctly update the inode on its superblock's dirty inode lists and will 7161da177e4SLinus Torvalds * update inode->i_state. 7171da177e4SLinus Torvalds * 7181da177e4SLinus Torvalds * The caller must have a ref on the inode. 7191da177e4SLinus Torvalds */ 7201da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc) 7211da177e4SLinus Torvalds { 7221da177e4SLinus Torvalds int ret; 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds spin_lock(&inode_lock); 7251da177e4SLinus Torvalds ret = __writeback_single_inode(inode, wbc); 7261da177e4SLinus Torvalds spin_unlock(&inode_lock); 7271da177e4SLinus Torvalds return ret; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode); 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds /** 7321da177e4SLinus Torvalds * generic_osync_inode - flush all dirty data for a given inode to disk 7331da177e4SLinus Torvalds * @inode: inode to write 73467be2dd1SMartin Waitz * @mapping: the address_space that should be flushed 7351da177e4SLinus Torvalds * @what: what to write and wait upon 7361da177e4SLinus Torvalds * 7371da177e4SLinus Torvalds * This can be called by file_write functions for files which have the 7381da177e4SLinus Torvalds * O_SYNC flag set, to flush dirty writes to disk. 7391da177e4SLinus Torvalds * 7401da177e4SLinus Torvalds * @what is a bitmask, specifying which part of the inode's data should be 741b8887e6eSRandy Dunlap * written and waited upon. 7421da177e4SLinus Torvalds * 7431da177e4SLinus Torvalds * OSYNC_DATA: i_mapping's dirty data 7441da177e4SLinus Torvalds * OSYNC_METADATA: the buffers at i_mapping->private_list 7451da177e4SLinus Torvalds * OSYNC_INODE: the inode itself 7461da177e4SLinus Torvalds */ 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds int generic_osync_inode(struct inode *inode, struct address_space *mapping, int what) 7491da177e4SLinus Torvalds { 7501da177e4SLinus Torvalds int err = 0; 7511da177e4SLinus Torvalds int need_write_inode_now = 0; 7521da177e4SLinus Torvalds int err2; 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds if (what & OSYNC_DATA) 7551da177e4SLinus Torvalds err = filemap_fdatawrite(mapping); 7561da177e4SLinus Torvalds if (what & (OSYNC_METADATA|OSYNC_DATA)) { 7571da177e4SLinus Torvalds err2 = sync_mapping_buffers(mapping); 7581da177e4SLinus Torvalds if (!err) 7591da177e4SLinus Torvalds err = err2; 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds if (what & OSYNC_DATA) { 7621da177e4SLinus Torvalds err2 = filemap_fdatawait(mapping); 7631da177e4SLinus Torvalds if (!err) 7641da177e4SLinus Torvalds err = err2; 7651da177e4SLinus Torvalds } 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds spin_lock(&inode_lock); 7681da177e4SLinus Torvalds if ((inode->i_state & I_DIRTY) && 7691da177e4SLinus Torvalds ((what & OSYNC_INODE) || (inode->i_state & I_DIRTY_DATASYNC))) 7701da177e4SLinus Torvalds need_write_inode_now = 1; 7711da177e4SLinus Torvalds spin_unlock(&inode_lock); 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds if (need_write_inode_now) { 7741da177e4SLinus Torvalds err2 = write_inode_now(inode, 1); 7751da177e4SLinus Torvalds if (!err) 7761da177e4SLinus Torvalds err = err2; 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds else 7791c0eeaf5SJoern Engel inode_sync_wait(inode); 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds return err; 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds EXPORT_SYMBOL(generic_osync_inode); 784