11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * fs/fs-writeback.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2002, Linus Torvalds. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Contains all the functions related to writing back and waiting 71da177e4SLinus Torvalds * upon dirty inodes against superblocks, and writing back dirty 81da177e4SLinus Torvalds * pages against inodes. ie: data writeback. Writeout of the 91da177e4SLinus Torvalds * inode itself is not handled here. 101da177e4SLinus Torvalds * 11e1f8e874SFrancois Cami * 10Apr2002 Andrew Morton 121da177e4SLinus Torvalds * Split out of fs/inode.c 131da177e4SLinus Torvalds * Additions for address_space-based writeback 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/kernel.h> 17f5ff8422SJens Axboe #include <linux/module.h> 181da177e4SLinus Torvalds #include <linux/spinlock.h> 191da177e4SLinus Torvalds #include <linux/sched.h> 201da177e4SLinus Torvalds #include <linux/fs.h> 211da177e4SLinus Torvalds #include <linux/mm.h> 221da177e4SLinus Torvalds #include <linux/writeback.h> 231da177e4SLinus Torvalds #include <linux/blkdev.h> 241da177e4SLinus Torvalds #include <linux/backing-dev.h> 251da177e4SLinus Torvalds #include <linux/buffer_head.h> 2607f3f05cSDavid Howells #include "internal.h" 271da177e4SLinus Torvalds 28f11b00f3SAdrian Bunk 29f11b00f3SAdrian Bunk /** 30f11b00f3SAdrian Bunk * writeback_acquire - attempt to get exclusive writeback access to a device 31f11b00f3SAdrian Bunk * @bdi: the device's backing_dev_info structure 32f11b00f3SAdrian Bunk * 33f11b00f3SAdrian Bunk * It is a waste of resources to have more than one pdflush thread blocked on 34f11b00f3SAdrian Bunk * a single request queue. Exclusion at the request_queue level is obtained 35f11b00f3SAdrian Bunk * via a flag in the request_queue's backing_dev_info.state. 36f11b00f3SAdrian Bunk * 37f11b00f3SAdrian Bunk * Non-request_queue-backed address_spaces will share default_backing_dev_info, 38f11b00f3SAdrian Bunk * unless they implement their own. Which is somewhat inefficient, as this 39f11b00f3SAdrian Bunk * may prevent concurrent writeback against multiple devices. 40f11b00f3SAdrian Bunk */ 41f11b00f3SAdrian Bunk static int writeback_acquire(struct backing_dev_info *bdi) 42f11b00f3SAdrian Bunk { 43f11b00f3SAdrian Bunk return !test_and_set_bit(BDI_pdflush, &bdi->state); 44f11b00f3SAdrian Bunk } 45f11b00f3SAdrian Bunk 46f11b00f3SAdrian Bunk /** 47f11b00f3SAdrian Bunk * writeback_in_progress - determine whether there is writeback in progress 48f11b00f3SAdrian Bunk * @bdi: the device's backing_dev_info structure. 49f11b00f3SAdrian Bunk * 50f11b00f3SAdrian Bunk * Determine whether there is writeback in progress against a backing device. 51f11b00f3SAdrian Bunk */ 52f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi) 53f11b00f3SAdrian Bunk { 54f11b00f3SAdrian Bunk return test_bit(BDI_pdflush, &bdi->state); 55f11b00f3SAdrian Bunk } 56f11b00f3SAdrian Bunk 57f11b00f3SAdrian Bunk /** 58f11b00f3SAdrian Bunk * writeback_release - relinquish exclusive writeback access against a device. 59f11b00f3SAdrian Bunk * @bdi: the device's backing_dev_info structure 60f11b00f3SAdrian Bunk */ 61f11b00f3SAdrian Bunk static void writeback_release(struct backing_dev_info *bdi) 62f11b00f3SAdrian Bunk { 63f11b00f3SAdrian Bunk BUG_ON(!writeback_in_progress(bdi)); 64f11b00f3SAdrian Bunk clear_bit(BDI_pdflush, &bdi->state); 65f11b00f3SAdrian Bunk } 66f11b00f3SAdrian Bunk 674195f73dSNick Piggin static noinline void block_dump___mark_inode_dirty(struct inode *inode) 684195f73dSNick Piggin { 694195f73dSNick Piggin if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { 704195f73dSNick Piggin struct dentry *dentry; 714195f73dSNick Piggin const char *name = "?"; 724195f73dSNick Piggin 734195f73dSNick Piggin dentry = d_find_alias(inode); 744195f73dSNick Piggin if (dentry) { 754195f73dSNick Piggin spin_lock(&dentry->d_lock); 764195f73dSNick Piggin name = (const char *) dentry->d_name.name; 774195f73dSNick Piggin } 784195f73dSNick Piggin printk(KERN_DEBUG 794195f73dSNick Piggin "%s(%d): dirtied inode %lu (%s) on %s\n", 804195f73dSNick Piggin current->comm, task_pid_nr(current), inode->i_ino, 814195f73dSNick Piggin name, inode->i_sb->s_id); 824195f73dSNick Piggin if (dentry) { 834195f73dSNick Piggin spin_unlock(&dentry->d_lock); 844195f73dSNick Piggin dput(dentry); 854195f73dSNick Piggin } 864195f73dSNick Piggin } 874195f73dSNick Piggin } 884195f73dSNick Piggin 891da177e4SLinus Torvalds /** 901da177e4SLinus Torvalds * __mark_inode_dirty - internal function 911da177e4SLinus Torvalds * @inode: inode to mark 921da177e4SLinus Torvalds * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) 931da177e4SLinus Torvalds * Mark an inode as dirty. Callers should use mark_inode_dirty or 941da177e4SLinus Torvalds * mark_inode_dirty_sync. 951da177e4SLinus Torvalds * 961da177e4SLinus Torvalds * Put the inode on the super block's dirty list. 971da177e4SLinus Torvalds * 981da177e4SLinus Torvalds * CAREFUL! We mark it dirty unconditionally, but move it onto the 991da177e4SLinus Torvalds * dirty list only if it is hashed or if it refers to a blockdev. 1001da177e4SLinus Torvalds * If it was not hashed, it will never be added to the dirty list 1011da177e4SLinus Torvalds * even if it is later hashed, as it will have been marked dirty already. 1021da177e4SLinus Torvalds * 1031da177e4SLinus Torvalds * In short, make sure you hash any inodes _before_ you start marking 1041da177e4SLinus Torvalds * them dirty. 1051da177e4SLinus Torvalds * 1061da177e4SLinus Torvalds * This function *must* be atomic for the I_DIRTY_PAGES case - 1071da177e4SLinus Torvalds * set_page_dirty() is called under spinlock in several places. 1081da177e4SLinus Torvalds * 1091da177e4SLinus Torvalds * Note that for blockdevs, inode->dirtied_when represents the dirtying time of 1101da177e4SLinus Torvalds * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of 1111da177e4SLinus Torvalds * the kernel-internal blockdev inode represents the dirtying time of the 1121da177e4SLinus Torvalds * blockdev's pages. This is why for I_DIRTY_PAGES we always use 1131da177e4SLinus Torvalds * page->mapping->host, so the page-dirtying time is recorded in the internal 1141da177e4SLinus Torvalds * blockdev inode. 1151da177e4SLinus Torvalds */ 1161da177e4SLinus Torvalds void __mark_inode_dirty(struct inode *inode, int flags) 1171da177e4SLinus Torvalds { 1181da177e4SLinus Torvalds struct super_block *sb = inode->i_sb; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds /* 1211da177e4SLinus Torvalds * Don't do this for I_DIRTY_PAGES - that doesn't actually 1221da177e4SLinus Torvalds * dirty the inode itself 1231da177e4SLinus Torvalds */ 1241da177e4SLinus Torvalds if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { 1251da177e4SLinus Torvalds if (sb->s_op->dirty_inode) 1261da177e4SLinus Torvalds sb->s_op->dirty_inode(inode); 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /* 1301da177e4SLinus Torvalds * make sure that changes are seen by all cpus before we test i_state 1311da177e4SLinus Torvalds * -- mikulas 1321da177e4SLinus Torvalds */ 1331da177e4SLinus Torvalds smp_mb(); 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds /* avoid the locking if we can */ 1361da177e4SLinus Torvalds if ((inode->i_state & flags) == flags) 1371da177e4SLinus Torvalds return; 1381da177e4SLinus Torvalds 1394195f73dSNick Piggin if (unlikely(block_dump)) 1404195f73dSNick Piggin block_dump___mark_inode_dirty(inode); 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds spin_lock(&inode_lock); 1431da177e4SLinus Torvalds if ((inode->i_state & flags) != flags) { 1441da177e4SLinus Torvalds const int was_dirty = inode->i_state & I_DIRTY; 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds inode->i_state |= flags; 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* 1491c0eeaf5SJoern Engel * If the inode is being synced, just update its dirty state. 1501da177e4SLinus Torvalds * The unlocker will place the inode on the appropriate 1511da177e4SLinus Torvalds * superblock list, based upon its state. 1521da177e4SLinus Torvalds */ 1531c0eeaf5SJoern Engel if (inode->i_state & I_SYNC) 1541da177e4SLinus Torvalds goto out; 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds /* 1571da177e4SLinus Torvalds * Only add valid (hashed) inodes to the superblock's 1581da177e4SLinus Torvalds * dirty list. Add blockdev inodes as well. 1591da177e4SLinus Torvalds */ 1601da177e4SLinus Torvalds if (!S_ISBLK(inode->i_mode)) { 1611da177e4SLinus Torvalds if (hlist_unhashed(&inode->i_hash)) 1621da177e4SLinus Torvalds goto out; 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds if (inode->i_state & (I_FREEING|I_CLEAR)) 1651da177e4SLinus Torvalds goto out; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds /* 1682c136579SFengguang Wu * If the inode was already on s_dirty/s_io/s_more_io, don't 1691da177e4SLinus Torvalds * reposition it (that would break s_dirty time-ordering). 1701da177e4SLinus Torvalds */ 1711da177e4SLinus Torvalds if (!was_dirty) { 1721da177e4SLinus Torvalds inode->dirtied_when = jiffies; 1731da177e4SLinus Torvalds list_move(&inode->i_list, &sb->s_dirty); 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds out: 1771da177e4SLinus Torvalds spin_unlock(&inode_lock); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds EXPORT_SYMBOL(__mark_inode_dirty); 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds static int write_inode(struct inode *inode, int sync) 1831da177e4SLinus Torvalds { 1841da177e4SLinus Torvalds if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) 1851da177e4SLinus Torvalds return inode->i_sb->s_op->write_inode(inode, sync); 1861da177e4SLinus Torvalds return 0; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /* 1906610a0bcSAndrew Morton * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 1916610a0bcSAndrew Morton * furthest end of its superblock's dirty-inode list. 1926610a0bcSAndrew Morton * 1936610a0bcSAndrew Morton * Before stamping the inode's ->dirtied_when, we check to see whether it is 1946610a0bcSAndrew Morton * already the most-recently-dirtied inode on the s_dirty list. If that is 1956610a0bcSAndrew Morton * the case then the inode must have been redirtied while it was being written 1966610a0bcSAndrew Morton * out and we don't reset its dirtied_when. 1976610a0bcSAndrew Morton */ 1986610a0bcSAndrew Morton static void redirty_tail(struct inode *inode) 1996610a0bcSAndrew Morton { 2006610a0bcSAndrew Morton struct super_block *sb = inode->i_sb; 2016610a0bcSAndrew Morton 2026610a0bcSAndrew Morton if (!list_empty(&sb->s_dirty)) { 2036610a0bcSAndrew Morton struct inode *tail_inode; 2046610a0bcSAndrew Morton 2056610a0bcSAndrew Morton tail_inode = list_entry(sb->s_dirty.next, struct inode, i_list); 206d2caa3c5SJeff Layton if (time_before(inode->dirtied_when, 2076610a0bcSAndrew Morton tail_inode->dirtied_when)) 2086610a0bcSAndrew Morton inode->dirtied_when = jiffies; 2096610a0bcSAndrew Morton } 2106610a0bcSAndrew Morton list_move(&inode->i_list, &sb->s_dirty); 2116610a0bcSAndrew Morton } 2126610a0bcSAndrew Morton 2136610a0bcSAndrew Morton /* 2140e0f4fc2SKen Chen * requeue inode for re-scanning after sb->s_io list is exhausted. 215c986d1e2SAndrew Morton */ 2160e0f4fc2SKen Chen static void requeue_io(struct inode *inode) 217c986d1e2SAndrew Morton { 2180e0f4fc2SKen Chen list_move(&inode->i_list, &inode->i_sb->s_more_io); 219c986d1e2SAndrew Morton } 220c986d1e2SAndrew Morton 2211c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode) 2221c0eeaf5SJoern Engel { 2231c0eeaf5SJoern Engel /* 2241c0eeaf5SJoern Engel * Prevent speculative execution through spin_unlock(&inode_lock); 2251c0eeaf5SJoern Engel */ 2261c0eeaf5SJoern Engel smp_mb(); 2271c0eeaf5SJoern Engel wake_up_bit(&inode->i_state, __I_SYNC); 2281c0eeaf5SJoern Engel } 2291c0eeaf5SJoern Engel 230d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t) 231d2caa3c5SJeff Layton { 232d2caa3c5SJeff Layton bool ret = time_after(inode->dirtied_when, t); 233d2caa3c5SJeff Layton #ifndef CONFIG_64BIT 234d2caa3c5SJeff Layton /* 235d2caa3c5SJeff Layton * For inodes being constantly redirtied, dirtied_when can get stuck. 236d2caa3c5SJeff Layton * It _appears_ to be in the future, but is actually in distant past. 237d2caa3c5SJeff Layton * This test is necessary to prevent such wrapped-around relative times 238d2caa3c5SJeff Layton * from permanently stopping the whole pdflush writeback. 239d2caa3c5SJeff Layton */ 240d2caa3c5SJeff Layton ret = ret && time_before_eq(inode->dirtied_when, jiffies); 241d2caa3c5SJeff Layton #endif 242d2caa3c5SJeff Layton return ret; 243d2caa3c5SJeff Layton } 244d2caa3c5SJeff Layton 245c986d1e2SAndrew Morton /* 2462c136579SFengguang Wu * Move expired dirty inodes from @delaying_queue to @dispatch_queue. 2472c136579SFengguang Wu */ 2482c136579SFengguang Wu static void move_expired_inodes(struct list_head *delaying_queue, 2492c136579SFengguang Wu struct list_head *dispatch_queue, 2502c136579SFengguang Wu unsigned long *older_than_this) 2512c136579SFengguang Wu { 2522c136579SFengguang Wu while (!list_empty(delaying_queue)) { 2532c136579SFengguang Wu struct inode *inode = list_entry(delaying_queue->prev, 2542c136579SFengguang Wu struct inode, i_list); 2552c136579SFengguang Wu if (older_than_this && 256d2caa3c5SJeff Layton inode_dirtied_after(inode, *older_than_this)) 2572c136579SFengguang Wu break; 2582c136579SFengguang Wu list_move(&inode->i_list, dispatch_queue); 2592c136579SFengguang Wu } 2602c136579SFengguang Wu } 2612c136579SFengguang Wu 2622c136579SFengguang Wu /* 2632c136579SFengguang Wu * Queue all expired dirty inodes for io, eldest first. 2642c136579SFengguang Wu */ 2652c136579SFengguang Wu static void queue_io(struct super_block *sb, 2662c136579SFengguang Wu unsigned long *older_than_this) 2672c136579SFengguang Wu { 2682c136579SFengguang Wu list_splice_init(&sb->s_more_io, sb->s_io.prev); 2692c136579SFengguang Wu move_expired_inodes(&sb->s_dirty, &sb->s_io, older_than_this); 2702c136579SFengguang Wu } 2712c136579SFengguang Wu 27208d8e974SFengguang Wu int sb_has_dirty_inodes(struct super_block *sb) 27308d8e974SFengguang Wu { 27408d8e974SFengguang Wu return !list_empty(&sb->s_dirty) || 27508d8e974SFengguang Wu !list_empty(&sb->s_io) || 27608d8e974SFengguang Wu !list_empty(&sb->s_more_io); 27708d8e974SFengguang Wu } 27808d8e974SFengguang Wu EXPORT_SYMBOL(sb_has_dirty_inodes); 27908d8e974SFengguang Wu 2802c136579SFengguang Wu /* 2811da177e4SLinus Torvalds * Write a single inode's dirty pages and inode data out to disk. 2821da177e4SLinus Torvalds * If `wait' is set, wait on the writeout. 2831da177e4SLinus Torvalds * 2841da177e4SLinus Torvalds * The whole writeout design is quite complex and fragile. We want to avoid 2851da177e4SLinus Torvalds * starvation of particular inodes when others are being redirtied, prevent 2861da177e4SLinus Torvalds * livelocks, etc. 2871da177e4SLinus Torvalds * 2881da177e4SLinus Torvalds * Called under inode_lock. 2891da177e4SLinus Torvalds */ 2901da177e4SLinus Torvalds static int 2911da177e4SLinus Torvalds __sync_single_inode(struct inode *inode, struct writeback_control *wbc) 2921da177e4SLinus Torvalds { 2931da177e4SLinus Torvalds unsigned dirty; 2941da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 2951da177e4SLinus Torvalds int wait = wbc->sync_mode == WB_SYNC_ALL; 2961da177e4SLinus Torvalds int ret; 2971da177e4SLinus Torvalds 2981c0eeaf5SJoern Engel BUG_ON(inode->i_state & I_SYNC); 2991da177e4SLinus Torvalds 3001c0eeaf5SJoern Engel /* Set I_SYNC, reset I_DIRTY */ 3011da177e4SLinus Torvalds dirty = inode->i_state & I_DIRTY; 3021c0eeaf5SJoern Engel inode->i_state |= I_SYNC; 3031da177e4SLinus Torvalds inode->i_state &= ~I_DIRTY; 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds spin_unlock(&inode_lock); 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds ret = do_writepages(mapping, wbc); 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds /* Don't write the inode if only I_DIRTY_PAGES was set */ 3101da177e4SLinus Torvalds if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { 3111da177e4SLinus Torvalds int err = write_inode(inode, wait); 3121da177e4SLinus Torvalds if (ret == 0) 3131da177e4SLinus Torvalds ret = err; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds if (wait) { 3171da177e4SLinus Torvalds int err = filemap_fdatawait(mapping); 3181da177e4SLinus Torvalds if (ret == 0) 3191da177e4SLinus Torvalds ret = err; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds spin_lock(&inode_lock); 3231c0eeaf5SJoern Engel inode->i_state &= ~I_SYNC; 32484a89245SWu Fengguang if (!(inode->i_state & (I_FREEING | I_CLEAR))) { 3251da177e4SLinus Torvalds if (!(inode->i_state & I_DIRTY) && 3261da177e4SLinus Torvalds mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 3271da177e4SLinus Torvalds /* 3281da177e4SLinus Torvalds * We didn't write back all the pages. nfs_writepages() 3291da177e4SLinus Torvalds * sometimes bales out without doing anything. Redirty 3302c136579SFengguang Wu * the inode; Move it from s_io onto s_more_io/s_dirty. 3311b43ef91SAndrew Morton */ 3321b43ef91SAndrew Morton /* 3331b43ef91SAndrew Morton * akpm: if the caller was the kupdate function we put 3341b43ef91SAndrew Morton * this inode at the head of s_dirty so it gets first 3351b43ef91SAndrew Morton * consideration. Otherwise, move it to the tail, for 3361b43ef91SAndrew Morton * the reasons described there. I'm not really sure 3371b43ef91SAndrew Morton * how much sense this makes. Presumably I had a good 3381b43ef91SAndrew Morton * reasons for doing it this way, and I'd rather not 3391b43ef91SAndrew Morton * muck with it at present. 3401da177e4SLinus Torvalds */ 3411da177e4SLinus Torvalds if (wbc->for_kupdate) { 3421da177e4SLinus Torvalds /* 3432c136579SFengguang Wu * For the kupdate function we move the inode 3442c136579SFengguang Wu * to s_more_io so it will get more writeout as 3452c136579SFengguang Wu * soon as the queue becomes uncongested. 3461da177e4SLinus Torvalds */ 3471da177e4SLinus Torvalds inode->i_state |= I_DIRTY_PAGES; 3488bc3be27SFengguang Wu if (wbc->nr_to_write <= 0) { 3498bc3be27SFengguang Wu /* 3508bc3be27SFengguang Wu * slice used up: queue for next turn 3518bc3be27SFengguang Wu */ 3520e0f4fc2SKen Chen requeue_io(inode); 3531da177e4SLinus Torvalds } else { 3541da177e4SLinus Torvalds /* 3558bc3be27SFengguang Wu * somehow blocked: retry later 3568bc3be27SFengguang Wu */ 3578bc3be27SFengguang Wu redirty_tail(inode); 3588bc3be27SFengguang Wu } 3598bc3be27SFengguang Wu } else { 3608bc3be27SFengguang Wu /* 3611da177e4SLinus Torvalds * Otherwise fully redirty the inode so that 3621da177e4SLinus Torvalds * other inodes on this superblock will get some 3631da177e4SLinus Torvalds * writeout. Otherwise heavy writing to one 3641da177e4SLinus Torvalds * file would indefinitely suspend writeout of 3651da177e4SLinus Torvalds * all the other files. 3661da177e4SLinus Torvalds */ 3671da177e4SLinus Torvalds inode->i_state |= I_DIRTY_PAGES; 3681b43ef91SAndrew Morton redirty_tail(inode); 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds } else if (inode->i_state & I_DIRTY) { 3711da177e4SLinus Torvalds /* 3721da177e4SLinus Torvalds * Someone redirtied the inode while were writing back 3731da177e4SLinus Torvalds * the pages. 3741da177e4SLinus Torvalds */ 3756610a0bcSAndrew Morton redirty_tail(inode); 3761da177e4SLinus Torvalds } else if (atomic_read(&inode->i_count)) { 3771da177e4SLinus Torvalds /* 3781da177e4SLinus Torvalds * The inode is clean, inuse 3791da177e4SLinus Torvalds */ 3801da177e4SLinus Torvalds list_move(&inode->i_list, &inode_in_use); 3811da177e4SLinus Torvalds } else { 3821da177e4SLinus Torvalds /* 3831da177e4SLinus Torvalds * The inode is clean, unused 3841da177e4SLinus Torvalds */ 3851da177e4SLinus Torvalds list_move(&inode->i_list, &inode_unused); 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds } 3881c0eeaf5SJoern Engel inode_sync_complete(inode); 3891da177e4SLinus Torvalds return ret; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds /* 3937f04c26dSAndrea Arcangeli * Write out an inode's dirty pages. Called under inode_lock. Either the 3947f04c26dSAndrea Arcangeli * caller has ref on the inode (either via __iget or via syscall against an fd) 3957f04c26dSAndrea Arcangeli * or the inode has I_WILL_FREE set (via generic_forget_inode) 3961da177e4SLinus Torvalds */ 3971da177e4SLinus Torvalds static int 3987f04c26dSAndrea Arcangeli __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) 3991da177e4SLinus Torvalds { 4001da177e4SLinus Torvalds wait_queue_head_t *wqh; 4011da177e4SLinus Torvalds 4027f04c26dSAndrea Arcangeli if (!atomic_read(&inode->i_count)) 403659603efSAndrea Arcangeli WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); 4047f04c26dSAndrea Arcangeli else 4057f04c26dSAndrea Arcangeli WARN_ON(inode->i_state & I_WILL_FREE); 4067f04c26dSAndrea Arcangeli 4071c0eeaf5SJoern Engel if ((wbc->sync_mode != WB_SYNC_ALL) && (inode->i_state & I_SYNC)) { 40865cb9b47SAndrew Morton /* 40965cb9b47SAndrew Morton * We're skipping this inode because it's locked, and we're not 4102c136579SFengguang Wu * doing writeback-for-data-integrity. Move it to s_more_io so 4112c136579SFengguang Wu * that writeback can proceed with the other inodes on s_io. 4122c136579SFengguang Wu * We'll have another go at writing back this inode when we 4132c136579SFengguang Wu * completed a full scan of s_io. 41465cb9b47SAndrew Morton */ 4150e0f4fc2SKen Chen requeue_io(inode); 4162d544564SQi Yong return 0; 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds /* 4201da177e4SLinus Torvalds * It's a data-integrity sync. We must wait. 4211da177e4SLinus Torvalds */ 4221c0eeaf5SJoern Engel if (inode->i_state & I_SYNC) { 4231c0eeaf5SJoern Engel DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); 4241da177e4SLinus Torvalds 4251c0eeaf5SJoern Engel wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 4261da177e4SLinus Torvalds do { 4271da177e4SLinus Torvalds spin_unlock(&inode_lock); 4281da177e4SLinus Torvalds __wait_on_bit(wqh, &wq, inode_wait, 4291da177e4SLinus Torvalds TASK_UNINTERRUPTIBLE); 4301da177e4SLinus Torvalds spin_lock(&inode_lock); 4311c0eeaf5SJoern Engel } while (inode->i_state & I_SYNC); 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds return __sync_single_inode(inode, wbc); 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds /* 4371da177e4SLinus Torvalds * Write out a superblock's list of dirty inodes. A wait will be performed 4381da177e4SLinus Torvalds * upon no inodes, all inodes or the final one, depending upon sync_mode. 4391da177e4SLinus Torvalds * 4401da177e4SLinus Torvalds * If older_than_this is non-NULL, then only write out inodes which 4411da177e4SLinus Torvalds * had their first dirtying at a time earlier than *older_than_this. 4421da177e4SLinus Torvalds * 4433e3cb64fSMasatake YAMATO * If we're a pdflush thread, then implement pdflush collision avoidance 4441da177e4SLinus Torvalds * against the entire list. 4451da177e4SLinus Torvalds * 4461da177e4SLinus Torvalds * If `bdi' is non-zero then we're being asked to writeback a specific queue. 4471da177e4SLinus Torvalds * This function assumes that the blockdev superblock's inodes are backed by 4481da177e4SLinus Torvalds * a variety of queues, so all inodes are searched. For other superblocks, 4491da177e4SLinus Torvalds * assume that all inodes are backed by the same queue. 4501da177e4SLinus Torvalds * 4511da177e4SLinus Torvalds * FIXME: this linear search could get expensive with many fileystems. But 4521da177e4SLinus Torvalds * how to fix? We need to go from an address_space to all inodes which share 4531da177e4SLinus Torvalds * a queue with that address_space. (Easy: have a global "dirty superblocks" 4541da177e4SLinus Torvalds * list). 4551da177e4SLinus Torvalds * 4561da177e4SLinus Torvalds * The inodes to be written are parked on sb->s_io. They are moved back onto 4571da177e4SLinus Torvalds * sb->s_dirty as they are selected for writing. This way, none can be missed 4581da177e4SLinus Torvalds * on the writer throttling path, and we get decent balancing between many 4591c0eeaf5SJoern Engel * throttled threads: we don't want them all piling up on inode_sync_wait. 4601da177e4SLinus Torvalds */ 4614ee6afd3SArtem Bityutskiy void generic_sync_sb_inodes(struct super_block *sb, 4624ee6afd3SArtem Bityutskiy struct writeback_control *wbc) 4631da177e4SLinus Torvalds { 4641da177e4SLinus Torvalds const unsigned long start = jiffies; /* livelock avoidance */ 46538f21977SNick Piggin int sync = wbc->sync_mode == WB_SYNC_ALL; 4661da177e4SLinus Torvalds 467ae8547b0SHans Reiser spin_lock(&inode_lock); 4681da177e4SLinus Torvalds if (!wbc->for_kupdate || list_empty(&sb->s_io)) 4692c136579SFengguang Wu queue_io(sb, wbc->older_than_this); 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds while (!list_empty(&sb->s_io)) { 4721da177e4SLinus Torvalds struct inode *inode = list_entry(sb->s_io.prev, 4731da177e4SLinus Torvalds struct inode, i_list); 4741da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 4751da177e4SLinus Torvalds struct backing_dev_info *bdi = mapping->backing_dev_info; 4761da177e4SLinus Torvalds long pages_skipped; 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds if (!bdi_cap_writeback_dirty(bdi)) { 4799852a0e7SAndrew Morton redirty_tail(inode); 4807b0de42dSDavid Howells if (sb_is_blkdev_sb(sb)) { 4811da177e4SLinus Torvalds /* 4821da177e4SLinus Torvalds * Dirty memory-backed blockdev: the ramdisk 4831da177e4SLinus Torvalds * driver does this. Skip just this inode 4841da177e4SLinus Torvalds */ 4851da177e4SLinus Torvalds continue; 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds /* 4881da177e4SLinus Torvalds * Dirty memory-backed inode against a filesystem other 4891da177e4SLinus Torvalds * than the kernel-internal bdev filesystem. Skip the 4901da177e4SLinus Torvalds * entire superblock. 4911da177e4SLinus Torvalds */ 4921da177e4SLinus Torvalds break; 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 49584a89245SWu Fengguang if (inode->i_state & (I_NEW | I_WILL_FREE)) { 4967ef0d737SNick Piggin requeue_io(inode); 4977ef0d737SNick Piggin continue; 4987ef0d737SNick Piggin } 4997ef0d737SNick Piggin 5001da177e4SLinus Torvalds if (wbc->nonblocking && bdi_write_congested(bdi)) { 5011da177e4SLinus Torvalds wbc->encountered_congestion = 1; 5027b0de42dSDavid Howells if (!sb_is_blkdev_sb(sb)) 5031da177e4SLinus Torvalds break; /* Skip a congested fs */ 5040e0f4fc2SKen Chen requeue_io(inode); 5051da177e4SLinus Torvalds continue; /* Skip a congested blockdev */ 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds if (wbc->bdi && bdi != wbc->bdi) { 5097b0de42dSDavid Howells if (!sb_is_blkdev_sb(sb)) 5101da177e4SLinus Torvalds break; /* fs has the wrong queue */ 5110e0f4fc2SKen Chen requeue_io(inode); 5121da177e4SLinus Torvalds continue; /* blockdev has wrong queue */ 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 515d2caa3c5SJeff Layton /* 516d2caa3c5SJeff Layton * Was this inode dirtied after sync_sb_inodes was called? 517d2caa3c5SJeff Layton * This keeps sync from extra jobs and livelock. 518d2caa3c5SJeff Layton */ 519d2caa3c5SJeff Layton if (inode_dirtied_after(inode, start)) 5201da177e4SLinus Torvalds break; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /* Is another pdflush already flushing this queue? */ 5231da177e4SLinus Torvalds if (current_is_pdflush() && !writeback_acquire(bdi)) 5241da177e4SLinus Torvalds break; 5251da177e4SLinus Torvalds 52684a89245SWu Fengguang BUG_ON(inode->i_state & (I_FREEING | I_CLEAR)); 5271da177e4SLinus Torvalds __iget(inode); 5281da177e4SLinus Torvalds pages_skipped = wbc->pages_skipped; 5291da177e4SLinus Torvalds __writeback_single_inode(inode, wbc); 5301da177e4SLinus Torvalds if (current_is_pdflush()) 5311da177e4SLinus Torvalds writeback_release(bdi); 5321da177e4SLinus Torvalds if (wbc->pages_skipped != pages_skipped) { 5331da177e4SLinus Torvalds /* 5341da177e4SLinus Torvalds * writeback is not making progress due to locked 5351da177e4SLinus Torvalds * buffers. Skip this inode for now. 5361da177e4SLinus Torvalds */ 537f57b9b7bSAndrew Morton redirty_tail(inode); 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds spin_unlock(&inode_lock); 5401da177e4SLinus Torvalds iput(inode); 5414ffc8444SOGAWA Hirofumi cond_resched(); 5421da177e4SLinus Torvalds spin_lock(&inode_lock); 5438bc3be27SFengguang Wu if (wbc->nr_to_write <= 0) { 5448bc3be27SFengguang Wu wbc->more_io = 1; 5451da177e4SLinus Torvalds break; 5461da177e4SLinus Torvalds } 5478bc3be27SFengguang Wu if (!list_empty(&sb->s_more_io)) 5488bc3be27SFengguang Wu wbc->more_io = 1; 5498bc3be27SFengguang Wu } 55038f21977SNick Piggin 55138f21977SNick Piggin if (sync) { 55238f21977SNick Piggin struct inode *inode, *old_inode = NULL; 55338f21977SNick Piggin 55438f21977SNick Piggin /* 55538f21977SNick Piggin * Data integrity sync. Must wait for all pages under writeback, 55638f21977SNick Piggin * because there may have been pages dirtied before our sync 55738f21977SNick Piggin * call, but which had writeout started before we write it out. 55838f21977SNick Piggin * In which case, the inode may not be on the dirty list, but 55938f21977SNick Piggin * we still have to wait for that writeout. 56038f21977SNick Piggin */ 56138f21977SNick Piggin list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 56238f21977SNick Piggin struct address_space *mapping; 56338f21977SNick Piggin 564b6fac63cSWu Fengguang if (inode->i_state & 565b6fac63cSWu Fengguang (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW)) 56638f21977SNick Piggin continue; 56738f21977SNick Piggin mapping = inode->i_mapping; 56838f21977SNick Piggin if (mapping->nrpages == 0) 56938f21977SNick Piggin continue; 57038f21977SNick Piggin __iget(inode); 571ae8547b0SHans Reiser spin_unlock(&inode_lock); 57238f21977SNick Piggin /* 57338f21977SNick Piggin * We hold a reference to 'inode' so it couldn't have 57438f21977SNick Piggin * been removed from s_inodes list while we dropped the 57538f21977SNick Piggin * inode_lock. We cannot iput the inode now as we can 57638f21977SNick Piggin * be holding the last reference and we cannot iput it 57738f21977SNick Piggin * under inode_lock. So we keep the reference and iput 57838f21977SNick Piggin * it later. 57938f21977SNick Piggin */ 58038f21977SNick Piggin iput(old_inode); 58138f21977SNick Piggin old_inode = inode; 58238f21977SNick Piggin 58338f21977SNick Piggin filemap_fdatawait(mapping); 58438f21977SNick Piggin 58538f21977SNick Piggin cond_resched(); 58638f21977SNick Piggin 58738f21977SNick Piggin spin_lock(&inode_lock); 58838f21977SNick Piggin } 58938f21977SNick Piggin spin_unlock(&inode_lock); 59038f21977SNick Piggin iput(old_inode); 59138f21977SNick Piggin } else 59238f21977SNick Piggin spin_unlock(&inode_lock); 59338f21977SNick Piggin 5941da177e4SLinus Torvalds return; /* Leave any unwritten inodes on s_io */ 5951da177e4SLinus Torvalds } 5964ee6afd3SArtem Bityutskiy EXPORT_SYMBOL_GPL(generic_sync_sb_inodes); 5974ee6afd3SArtem Bityutskiy 5984ee6afd3SArtem Bityutskiy static void sync_sb_inodes(struct super_block *sb, 5994ee6afd3SArtem Bityutskiy struct writeback_control *wbc) 6004ee6afd3SArtem Bityutskiy { 6014ee6afd3SArtem Bityutskiy generic_sync_sb_inodes(sb, wbc); 6024ee6afd3SArtem Bityutskiy } 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds /* 6051da177e4SLinus Torvalds * Start writeback of dirty pagecache data against all unlocked inodes. 6061da177e4SLinus Torvalds * 6071da177e4SLinus Torvalds * Note: 6081da177e4SLinus Torvalds * We don't need to grab a reference to superblock here. If it has non-empty 6091da177e4SLinus Torvalds * ->s_dirty it's hadn't been killed yet and kill_super() won't proceed 6102c136579SFengguang Wu * past sync_inodes_sb() until the ->s_dirty/s_io/s_more_io lists are all 6111da177e4SLinus Torvalds * empty. Since __sync_single_inode() regains inode_lock before it finally moves 6121da177e4SLinus Torvalds * inode from superblock lists we are OK. 6131da177e4SLinus Torvalds * 6141da177e4SLinus Torvalds * If `older_than_this' is non-zero then only flush inodes which have a 6151da177e4SLinus Torvalds * flushtime older than *older_than_this. 6161da177e4SLinus Torvalds * 6171da177e4SLinus Torvalds * If `bdi' is non-zero then we will scan the first inode against each 6181da177e4SLinus Torvalds * superblock until we find the matching ones. One group will be the dirty 6191da177e4SLinus Torvalds * inodes against a filesystem. Then when we hit the dummy blockdev superblock, 6201da177e4SLinus Torvalds * sync_sb_inodes will seekout the blockdev which matches `bdi'. Maybe not 6211da177e4SLinus Torvalds * super-efficient but we're about to do a ton of I/O... 6221da177e4SLinus Torvalds */ 6231da177e4SLinus Torvalds void 6241da177e4SLinus Torvalds writeback_inodes(struct writeback_control *wbc) 6251da177e4SLinus Torvalds { 6261da177e4SLinus Torvalds struct super_block *sb; 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds might_sleep(); 6291da177e4SLinus Torvalds spin_lock(&sb_lock); 6301da177e4SLinus Torvalds restart: 631797074e4SAkinobu Mita list_for_each_entry_reverse(sb, &super_blocks, s_list) { 63208d8e974SFengguang Wu if (sb_has_dirty_inodes(sb)) { 6331da177e4SLinus Torvalds /* we're making our own get_super here */ 6341da177e4SLinus Torvalds sb->s_count++; 6351da177e4SLinus Torvalds spin_unlock(&sb_lock); 6361da177e4SLinus Torvalds /* 6371da177e4SLinus Torvalds * If we can't get the readlock, there's no sense in 6381da177e4SLinus Torvalds * waiting around, most of the time the FS is going to 6391da177e4SLinus Torvalds * be unmounted by the time it is released. 6401da177e4SLinus Torvalds */ 6411da177e4SLinus Torvalds if (down_read_trylock(&sb->s_umount)) { 642ae8547b0SHans Reiser if (sb->s_root) 6431da177e4SLinus Torvalds sync_sb_inodes(sb, wbc); 6441da177e4SLinus Torvalds up_read(&sb->s_umount); 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds spin_lock(&sb_lock); 6471da177e4SLinus Torvalds if (__put_super_and_need_restart(sb)) 6481da177e4SLinus Torvalds goto restart; 6491da177e4SLinus Torvalds } 6501da177e4SLinus Torvalds if (wbc->nr_to_write <= 0) 6511da177e4SLinus Torvalds break; 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds spin_unlock(&sb_lock); 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds /* 6571da177e4SLinus Torvalds * writeback and wait upon the filesystem's dirty inodes. The caller will 6584f5a99d6SNick Piggin * do this in two passes - one to write, and one to wait. 6591da177e4SLinus Torvalds * 6601da177e4SLinus Torvalds * A finite limit is set on the number of pages which will be written. 6611da177e4SLinus Torvalds * To prevent infinite livelock of sys_sync(). 6621da177e4SLinus Torvalds * 6631da177e4SLinus Torvalds * We add in the number of potentially dirty inodes, because each inode write 6641da177e4SLinus Torvalds * can dirty pagecache in the underlying blockdev. 6651da177e4SLinus Torvalds */ 6661da177e4SLinus Torvalds void sync_inodes_sb(struct super_block *sb, int wait) 6671da177e4SLinus Torvalds { 6681da177e4SLinus Torvalds struct writeback_control wbc = { 6694f5a99d6SNick Piggin .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, 670111ebb6eSOGAWA Hirofumi .range_start = 0, 671111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 6721da177e4SLinus Torvalds }; 67338f21977SNick Piggin 67438f21977SNick Piggin if (!wait) { 675b1e7a8fdSChristoph Lameter unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY); 676fd39fc85SChristoph Lameter unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS); 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds wbc.nr_to_write = nr_dirty + nr_unstable + 67938f21977SNick Piggin (inodes_stat.nr_inodes - inodes_stat.nr_unused); 68038f21977SNick Piggin } else 68138f21977SNick Piggin wbc.nr_to_write = LONG_MAX; /* doesn't actually matter */ 68238f21977SNick Piggin 6831da177e4SLinus Torvalds sync_sb_inodes(sb, &wbc); 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds /** 6871da177e4SLinus Torvalds * write_inode_now - write an inode to disk 6881da177e4SLinus Torvalds * @inode: inode to write to disk 6891da177e4SLinus Torvalds * @sync: whether the write should be synchronous or not 6901da177e4SLinus Torvalds * 6917f04c26dSAndrea Arcangeli * This function commits an inode to disk immediately if it is dirty. This is 6927f04c26dSAndrea Arcangeli * primarily needed by knfsd. 6937f04c26dSAndrea Arcangeli * 6947f04c26dSAndrea Arcangeli * The caller must either have a ref on the inode or must have set I_WILL_FREE. 6951da177e4SLinus Torvalds */ 6961da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync) 6971da177e4SLinus Torvalds { 6981da177e4SLinus Torvalds int ret; 6991da177e4SLinus Torvalds struct writeback_control wbc = { 7001da177e4SLinus Torvalds .nr_to_write = LONG_MAX, 70118914b18SMike Galbraith .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 702111ebb6eSOGAWA Hirofumi .range_start = 0, 703111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 7041da177e4SLinus Torvalds }; 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds if (!mapping_cap_writeback_dirty(inode->i_mapping)) 70749364ce2SAndrew Morton wbc.nr_to_write = 0; 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds might_sleep(); 7101da177e4SLinus Torvalds spin_lock(&inode_lock); 7111da177e4SLinus Torvalds ret = __writeback_single_inode(inode, &wbc); 7121da177e4SLinus Torvalds spin_unlock(&inode_lock); 7131da177e4SLinus Torvalds if (sync) 7141c0eeaf5SJoern Engel inode_sync_wait(inode); 7151da177e4SLinus Torvalds return ret; 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now); 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds /** 7201da177e4SLinus Torvalds * sync_inode - write an inode and its pages to disk. 7211da177e4SLinus Torvalds * @inode: the inode to sync 7221da177e4SLinus Torvalds * @wbc: controls the writeback mode 7231da177e4SLinus Torvalds * 7241da177e4SLinus Torvalds * sync_inode() will write an inode and its pages to disk. It will also 7251da177e4SLinus Torvalds * correctly update the inode on its superblock's dirty inode lists and will 7261da177e4SLinus Torvalds * update inode->i_state. 7271da177e4SLinus Torvalds * 7281da177e4SLinus Torvalds * The caller must have a ref on the inode. 7291da177e4SLinus Torvalds */ 7301da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc) 7311da177e4SLinus Torvalds { 7321da177e4SLinus Torvalds int ret; 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds spin_lock(&inode_lock); 7351da177e4SLinus Torvalds ret = __writeback_single_inode(inode, wbc); 7361da177e4SLinus Torvalds spin_unlock(&inode_lock); 7371da177e4SLinus Torvalds return ret; 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode); 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds /** 7421da177e4SLinus Torvalds * generic_osync_inode - flush all dirty data for a given inode to disk 7431da177e4SLinus Torvalds * @inode: inode to write 74467be2dd1SMartin Waitz * @mapping: the address_space that should be flushed 7451da177e4SLinus Torvalds * @what: what to write and wait upon 7461da177e4SLinus Torvalds * 7471da177e4SLinus Torvalds * This can be called by file_write functions for files which have the 7481da177e4SLinus Torvalds * O_SYNC flag set, to flush dirty writes to disk. 7491da177e4SLinus Torvalds * 7501da177e4SLinus Torvalds * @what is a bitmask, specifying which part of the inode's data should be 751b8887e6eSRandy Dunlap * written and waited upon. 7521da177e4SLinus Torvalds * 7531da177e4SLinus Torvalds * OSYNC_DATA: i_mapping's dirty data 7541da177e4SLinus Torvalds * OSYNC_METADATA: the buffers at i_mapping->private_list 7551da177e4SLinus Torvalds * OSYNC_INODE: the inode itself 7561da177e4SLinus Torvalds */ 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds int generic_osync_inode(struct inode *inode, struct address_space *mapping, int what) 7591da177e4SLinus Torvalds { 7601da177e4SLinus Torvalds int err = 0; 7611da177e4SLinus Torvalds int need_write_inode_now = 0; 7621da177e4SLinus Torvalds int err2; 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds if (what & OSYNC_DATA) 7651da177e4SLinus Torvalds err = filemap_fdatawrite(mapping); 7661da177e4SLinus Torvalds if (what & (OSYNC_METADATA|OSYNC_DATA)) { 7671da177e4SLinus Torvalds err2 = sync_mapping_buffers(mapping); 7681da177e4SLinus Torvalds if (!err) 7691da177e4SLinus Torvalds err = err2; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds if (what & OSYNC_DATA) { 7721da177e4SLinus Torvalds err2 = filemap_fdatawait(mapping); 7731da177e4SLinus Torvalds if (!err) 7741da177e4SLinus Torvalds err = err2; 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds spin_lock(&inode_lock); 7781da177e4SLinus Torvalds if ((inode->i_state & I_DIRTY) && 7791da177e4SLinus Torvalds ((what & OSYNC_INODE) || (inode->i_state & I_DIRTY_DATASYNC))) 7801da177e4SLinus Torvalds need_write_inode_now = 1; 7811da177e4SLinus Torvalds spin_unlock(&inode_lock); 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds if (need_write_inode_now) { 7841da177e4SLinus Torvalds err2 = write_inode_now(inode, 1); 7851da177e4SLinus Torvalds if (!err) 7861da177e4SLinus Torvalds err = err2; 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds else 7891c0eeaf5SJoern Engel inode_sync_wait(inode); 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds return err; 7921da177e4SLinus Torvalds } 7931da177e4SLinus Torvalds EXPORT_SYMBOL(generic_osync_inode); 794