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> 195a0e3ad6STejun Heo #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/sched.h> 211da177e4SLinus Torvalds #include <linux/fs.h> 221da177e4SLinus Torvalds #include <linux/mm.h> 2303ba3782SJens Axboe #include <linux/kthread.h> 2403ba3782SJens Axboe #include <linux/freezer.h> 251da177e4SLinus Torvalds #include <linux/writeback.h> 261da177e4SLinus Torvalds #include <linux/blkdev.h> 271da177e4SLinus Torvalds #include <linux/backing-dev.h> 281da177e4SLinus Torvalds #include <linux/buffer_head.h> 29455b2864SDave Chinner #include <linux/tracepoint.h> 3007f3f05cSDavid Howells #include "internal.h" 311da177e4SLinus Torvalds 32d0bceac7SJens Axboe /* 33c4a77a6cSJens Axboe * Passed into wb_writeback(), essentially a subset of writeback_control 34c4a77a6cSJens Axboe */ 3583ba7b07SChristoph Hellwig struct wb_writeback_work { 36c4a77a6cSJens Axboe long nr_pages; 37c4a77a6cSJens Axboe struct super_block *sb; 38c4a77a6cSJens Axboe enum writeback_sync_modes sync_mode; 3952957fe1SH Hartley Sweeten unsigned int for_kupdate:1; 4052957fe1SH Hartley Sweeten unsigned int range_cyclic:1; 4152957fe1SH Hartley Sweeten unsigned int for_background:1; 42c4a77a6cSJens Axboe 438010c3b6SJens Axboe struct list_head list; /* pending work list */ 4483ba7b07SChristoph Hellwig struct completion *done; /* set if the caller waits */ 4503ba3782SJens Axboe }; 4603ba3782SJens Axboe 47455b2864SDave Chinner /* 48455b2864SDave Chinner * Include the creation of the trace points after defining the 49455b2864SDave Chinner * wb_writeback_work structure so that the definition remains local to this 50455b2864SDave Chinner * file. 51455b2864SDave Chinner */ 52455b2864SDave Chinner #define CREATE_TRACE_POINTS 53455b2864SDave Chinner #include <trace/events/writeback.h> 54455b2864SDave Chinner 55455b2864SDave Chinner #define inode_to_bdi(inode) ((inode)->i_mapping->backing_dev_info) 56455b2864SDave Chinner 57455b2864SDave Chinner /* 58455b2864SDave Chinner * We don't actually have pdflush, but this one is exported though /proc... 59455b2864SDave Chinner */ 60455b2864SDave Chinner int nr_pdflush_threads; 61455b2864SDave Chinner 62f11b00f3SAdrian Bunk /** 63f11b00f3SAdrian Bunk * writeback_in_progress - determine whether there is writeback in progress 64f11b00f3SAdrian Bunk * @bdi: the device's backing_dev_info structure. 65f11b00f3SAdrian Bunk * 6603ba3782SJens Axboe * Determine whether there is writeback waiting to be handled against a 6703ba3782SJens Axboe * backing device. 68f11b00f3SAdrian Bunk */ 69f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi) 70f11b00f3SAdrian Bunk { 7103ba3782SJens Axboe return !list_empty(&bdi->work_list); 72f11b00f3SAdrian Bunk } 73f11b00f3SAdrian Bunk 7483ba7b07SChristoph Hellwig static void bdi_queue_work(struct backing_dev_info *bdi, 7583ba7b07SChristoph Hellwig struct wb_writeback_work *work) 764195f73dSNick Piggin { 77455b2864SDave Chinner trace_writeback_queue(bdi, work); 78455b2864SDave Chinner 7903ba3782SJens Axboe spin_lock(&bdi->wb_lock); 8083ba7b07SChristoph Hellwig list_add_tail(&work->list, &bdi->work_list); 8103ba3782SJens Axboe spin_unlock(&bdi->wb_lock); 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds /* 8403ba3782SJens Axboe * If the default thread isn't there, make sure we add it. When 8503ba3782SJens Axboe * it gets created and wakes up, we'll run this work. 861da177e4SLinus Torvalds */ 87c1955ce3SChristoph Hellwig if (unlikely(!bdi->wb.task)) { 88455b2864SDave Chinner trace_writeback_nothread(bdi, work); 8903ba3782SJens Axboe wake_up_process(default_backing_dev_info.wb.task); 90c1955ce3SChristoph Hellwig } else { 9103ba3782SJens Axboe struct bdi_writeback *wb = &bdi->wb; 921da177e4SLinus Torvalds 931ef7d9aaSNick Piggin if (wb->task) 9403ba3782SJens Axboe wake_up_process(wb->task); 951da177e4SLinus Torvalds } 9603ba3782SJens Axboe } 971da177e4SLinus Torvalds 9883ba7b07SChristoph Hellwig static void 9983ba7b07SChristoph Hellwig __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages, 10083ba7b07SChristoph Hellwig bool range_cyclic, bool for_background) 1011da177e4SLinus Torvalds { 10283ba7b07SChristoph Hellwig struct wb_writeback_work *work; 10303ba3782SJens Axboe 104bcddc3f0SJens Axboe /* 105bcddc3f0SJens Axboe * This is WB_SYNC_NONE writeback, so if allocation fails just 106bcddc3f0SJens Axboe * wakeup the thread for old dirty data writeback 107bcddc3f0SJens Axboe */ 10883ba7b07SChristoph Hellwig work = kzalloc(sizeof(*work), GFP_ATOMIC); 10983ba7b07SChristoph Hellwig if (!work) { 110455b2864SDave Chinner if (bdi->wb.task) { 111455b2864SDave Chinner trace_writeback_nowork(bdi); 11283ba7b07SChristoph Hellwig wake_up_process(bdi->wb.task); 113455b2864SDave Chinner } 11483ba7b07SChristoph Hellwig return; 11583ba7b07SChristoph Hellwig } 11683ba7b07SChristoph Hellwig 11783ba7b07SChristoph Hellwig work->sync_mode = WB_SYNC_NONE; 11883ba7b07SChristoph Hellwig work->nr_pages = nr_pages; 11983ba7b07SChristoph Hellwig work->range_cyclic = range_cyclic; 12083ba7b07SChristoph Hellwig work->for_background = for_background; 12183ba7b07SChristoph Hellwig 122f11fcae8SJens Axboe bdi_queue_work(bdi, work); 12303ba3782SJens Axboe } 124b6e51316SJens Axboe 125b6e51316SJens Axboe /** 126b6e51316SJens Axboe * bdi_start_writeback - start writeback 127b6e51316SJens Axboe * @bdi: the backing device to write from 128b6e51316SJens Axboe * @nr_pages: the number of pages to write 129b6e51316SJens Axboe * 130b6e51316SJens Axboe * Description: 131b6e51316SJens Axboe * This does WB_SYNC_NONE opportunistic writeback. The IO is only 132b6e51316SJens Axboe * started when this function returns, we make no guarentees on 1330e3c9a22SJens Axboe * completion. Caller need not hold sb s_umount semaphore. 134b6e51316SJens Axboe * 135b6e51316SJens Axboe */ 136c5444198SChristoph Hellwig void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages) 137b6e51316SJens Axboe { 13883ba7b07SChristoph Hellwig __bdi_start_writeback(bdi, nr_pages, true, false); 139d3ddec76SWu Fengguang } 140d3ddec76SWu Fengguang 141c5444198SChristoph Hellwig /** 142c5444198SChristoph Hellwig * bdi_start_background_writeback - start background writeback 143c5444198SChristoph Hellwig * @bdi: the backing device to write from 144c5444198SChristoph Hellwig * 145c5444198SChristoph Hellwig * Description: 146c5444198SChristoph Hellwig * This does WB_SYNC_NONE background writeback. The IO is only 147c5444198SChristoph Hellwig * started when this function returns, we make no guarentees on 148c5444198SChristoph Hellwig * completion. Caller need not hold sb s_umount semaphore. 149c5444198SChristoph Hellwig */ 150c5444198SChristoph Hellwig void bdi_start_background_writeback(struct backing_dev_info *bdi) 151c5444198SChristoph Hellwig { 15283ba7b07SChristoph Hellwig __bdi_start_writeback(bdi, LONG_MAX, true, true); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds /* 1566610a0bcSAndrew Morton * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 1576610a0bcSAndrew Morton * furthest end of its superblock's dirty-inode list. 1586610a0bcSAndrew Morton * 1596610a0bcSAndrew Morton * Before stamping the inode's ->dirtied_when, we check to see whether it is 16066f3b8e2SJens Axboe * already the most-recently-dirtied inode on the b_dirty list. If that is 1616610a0bcSAndrew Morton * the case then the inode must have been redirtied while it was being written 1626610a0bcSAndrew Morton * out and we don't reset its dirtied_when. 1636610a0bcSAndrew Morton */ 1646610a0bcSAndrew Morton static void redirty_tail(struct inode *inode) 1656610a0bcSAndrew Morton { 16603ba3782SJens Axboe struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 1676610a0bcSAndrew Morton 16803ba3782SJens Axboe if (!list_empty(&wb->b_dirty)) { 16966f3b8e2SJens Axboe struct inode *tail; 1706610a0bcSAndrew Morton 17103ba3782SJens Axboe tail = list_entry(wb->b_dirty.next, struct inode, i_list); 17266f3b8e2SJens Axboe if (time_before(inode->dirtied_when, tail->dirtied_when)) 1736610a0bcSAndrew Morton inode->dirtied_when = jiffies; 1746610a0bcSAndrew Morton } 17503ba3782SJens Axboe list_move(&inode->i_list, &wb->b_dirty); 1766610a0bcSAndrew Morton } 1776610a0bcSAndrew Morton 1786610a0bcSAndrew Morton /* 17966f3b8e2SJens Axboe * requeue inode for re-scanning after bdi->b_io list is exhausted. 180c986d1e2SAndrew Morton */ 1810e0f4fc2SKen Chen static void requeue_io(struct inode *inode) 182c986d1e2SAndrew Morton { 18303ba3782SJens Axboe struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 18403ba3782SJens Axboe 18503ba3782SJens Axboe list_move(&inode->i_list, &wb->b_more_io); 186c986d1e2SAndrew Morton } 187c986d1e2SAndrew Morton 1881c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode) 1891c0eeaf5SJoern Engel { 1901c0eeaf5SJoern Engel /* 1911c0eeaf5SJoern Engel * Prevent speculative execution through spin_unlock(&inode_lock); 1921c0eeaf5SJoern Engel */ 1931c0eeaf5SJoern Engel smp_mb(); 1941c0eeaf5SJoern Engel wake_up_bit(&inode->i_state, __I_SYNC); 1951c0eeaf5SJoern Engel } 1961c0eeaf5SJoern Engel 197d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t) 198d2caa3c5SJeff Layton { 199d2caa3c5SJeff Layton bool ret = time_after(inode->dirtied_when, t); 200d2caa3c5SJeff Layton #ifndef CONFIG_64BIT 201d2caa3c5SJeff Layton /* 202d2caa3c5SJeff Layton * For inodes being constantly redirtied, dirtied_when can get stuck. 203d2caa3c5SJeff Layton * It _appears_ to be in the future, but is actually in distant past. 204d2caa3c5SJeff Layton * This test is necessary to prevent such wrapped-around relative times 2055b0830cbSJens Axboe * from permanently stopping the whole bdi writeback. 206d2caa3c5SJeff Layton */ 207d2caa3c5SJeff Layton ret = ret && time_before_eq(inode->dirtied_when, jiffies); 208d2caa3c5SJeff Layton #endif 209d2caa3c5SJeff Layton return ret; 210d2caa3c5SJeff Layton } 211d2caa3c5SJeff Layton 212c986d1e2SAndrew Morton /* 2132c136579SFengguang Wu * Move expired dirty inodes from @delaying_queue to @dispatch_queue. 2142c136579SFengguang Wu */ 2152c136579SFengguang Wu static void move_expired_inodes(struct list_head *delaying_queue, 2162c136579SFengguang Wu struct list_head *dispatch_queue, 2172c136579SFengguang Wu unsigned long *older_than_this) 2182c136579SFengguang Wu { 2195c03449dSShaohua Li LIST_HEAD(tmp); 2205c03449dSShaohua Li struct list_head *pos, *node; 221cf137307SJens Axboe struct super_block *sb = NULL; 2225c03449dSShaohua Li struct inode *inode; 223cf137307SJens Axboe int do_sb_sort = 0; 2245c03449dSShaohua Li 2252c136579SFengguang Wu while (!list_empty(delaying_queue)) { 2265c03449dSShaohua Li inode = list_entry(delaying_queue->prev, struct inode, i_list); 2272c136579SFengguang Wu if (older_than_this && 228d2caa3c5SJeff Layton inode_dirtied_after(inode, *older_than_this)) 2292c136579SFengguang Wu break; 230cf137307SJens Axboe if (sb && sb != inode->i_sb) 231cf137307SJens Axboe do_sb_sort = 1; 232cf137307SJens Axboe sb = inode->i_sb; 2335c03449dSShaohua Li list_move(&inode->i_list, &tmp); 2345c03449dSShaohua Li } 2355c03449dSShaohua Li 236cf137307SJens Axboe /* just one sb in list, splice to dispatch_queue and we're done */ 237cf137307SJens Axboe if (!do_sb_sort) { 238cf137307SJens Axboe list_splice(&tmp, dispatch_queue); 239cf137307SJens Axboe return; 240cf137307SJens Axboe } 241cf137307SJens Axboe 2425c03449dSShaohua Li /* Move inodes from one superblock together */ 2435c03449dSShaohua Li while (!list_empty(&tmp)) { 2445c03449dSShaohua Li inode = list_entry(tmp.prev, struct inode, i_list); 2455c03449dSShaohua Li sb = inode->i_sb; 2465c03449dSShaohua Li list_for_each_prev_safe(pos, node, &tmp) { 2475c03449dSShaohua Li inode = list_entry(pos, struct inode, i_list); 2485c03449dSShaohua Li if (inode->i_sb == sb) 2492c136579SFengguang Wu list_move(&inode->i_list, dispatch_queue); 2502c136579SFengguang Wu } 2512c136579SFengguang Wu } 2525c03449dSShaohua Li } 2532c136579SFengguang Wu 2542c136579SFengguang Wu /* 2552c136579SFengguang Wu * Queue all expired dirty inodes for io, eldest first. 2562c136579SFengguang Wu */ 25703ba3782SJens Axboe static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this) 2582c136579SFengguang Wu { 25903ba3782SJens Axboe list_splice_init(&wb->b_more_io, wb->b_io.prev); 26003ba3782SJens Axboe move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this); 26166f3b8e2SJens Axboe } 26266f3b8e2SJens Axboe 263a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc) 26466f3b8e2SJens Axboe { 26503ba3782SJens Axboe if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) 266a9185b41SChristoph Hellwig return inode->i_sb->s_op->write_inode(inode, wbc); 26703ba3782SJens Axboe return 0; 26866f3b8e2SJens Axboe } 26908d8e974SFengguang Wu 2702c136579SFengguang Wu /* 27101c03194SChristoph Hellwig * Wait for writeback on an inode to complete. 27201c03194SChristoph Hellwig */ 27301c03194SChristoph Hellwig static void inode_wait_for_writeback(struct inode *inode) 27401c03194SChristoph Hellwig { 27501c03194SChristoph Hellwig DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); 27601c03194SChristoph Hellwig wait_queue_head_t *wqh; 27701c03194SChristoph Hellwig 27801c03194SChristoph Hellwig wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 27958a9d3d8SRichard Kennedy while (inode->i_state & I_SYNC) { 28001c03194SChristoph Hellwig spin_unlock(&inode_lock); 28101c03194SChristoph Hellwig __wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE); 28201c03194SChristoph Hellwig spin_lock(&inode_lock); 28358a9d3d8SRichard Kennedy } 28401c03194SChristoph Hellwig } 28501c03194SChristoph Hellwig 28601c03194SChristoph Hellwig /* 28701c03194SChristoph Hellwig * Write out an inode's dirty pages. Called under inode_lock. Either the 28801c03194SChristoph Hellwig * caller has ref on the inode (either via __iget or via syscall against an fd) 28901c03194SChristoph Hellwig * or the inode has I_WILL_FREE set (via generic_forget_inode) 29001c03194SChristoph Hellwig * 2911da177e4SLinus Torvalds * If `wait' is set, wait on the writeout. 2921da177e4SLinus Torvalds * 2931da177e4SLinus Torvalds * The whole writeout design is quite complex and fragile. We want to avoid 2941da177e4SLinus Torvalds * starvation of particular inodes when others are being redirtied, prevent 2951da177e4SLinus Torvalds * livelocks, etc. 2961da177e4SLinus Torvalds * 2971da177e4SLinus Torvalds * Called under inode_lock. 2981da177e4SLinus Torvalds */ 2991da177e4SLinus Torvalds static int 30001c03194SChristoph Hellwig writeback_single_inode(struct inode *inode, struct writeback_control *wbc) 3011da177e4SLinus Torvalds { 3021da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 30301c03194SChristoph Hellwig unsigned dirty; 3041da177e4SLinus Torvalds int ret; 3051da177e4SLinus Torvalds 30601c03194SChristoph Hellwig if (!atomic_read(&inode->i_count)) 30701c03194SChristoph Hellwig WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); 30801c03194SChristoph Hellwig else 30901c03194SChristoph Hellwig WARN_ON(inode->i_state & I_WILL_FREE); 31001c03194SChristoph Hellwig 31101c03194SChristoph Hellwig if (inode->i_state & I_SYNC) { 31201c03194SChristoph Hellwig /* 31301c03194SChristoph Hellwig * If this inode is locked for writeback and we are not doing 31466f3b8e2SJens Axboe * writeback-for-data-integrity, move it to b_more_io so that 31501c03194SChristoph Hellwig * writeback can proceed with the other inodes on s_io. 31601c03194SChristoph Hellwig * 31701c03194SChristoph Hellwig * We'll have another go at writing back this inode when we 31866f3b8e2SJens Axboe * completed a full scan of b_io. 31901c03194SChristoph Hellwig */ 320a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) { 32101c03194SChristoph Hellwig requeue_io(inode); 32201c03194SChristoph Hellwig return 0; 32301c03194SChristoph Hellwig } 32401c03194SChristoph Hellwig 32501c03194SChristoph Hellwig /* 32601c03194SChristoph Hellwig * It's a data-integrity sync. We must wait. 32701c03194SChristoph Hellwig */ 32801c03194SChristoph Hellwig inode_wait_for_writeback(inode); 32901c03194SChristoph Hellwig } 33001c03194SChristoph Hellwig 3311c0eeaf5SJoern Engel BUG_ON(inode->i_state & I_SYNC); 3321da177e4SLinus Torvalds 3335547e8aaSDmitry Monakhov /* Set I_SYNC, reset I_DIRTY_PAGES */ 3341c0eeaf5SJoern Engel inode->i_state |= I_SYNC; 3355547e8aaSDmitry Monakhov inode->i_state &= ~I_DIRTY_PAGES; 3361da177e4SLinus Torvalds spin_unlock(&inode_lock); 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds ret = do_writepages(mapping, wbc); 3391da177e4SLinus Torvalds 34026821ed4SChristoph Hellwig /* 34126821ed4SChristoph Hellwig * Make sure to wait on the data before writing out the metadata. 34226821ed4SChristoph Hellwig * This is important for filesystems that modify metadata on data 34326821ed4SChristoph Hellwig * I/O completion. 34426821ed4SChristoph Hellwig */ 345a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) { 34626821ed4SChristoph Hellwig int err = filemap_fdatawait(mapping); 3471da177e4SLinus Torvalds if (ret == 0) 3481da177e4SLinus Torvalds ret = err; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds 3515547e8aaSDmitry Monakhov /* 3525547e8aaSDmitry Monakhov * Some filesystems may redirty the inode during the writeback 3535547e8aaSDmitry Monakhov * due to delalloc, clear dirty metadata flags right before 3545547e8aaSDmitry Monakhov * write_inode() 3555547e8aaSDmitry Monakhov */ 3565547e8aaSDmitry Monakhov spin_lock(&inode_lock); 3575547e8aaSDmitry Monakhov dirty = inode->i_state & I_DIRTY; 3585547e8aaSDmitry Monakhov inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC); 3595547e8aaSDmitry Monakhov spin_unlock(&inode_lock); 36026821ed4SChristoph Hellwig /* Don't write the inode if only I_DIRTY_PAGES was set */ 36126821ed4SChristoph Hellwig if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { 362a9185b41SChristoph Hellwig int err = write_inode(inode, wbc); 3631da177e4SLinus Torvalds if (ret == 0) 3641da177e4SLinus Torvalds ret = err; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds spin_lock(&inode_lock); 3681c0eeaf5SJoern Engel inode->i_state &= ~I_SYNC; 36984a89245SWu Fengguang if (!(inode->i_state & (I_FREEING | I_CLEAR))) { 370b3af9468SWu Fengguang if ((inode->i_state & I_DIRTY_PAGES) && wbc->for_kupdate) { 371ae1b7f7dSWu Fengguang /* 372b3af9468SWu Fengguang * More pages get dirtied by a fast dirtier. 373b3af9468SWu Fengguang */ 374b3af9468SWu Fengguang goto select_queue; 375b3af9468SWu Fengguang } else if (inode->i_state & I_DIRTY) { 376b3af9468SWu Fengguang /* 377b3af9468SWu Fengguang * At least XFS will redirty the inode during the 378b3af9468SWu Fengguang * writeback (delalloc) and on io completion (isize). 379ae1b7f7dSWu Fengguang */ 380ae1b7f7dSWu Fengguang redirty_tail(inode); 381ae1b7f7dSWu Fengguang } else if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 3821da177e4SLinus Torvalds /* 3831da177e4SLinus Torvalds * We didn't write back all the pages. nfs_writepages() 3841da177e4SLinus Torvalds * sometimes bales out without doing anything. Redirty 38566f3b8e2SJens Axboe * the inode; Move it from b_io onto b_more_io/b_dirty. 3861b43ef91SAndrew Morton */ 3871b43ef91SAndrew Morton /* 3881b43ef91SAndrew Morton * akpm: if the caller was the kupdate function we put 38966f3b8e2SJens Axboe * this inode at the head of b_dirty so it gets first 3901b43ef91SAndrew Morton * consideration. Otherwise, move it to the tail, for 3911b43ef91SAndrew Morton * the reasons described there. I'm not really sure 3921b43ef91SAndrew Morton * how much sense this makes. Presumably I had a good 3931b43ef91SAndrew Morton * reasons for doing it this way, and I'd rather not 3941b43ef91SAndrew Morton * muck with it at present. 3951da177e4SLinus Torvalds */ 3961da177e4SLinus Torvalds if (wbc->for_kupdate) { 3971da177e4SLinus Torvalds /* 3982c136579SFengguang Wu * For the kupdate function we move the inode 39966f3b8e2SJens Axboe * to b_more_io so it will get more writeout as 4002c136579SFengguang Wu * soon as the queue becomes uncongested. 4011da177e4SLinus Torvalds */ 4021da177e4SLinus Torvalds inode->i_state |= I_DIRTY_PAGES; 403b3af9468SWu Fengguang select_queue: 4048bc3be27SFengguang Wu if (wbc->nr_to_write <= 0) { 4058bc3be27SFengguang Wu /* 4068bc3be27SFengguang Wu * slice used up: queue for next turn 4078bc3be27SFengguang Wu */ 4080e0f4fc2SKen Chen requeue_io(inode); 4091da177e4SLinus Torvalds } else { 4101da177e4SLinus Torvalds /* 4118bc3be27SFengguang Wu * somehow blocked: retry later 4128bc3be27SFengguang Wu */ 4138bc3be27SFengguang Wu redirty_tail(inode); 4148bc3be27SFengguang Wu } 4158bc3be27SFengguang Wu } else { 4168bc3be27SFengguang Wu /* 4171da177e4SLinus Torvalds * Otherwise fully redirty the inode so that 4181da177e4SLinus Torvalds * other inodes on this superblock will get some 4191da177e4SLinus Torvalds * writeout. Otherwise heavy writing to one 4201da177e4SLinus Torvalds * file would indefinitely suspend writeout of 4211da177e4SLinus Torvalds * all the other files. 4221da177e4SLinus Torvalds */ 4231da177e4SLinus Torvalds inode->i_state |= I_DIRTY_PAGES; 4241b43ef91SAndrew Morton redirty_tail(inode); 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds } else if (atomic_read(&inode->i_count)) { 4271da177e4SLinus Torvalds /* 4281da177e4SLinus Torvalds * The inode is clean, inuse 4291da177e4SLinus Torvalds */ 4301da177e4SLinus Torvalds list_move(&inode->i_list, &inode_in_use); 4311da177e4SLinus Torvalds } else { 4321da177e4SLinus Torvalds /* 4331da177e4SLinus Torvalds * The inode is clean, unused 4341da177e4SLinus Torvalds */ 4351da177e4SLinus Torvalds list_move(&inode->i_list, &inode_unused); 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds } 4381c0eeaf5SJoern Engel inode_sync_complete(inode); 4391da177e4SLinus Torvalds return ret; 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds 44203ba3782SJens Axboe /* 443d19de7edSChristoph Hellwig * For background writeback the caller does not have the sb pinned 44403ba3782SJens Axboe * before calling writeback. So make sure that we do pin it, so it doesn't 44503ba3782SJens Axboe * go away while we are writing inodes from it. 44603ba3782SJens Axboe */ 447d19de7edSChristoph Hellwig static bool pin_sb_for_writeback(struct super_block *sb) 4481da177e4SLinus Torvalds { 44903ba3782SJens Axboe spin_lock(&sb_lock); 45029cb4859SChristoph Hellwig if (list_empty(&sb->s_instances)) { 45103ba3782SJens Axboe spin_unlock(&sb_lock); 45229cb4859SChristoph Hellwig return false; 45303ba3782SJens Axboe } 45429cb4859SChristoph Hellwig 45529cb4859SChristoph Hellwig sb->s_count++; 45629cb4859SChristoph Hellwig spin_unlock(&sb_lock); 45729cb4859SChristoph Hellwig 45829cb4859SChristoph Hellwig if (down_read_trylock(&sb->s_umount)) { 45929cb4859SChristoph Hellwig if (sb->s_root) 46029cb4859SChristoph Hellwig return true; 46103ba3782SJens Axboe up_read(&sb->s_umount); 46203ba3782SJens Axboe } 46329cb4859SChristoph Hellwig 46429cb4859SChristoph Hellwig put_super(sb); 465d19de7edSChristoph Hellwig return false; 46603ba3782SJens Axboe } 46703ba3782SJens Axboe 468f11c9c5cSEdward Shishkin /* 469f11c9c5cSEdward Shishkin * Write a portion of b_io inodes which belong to @sb. 470edadfb10SChristoph Hellwig * 471edadfb10SChristoph Hellwig * If @only_this_sb is true, then find and write all such 472f11c9c5cSEdward Shishkin * inodes. Otherwise write only ones which go sequentially 473f11c9c5cSEdward Shishkin * in reverse order. 474edadfb10SChristoph Hellwig * 475f11c9c5cSEdward Shishkin * Return 1, if the caller writeback routine should be 476f11c9c5cSEdward Shishkin * interrupted. Otherwise return 0. 477f11c9c5cSEdward Shishkin */ 478edadfb10SChristoph Hellwig static int writeback_sb_inodes(struct super_block *sb, struct bdi_writeback *wb, 479edadfb10SChristoph Hellwig struct writeback_control *wbc, bool only_this_sb) 48003ba3782SJens Axboe { 48103ba3782SJens Axboe while (!list_empty(&wb->b_io)) { 482f11c9c5cSEdward Shishkin long pages_skipped; 48303ba3782SJens Axboe struct inode *inode = list_entry(wb->b_io.prev, 4841da177e4SLinus Torvalds struct inode, i_list); 485edadfb10SChristoph Hellwig 486edadfb10SChristoph Hellwig if (inode->i_sb != sb) { 487edadfb10SChristoph Hellwig if (only_this_sb) { 488edadfb10SChristoph Hellwig /* 489edadfb10SChristoph Hellwig * We only want to write back data for this 490edadfb10SChristoph Hellwig * superblock, move all inodes not belonging 491edadfb10SChristoph Hellwig * to it back onto the dirty list. 492edadfb10SChristoph Hellwig */ 49366f3b8e2SJens Axboe redirty_tail(inode); 49466f3b8e2SJens Axboe continue; 49566f3b8e2SJens Axboe } 496edadfb10SChristoph Hellwig 497edadfb10SChristoph Hellwig /* 498edadfb10SChristoph Hellwig * The inode belongs to a different superblock. 499edadfb10SChristoph Hellwig * Bounce back to the caller to unpin this and 500edadfb10SChristoph Hellwig * pin the next superblock. 501edadfb10SChristoph Hellwig */ 502f11c9c5cSEdward Shishkin return 0; 503edadfb10SChristoph Hellwig } 504edadfb10SChristoph Hellwig 50584a89245SWu Fengguang if (inode->i_state & (I_NEW | I_WILL_FREE)) { 5067ef0d737SNick Piggin requeue_io(inode); 5077ef0d737SNick Piggin continue; 5087ef0d737SNick Piggin } 509d2caa3c5SJeff Layton /* 510d2caa3c5SJeff Layton * Was this inode dirtied after sync_sb_inodes was called? 511d2caa3c5SJeff Layton * This keeps sync from extra jobs and livelock. 512d2caa3c5SJeff Layton */ 513f11c9c5cSEdward Shishkin if (inode_dirtied_after(inode, wbc->wb_start)) 514f11c9c5cSEdward Shishkin return 1; 5151da177e4SLinus Torvalds 51684a89245SWu Fengguang BUG_ON(inode->i_state & (I_FREEING | I_CLEAR)); 5171da177e4SLinus Torvalds __iget(inode); 5181da177e4SLinus Torvalds pages_skipped = wbc->pages_skipped; 51901c03194SChristoph Hellwig writeback_single_inode(inode, wbc); 5201da177e4SLinus Torvalds if (wbc->pages_skipped != pages_skipped) { 5211da177e4SLinus Torvalds /* 5221da177e4SLinus Torvalds * writeback is not making progress due to locked 5231da177e4SLinus Torvalds * buffers. Skip this inode for now. 5241da177e4SLinus Torvalds */ 525f57b9b7bSAndrew Morton redirty_tail(inode); 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds spin_unlock(&inode_lock); 5281da177e4SLinus Torvalds iput(inode); 5294ffc8444SOGAWA Hirofumi cond_resched(); 5301da177e4SLinus Torvalds spin_lock(&inode_lock); 5318bc3be27SFengguang Wu if (wbc->nr_to_write <= 0) { 5328bc3be27SFengguang Wu wbc->more_io = 1; 533f11c9c5cSEdward Shishkin return 1; 5341da177e4SLinus Torvalds } 53503ba3782SJens Axboe if (!list_empty(&wb->b_more_io)) 5368bc3be27SFengguang Wu wbc->more_io = 1; 5378bc3be27SFengguang Wu } 538f11c9c5cSEdward Shishkin /* b_io is empty */ 539f11c9c5cSEdward Shishkin return 1; 540f11c9c5cSEdward Shishkin } 54138f21977SNick Piggin 5429c3a8ee8SChristoph Hellwig void writeback_inodes_wb(struct bdi_writeback *wb, 543f11c9c5cSEdward Shishkin struct writeback_control *wbc) 544f11c9c5cSEdward Shishkin { 545f11c9c5cSEdward Shishkin int ret = 0; 5469ecc2738SJens Axboe 547f11c9c5cSEdward Shishkin wbc->wb_start = jiffies; /* livelock avoidance */ 548f11c9c5cSEdward Shishkin spin_lock(&inode_lock); 549f11c9c5cSEdward Shishkin if (!wbc->for_kupdate || list_empty(&wb->b_io)) 550f11c9c5cSEdward Shishkin queue_io(wb, wbc->older_than_this); 551f11c9c5cSEdward Shishkin 552f11c9c5cSEdward Shishkin while (!list_empty(&wb->b_io)) { 553f11c9c5cSEdward Shishkin struct inode *inode = list_entry(wb->b_io.prev, 554f11c9c5cSEdward Shishkin struct inode, i_list); 555f11c9c5cSEdward Shishkin struct super_block *sb = inode->i_sb; 556f11c9c5cSEdward Shishkin 557334132aeSChristoph Hellwig if (!pin_sb_for_writeback(sb)) { 558334132aeSChristoph Hellwig requeue_io(inode); 559d19de7edSChristoph Hellwig continue; 560334132aeSChristoph Hellwig } 561edadfb10SChristoph Hellwig ret = writeback_sb_inodes(sb, wb, wbc, false); 562d19de7edSChristoph Hellwig drop_super(sb); 563f11c9c5cSEdward Shishkin 564f11c9c5cSEdward Shishkin if (ret) 565f11c9c5cSEdward Shishkin break; 566f11c9c5cSEdward Shishkin } 56766f3b8e2SJens Axboe spin_unlock(&inode_lock); 56866f3b8e2SJens Axboe /* Leave any unwritten inodes on b_io */ 56966f3b8e2SJens Axboe } 57066f3b8e2SJens Axboe 571edadfb10SChristoph Hellwig static void __writeback_inodes_sb(struct super_block *sb, 572edadfb10SChristoph Hellwig struct bdi_writeback *wb, struct writeback_control *wbc) 573edadfb10SChristoph Hellwig { 574edadfb10SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 575edadfb10SChristoph Hellwig 576edadfb10SChristoph Hellwig wbc->wb_start = jiffies; /* livelock avoidance */ 577edadfb10SChristoph Hellwig spin_lock(&inode_lock); 578edadfb10SChristoph Hellwig if (!wbc->for_kupdate || list_empty(&wb->b_io)) 579edadfb10SChristoph Hellwig queue_io(wb, wbc->older_than_this); 580edadfb10SChristoph Hellwig writeback_sb_inodes(sb, wb, wbc, true); 581edadfb10SChristoph Hellwig spin_unlock(&inode_lock); 582edadfb10SChristoph Hellwig } 583edadfb10SChristoph Hellwig 58403ba3782SJens Axboe /* 58503ba3782SJens Axboe * The maximum number of pages to writeout in a single bdi flush/kupdate 58603ba3782SJens Axboe * operation. We do this so we don't hold I_SYNC against an inode for 58703ba3782SJens Axboe * enormous amounts of time, which would block a userspace task which has 58803ba3782SJens Axboe * been forced to throttle against that inode. Also, the code reevaluates 58903ba3782SJens Axboe * the dirty each time it has written this many pages. 59003ba3782SJens Axboe */ 59103ba3782SJens Axboe #define MAX_WRITEBACK_PAGES 1024 59203ba3782SJens Axboe 59303ba3782SJens Axboe static inline bool over_bground_thresh(void) 59403ba3782SJens Axboe { 59503ba3782SJens Axboe unsigned long background_thresh, dirty_thresh; 59603ba3782SJens Axboe 59703ba3782SJens Axboe get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL); 59803ba3782SJens Axboe 59903ba3782SJens Axboe return (global_page_state(NR_FILE_DIRTY) + 60003ba3782SJens Axboe global_page_state(NR_UNSTABLE_NFS) >= background_thresh); 60103ba3782SJens Axboe } 60203ba3782SJens Axboe 60303ba3782SJens Axboe /* 60403ba3782SJens Axboe * Explicit flushing or periodic writeback of "old" data. 60503ba3782SJens Axboe * 60603ba3782SJens Axboe * Define "old": the first time one of an inode's pages is dirtied, we mark the 60703ba3782SJens Axboe * dirtying-time in the inode's address_space. So this periodic writeback code 60803ba3782SJens Axboe * just walks the superblock inode list, writing back any inodes which are 60903ba3782SJens Axboe * older than a specific point in time. 61003ba3782SJens Axboe * 61103ba3782SJens Axboe * Try to run once per dirty_writeback_interval. But if a writeback event 61203ba3782SJens Axboe * takes longer than a dirty_writeback_interval interval, then leave a 61303ba3782SJens Axboe * one-second gap. 61403ba3782SJens Axboe * 61503ba3782SJens Axboe * older_than_this takes precedence over nr_to_write. So we'll only write back 61603ba3782SJens Axboe * all dirty pages if they are all attached to "old" mappings. 61703ba3782SJens Axboe */ 618c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb, 61983ba7b07SChristoph Hellwig struct wb_writeback_work *work) 62003ba3782SJens Axboe { 62103ba3782SJens Axboe struct writeback_control wbc = { 62283ba7b07SChristoph Hellwig .sync_mode = work->sync_mode, 62303ba3782SJens Axboe .older_than_this = NULL, 62483ba7b07SChristoph Hellwig .for_kupdate = work->for_kupdate, 62583ba7b07SChristoph Hellwig .for_background = work->for_background, 62683ba7b07SChristoph Hellwig .range_cyclic = work->range_cyclic, 62703ba3782SJens Axboe }; 62803ba3782SJens Axboe unsigned long oldest_jif; 62903ba3782SJens Axboe long wrote = 0; 630a5989bdcSJan Kara struct inode *inode; 63103ba3782SJens Axboe 63203ba3782SJens Axboe if (wbc.for_kupdate) { 63303ba3782SJens Axboe wbc.older_than_this = &oldest_jif; 63403ba3782SJens Axboe oldest_jif = jiffies - 63503ba3782SJens Axboe msecs_to_jiffies(dirty_expire_interval * 10); 63603ba3782SJens Axboe } 637c4a77a6cSJens Axboe if (!wbc.range_cyclic) { 638c4a77a6cSJens Axboe wbc.range_start = 0; 639c4a77a6cSJens Axboe wbc.range_end = LLONG_MAX; 640c4a77a6cSJens Axboe } 64103ba3782SJens Axboe 64203ba3782SJens Axboe for (;;) { 64303ba3782SJens Axboe /* 644d3ddec76SWu Fengguang * Stop writeback when nr_pages has been consumed 64503ba3782SJens Axboe */ 64683ba7b07SChristoph Hellwig if (work->nr_pages <= 0) 64703ba3782SJens Axboe break; 64803ba3782SJens Axboe 64903ba3782SJens Axboe /* 650d3ddec76SWu Fengguang * For background writeout, stop when we are below the 651d3ddec76SWu Fengguang * background dirty threshold 65203ba3782SJens Axboe */ 65383ba7b07SChristoph Hellwig if (work->for_background && !over_bground_thresh()) 65403ba3782SJens Axboe break; 65503ba3782SJens Axboe 65603ba3782SJens Axboe wbc.more_io = 0; 65703ba3782SJens Axboe wbc.nr_to_write = MAX_WRITEBACK_PAGES; 65803ba3782SJens Axboe wbc.pages_skipped = 0; 65983ba7b07SChristoph Hellwig if (work->sb) 66083ba7b07SChristoph Hellwig __writeback_inodes_sb(work->sb, wb, &wbc); 661edadfb10SChristoph Hellwig else 66203ba3782SJens Axboe writeback_inodes_wb(wb, &wbc); 66383ba7b07SChristoph Hellwig work->nr_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write; 66403ba3782SJens Axboe wrote += MAX_WRITEBACK_PAGES - wbc.nr_to_write; 66503ba3782SJens Axboe 66603ba3782SJens Axboe /* 66771fd05a8SJens Axboe * If we consumed everything, see if we have more 66803ba3782SJens Axboe */ 66971fd05a8SJens Axboe if (wbc.nr_to_write <= 0) 67071fd05a8SJens Axboe continue; 67171fd05a8SJens Axboe /* 67271fd05a8SJens Axboe * Didn't write everything and we don't have more IO, bail 67371fd05a8SJens Axboe */ 67471fd05a8SJens Axboe if (!wbc.more_io) 67571fd05a8SJens Axboe break; 67671fd05a8SJens Axboe /* 67771fd05a8SJens Axboe * Did we write something? Try for more 67871fd05a8SJens Axboe */ 679a5989bdcSJan Kara if (wbc.nr_to_write < MAX_WRITEBACK_PAGES) 68003ba3782SJens Axboe continue; 681a5989bdcSJan Kara /* 682a5989bdcSJan Kara * Nothing written. Wait for some inode to 683a5989bdcSJan Kara * become available for writeback. Otherwise 684a5989bdcSJan Kara * we'll just busyloop. 685a5989bdcSJan Kara */ 686a5989bdcSJan Kara spin_lock(&inode_lock); 687a5989bdcSJan Kara if (!list_empty(&wb->b_more_io)) { 68871fd05a8SJens Axboe inode = list_entry(wb->b_more_io.prev, 689a5989bdcSJan Kara struct inode, i_list); 690a5989bdcSJan Kara inode_wait_for_writeback(inode); 691a5989bdcSJan Kara } 692a5989bdcSJan Kara spin_unlock(&inode_lock); 69303ba3782SJens Axboe } 69403ba3782SJens Axboe 69503ba3782SJens Axboe return wrote; 69603ba3782SJens Axboe } 69703ba3782SJens Axboe 69803ba3782SJens Axboe /* 69983ba7b07SChristoph Hellwig * Return the next wb_writeback_work struct that hasn't been processed yet. 70003ba3782SJens Axboe */ 70183ba7b07SChristoph Hellwig static struct wb_writeback_work * 70283ba7b07SChristoph Hellwig get_next_work_item(struct backing_dev_info *bdi, struct bdi_writeback *wb) 70303ba3782SJens Axboe { 70483ba7b07SChristoph Hellwig struct wb_writeback_work *work = NULL; 70503ba3782SJens Axboe 70683ba7b07SChristoph Hellwig spin_lock(&bdi->wb_lock); 70783ba7b07SChristoph Hellwig if (!list_empty(&bdi->work_list)) { 70883ba7b07SChristoph Hellwig work = list_entry(bdi->work_list.next, 70983ba7b07SChristoph Hellwig struct wb_writeback_work, list); 71083ba7b07SChristoph Hellwig list_del_init(&work->list); 71103ba3782SJens Axboe } 71283ba7b07SChristoph Hellwig spin_unlock(&bdi->wb_lock); 71383ba7b07SChristoph Hellwig return work; 71403ba3782SJens Axboe } 71503ba3782SJens Axboe 71603ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb) 71703ba3782SJens Axboe { 71803ba3782SJens Axboe unsigned long expired; 71903ba3782SJens Axboe long nr_pages; 72003ba3782SJens Axboe 72169b62d01SJens Axboe /* 72269b62d01SJens Axboe * When set to zero, disable periodic writeback 72369b62d01SJens Axboe */ 72469b62d01SJens Axboe if (!dirty_writeback_interval) 72569b62d01SJens Axboe return 0; 72669b62d01SJens Axboe 72703ba3782SJens Axboe expired = wb->last_old_flush + 72803ba3782SJens Axboe msecs_to_jiffies(dirty_writeback_interval * 10); 72903ba3782SJens Axboe if (time_before(jiffies, expired)) 73003ba3782SJens Axboe return 0; 73103ba3782SJens Axboe 73203ba3782SJens Axboe wb->last_old_flush = jiffies; 73303ba3782SJens Axboe nr_pages = global_page_state(NR_FILE_DIRTY) + 73403ba3782SJens Axboe global_page_state(NR_UNSTABLE_NFS) + 73503ba3782SJens Axboe (inodes_stat.nr_inodes - inodes_stat.nr_unused); 73603ba3782SJens Axboe 737c4a77a6cSJens Axboe if (nr_pages) { 73883ba7b07SChristoph Hellwig struct wb_writeback_work work = { 739c4a77a6cSJens Axboe .nr_pages = nr_pages, 740c4a77a6cSJens Axboe .sync_mode = WB_SYNC_NONE, 741c4a77a6cSJens Axboe .for_kupdate = 1, 742c4a77a6cSJens Axboe .range_cyclic = 1, 743c4a77a6cSJens Axboe }; 744c4a77a6cSJens Axboe 74583ba7b07SChristoph Hellwig return wb_writeback(wb, &work); 746c4a77a6cSJens Axboe } 74703ba3782SJens Axboe 74803ba3782SJens Axboe return 0; 74903ba3782SJens Axboe } 75003ba3782SJens Axboe 75103ba3782SJens Axboe /* 75203ba3782SJens Axboe * Retrieve work items and do the writeback they describe 75303ba3782SJens Axboe */ 75403ba3782SJens Axboe long wb_do_writeback(struct bdi_writeback *wb, int force_wait) 75503ba3782SJens Axboe { 75603ba3782SJens Axboe struct backing_dev_info *bdi = wb->bdi; 75783ba7b07SChristoph Hellwig struct wb_writeback_work *work; 758c4a77a6cSJens Axboe long wrote = 0; 75903ba3782SJens Axboe 76003ba3782SJens Axboe while ((work = get_next_work_item(bdi, wb)) != NULL) { 76103ba3782SJens Axboe /* 76203ba3782SJens Axboe * Override sync mode, in case we must wait for completion 76383ba7b07SChristoph Hellwig * because this thread is exiting now. 76403ba3782SJens Axboe */ 76503ba3782SJens Axboe if (force_wait) 76683ba7b07SChristoph Hellwig work->sync_mode = WB_SYNC_ALL; 76783ba7b07SChristoph Hellwig 768455b2864SDave Chinner trace_writeback_exec(bdi, work); 769455b2864SDave Chinner 77083ba7b07SChristoph Hellwig wrote += wb_writeback(wb, work); 77103ba3782SJens Axboe 77203ba3782SJens Axboe /* 77383ba7b07SChristoph Hellwig * Notify the caller of completion if this is a synchronous 77483ba7b07SChristoph Hellwig * work item, otherwise just free it. 77503ba3782SJens Axboe */ 77683ba7b07SChristoph Hellwig if (work->done) 77783ba7b07SChristoph Hellwig complete(work->done); 77883ba7b07SChristoph Hellwig else 77983ba7b07SChristoph Hellwig kfree(work); 78003ba3782SJens Axboe } 78103ba3782SJens Axboe 78203ba3782SJens Axboe /* 78303ba3782SJens Axboe * Check for periodic writeback, kupdated() style 78403ba3782SJens Axboe */ 78503ba3782SJens Axboe wrote += wb_check_old_data_flush(wb); 78603ba3782SJens Axboe 78703ba3782SJens Axboe return wrote; 78803ba3782SJens Axboe } 78903ba3782SJens Axboe 79003ba3782SJens Axboe /* 79103ba3782SJens Axboe * Handle writeback of dirty data for the device backed by this bdi. Also 79203ba3782SJens Axboe * wakes up periodically and does kupdated style flushing. 79303ba3782SJens Axboe */ 79408243900SChristoph Hellwig int bdi_writeback_thread(void *data) 79503ba3782SJens Axboe { 79608243900SChristoph Hellwig struct bdi_writeback *wb = data; 79708243900SChristoph Hellwig struct backing_dev_info *bdi = wb->bdi; 79803ba3782SJens Axboe unsigned long last_active = jiffies; 79903ba3782SJens Axboe unsigned long wait_jiffies = -1UL; 80003ba3782SJens Axboe long pages_written; 80103ba3782SJens Axboe 80208243900SChristoph Hellwig /* 80308243900SChristoph Hellwig * Add us to the active bdi_list 80408243900SChristoph Hellwig */ 80508243900SChristoph Hellwig spin_lock_bh(&bdi_lock); 80608243900SChristoph Hellwig list_add_rcu(&bdi->bdi_list, &bdi_list); 80708243900SChristoph Hellwig spin_unlock_bh(&bdi_lock); 80808243900SChristoph Hellwig 80908243900SChristoph Hellwig current->flags |= PF_FLUSHER | PF_SWAPWRITE; 81008243900SChristoph Hellwig set_freezable(); 81108243900SChristoph Hellwig 81208243900SChristoph Hellwig /* 81308243900SChristoph Hellwig * Our parent may run at a different priority, just set us to normal 81408243900SChristoph Hellwig */ 81508243900SChristoph Hellwig set_user_nice(current, 0); 81608243900SChristoph Hellwig 81708243900SChristoph Hellwig /* 81808243900SChristoph Hellwig * Clear pending bit and wakeup anybody waiting to tear us down 81908243900SChristoph Hellwig */ 82008243900SChristoph Hellwig clear_bit(BDI_pending, &bdi->state); 82108243900SChristoph Hellwig smp_mb__after_clear_bit(); 82208243900SChristoph Hellwig wake_up_bit(&bdi->state, BDI_pending); 82308243900SChristoph Hellwig 824455b2864SDave Chinner trace_writeback_thread_start(bdi); 825455b2864SDave Chinner 82603ba3782SJens Axboe while (!kthread_should_stop()) { 82703ba3782SJens Axboe pages_written = wb_do_writeback(wb, 0); 82803ba3782SJens Axboe 829455b2864SDave Chinner trace_writeback_pages_written(pages_written); 830455b2864SDave Chinner 83103ba3782SJens Axboe if (pages_written) 83203ba3782SJens Axboe last_active = jiffies; 83303ba3782SJens Axboe else if (wait_jiffies != -1UL) { 83403ba3782SJens Axboe unsigned long max_idle; 83503ba3782SJens Axboe 83603ba3782SJens Axboe /* 83703ba3782SJens Axboe * Longest period of inactivity that we tolerate. If we 83803ba3782SJens Axboe * see dirty data again later, the task will get 83903ba3782SJens Axboe * recreated automatically. 84003ba3782SJens Axboe */ 84103ba3782SJens Axboe max_idle = max(5UL * 60 * HZ, wait_jiffies); 84203ba3782SJens Axboe if (time_after(jiffies, max_idle + last_active)) 84303ba3782SJens Axboe break; 84403ba3782SJens Axboe } 84503ba3782SJens Axboe 84669b62d01SJens Axboe if (dirty_writeback_interval) { 84703ba3782SJens Axboe wait_jiffies = msecs_to_jiffies(dirty_writeback_interval * 10); 84849db0414SJens Axboe schedule_timeout_interruptible(wait_jiffies); 849f9eadbbdSJens Axboe } else { 850f9eadbbdSJens Axboe set_current_state(TASK_INTERRUPTIBLE); 851f9eadbbdSJens Axboe if (list_empty_careful(&wb->bdi->work_list) && 852f9eadbbdSJens Axboe !kthread_should_stop()) 85369b62d01SJens Axboe schedule(); 854f9eadbbdSJens Axboe __set_current_state(TASK_RUNNING); 855f9eadbbdSJens Axboe } 85669b62d01SJens Axboe 85703ba3782SJens Axboe try_to_freeze(); 85803ba3782SJens Axboe } 85903ba3782SJens Axboe 86008243900SChristoph Hellwig wb->task = NULL; 86108243900SChristoph Hellwig 86208243900SChristoph Hellwig /* 86308243900SChristoph Hellwig * Flush any work that raced with us exiting. No new work 86408243900SChristoph Hellwig * will be added, since this bdi isn't discoverable anymore. 86508243900SChristoph Hellwig */ 86608243900SChristoph Hellwig if (!list_empty(&bdi->work_list)) 86708243900SChristoph Hellwig wb_do_writeback(wb, 1); 868455b2864SDave Chinner 869455b2864SDave Chinner trace_writeback_thread_stop(bdi); 87003ba3782SJens Axboe return 0; 87103ba3782SJens Axboe } 87203ba3782SJens Axboe 87308243900SChristoph Hellwig 87403ba3782SJens Axboe /* 87503ba3782SJens Axboe * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back 87603ba3782SJens Axboe * the whole world. 87703ba3782SJens Axboe */ 87803ba3782SJens Axboe void wakeup_flusher_threads(long nr_pages) 87903ba3782SJens Axboe { 880b8c2f347SChristoph Hellwig struct backing_dev_info *bdi; 881b8c2f347SChristoph Hellwig 88283ba7b07SChristoph Hellwig if (!nr_pages) { 88383ba7b07SChristoph Hellwig nr_pages = global_page_state(NR_FILE_DIRTY) + 88403ba3782SJens Axboe global_page_state(NR_UNSTABLE_NFS); 885b8c2f347SChristoph Hellwig } 886b8c2f347SChristoph Hellwig 887b8c2f347SChristoph Hellwig rcu_read_lock(); 888b8c2f347SChristoph Hellwig list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 889b8c2f347SChristoph Hellwig if (!bdi_has_dirty_io(bdi)) 890b8c2f347SChristoph Hellwig continue; 89183ba7b07SChristoph Hellwig __bdi_start_writeback(bdi, nr_pages, false, false); 892b8c2f347SChristoph Hellwig } 893b8c2f347SChristoph Hellwig rcu_read_unlock(); 89403ba3782SJens Axboe } 89503ba3782SJens Axboe 89603ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode) 89703ba3782SJens Axboe { 89803ba3782SJens Axboe if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { 89903ba3782SJens Axboe struct dentry *dentry; 90003ba3782SJens Axboe const char *name = "?"; 90103ba3782SJens Axboe 90203ba3782SJens Axboe dentry = d_find_alias(inode); 90303ba3782SJens Axboe if (dentry) { 90403ba3782SJens Axboe spin_lock(&dentry->d_lock); 90503ba3782SJens Axboe name = (const char *) dentry->d_name.name; 90603ba3782SJens Axboe } 90703ba3782SJens Axboe printk(KERN_DEBUG 90803ba3782SJens Axboe "%s(%d): dirtied inode %lu (%s) on %s\n", 90903ba3782SJens Axboe current->comm, task_pid_nr(current), inode->i_ino, 91003ba3782SJens Axboe name, inode->i_sb->s_id); 91103ba3782SJens Axboe if (dentry) { 91203ba3782SJens Axboe spin_unlock(&dentry->d_lock); 91303ba3782SJens Axboe dput(dentry); 91403ba3782SJens Axboe } 91503ba3782SJens Axboe } 91603ba3782SJens Axboe } 91703ba3782SJens Axboe 91803ba3782SJens Axboe /** 91903ba3782SJens Axboe * __mark_inode_dirty - internal function 92003ba3782SJens Axboe * @inode: inode to mark 92103ba3782SJens Axboe * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) 92203ba3782SJens Axboe * Mark an inode as dirty. Callers should use mark_inode_dirty or 92303ba3782SJens Axboe * mark_inode_dirty_sync. 92403ba3782SJens Axboe * 92503ba3782SJens Axboe * Put the inode on the super block's dirty list. 92603ba3782SJens Axboe * 92703ba3782SJens Axboe * CAREFUL! We mark it dirty unconditionally, but move it onto the 92803ba3782SJens Axboe * dirty list only if it is hashed or if it refers to a blockdev. 92903ba3782SJens Axboe * If it was not hashed, it will never be added to the dirty list 93003ba3782SJens Axboe * even if it is later hashed, as it will have been marked dirty already. 93103ba3782SJens Axboe * 93203ba3782SJens Axboe * In short, make sure you hash any inodes _before_ you start marking 93303ba3782SJens Axboe * them dirty. 93403ba3782SJens Axboe * 93503ba3782SJens Axboe * This function *must* be atomic for the I_DIRTY_PAGES case - 93603ba3782SJens Axboe * set_page_dirty() is called under spinlock in several places. 93703ba3782SJens Axboe * 93803ba3782SJens Axboe * Note that for blockdevs, inode->dirtied_when represents the dirtying time of 93903ba3782SJens Axboe * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of 94003ba3782SJens Axboe * the kernel-internal blockdev inode represents the dirtying time of the 94103ba3782SJens Axboe * blockdev's pages. This is why for I_DIRTY_PAGES we always use 94203ba3782SJens Axboe * page->mapping->host, so the page-dirtying time is recorded in the internal 94303ba3782SJens Axboe * blockdev inode. 94403ba3782SJens Axboe */ 94503ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags) 94603ba3782SJens Axboe { 94703ba3782SJens Axboe struct super_block *sb = inode->i_sb; 94803ba3782SJens Axboe 94903ba3782SJens Axboe /* 95003ba3782SJens Axboe * Don't do this for I_DIRTY_PAGES - that doesn't actually 95103ba3782SJens Axboe * dirty the inode itself 95203ba3782SJens Axboe */ 95303ba3782SJens Axboe if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { 95403ba3782SJens Axboe if (sb->s_op->dirty_inode) 95503ba3782SJens Axboe sb->s_op->dirty_inode(inode); 95603ba3782SJens Axboe } 95703ba3782SJens Axboe 95803ba3782SJens Axboe /* 95903ba3782SJens Axboe * make sure that changes are seen by all cpus before we test i_state 96003ba3782SJens Axboe * -- mikulas 96103ba3782SJens Axboe */ 96203ba3782SJens Axboe smp_mb(); 96303ba3782SJens Axboe 96403ba3782SJens Axboe /* avoid the locking if we can */ 96503ba3782SJens Axboe if ((inode->i_state & flags) == flags) 96603ba3782SJens Axboe return; 96703ba3782SJens Axboe 96803ba3782SJens Axboe if (unlikely(block_dump)) 96903ba3782SJens Axboe block_dump___mark_inode_dirty(inode); 97003ba3782SJens Axboe 97103ba3782SJens Axboe spin_lock(&inode_lock); 97203ba3782SJens Axboe if ((inode->i_state & flags) != flags) { 97303ba3782SJens Axboe const int was_dirty = inode->i_state & I_DIRTY; 97403ba3782SJens Axboe 97503ba3782SJens Axboe inode->i_state |= flags; 97603ba3782SJens Axboe 97703ba3782SJens Axboe /* 97803ba3782SJens Axboe * If the inode is being synced, just update its dirty state. 97903ba3782SJens Axboe * The unlocker will place the inode on the appropriate 98003ba3782SJens Axboe * superblock list, based upon its state. 98103ba3782SJens Axboe */ 98203ba3782SJens Axboe if (inode->i_state & I_SYNC) 98303ba3782SJens Axboe goto out; 98403ba3782SJens Axboe 98503ba3782SJens Axboe /* 98603ba3782SJens Axboe * Only add valid (hashed) inodes to the superblock's 98703ba3782SJens Axboe * dirty list. Add blockdev inodes as well. 98803ba3782SJens Axboe */ 98903ba3782SJens Axboe if (!S_ISBLK(inode->i_mode)) { 99003ba3782SJens Axboe if (hlist_unhashed(&inode->i_hash)) 99103ba3782SJens Axboe goto out; 99203ba3782SJens Axboe } 99303ba3782SJens Axboe if (inode->i_state & (I_FREEING|I_CLEAR)) 99403ba3782SJens Axboe goto out; 99503ba3782SJens Axboe 99603ba3782SJens Axboe /* 99703ba3782SJens Axboe * If the inode was already on b_dirty/b_io/b_more_io, don't 99803ba3782SJens Axboe * reposition it (that would break b_dirty time-ordering). 99903ba3782SJens Axboe */ 100003ba3782SJens Axboe if (!was_dirty) { 100103ba3782SJens Axboe struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 1002500b067cSJens Axboe struct backing_dev_info *bdi = wb->bdi; 1003500b067cSJens Axboe 1004500b067cSJens Axboe if (bdi_cap_writeback_dirty(bdi) && 1005500b067cSJens Axboe !test_bit(BDI_registered, &bdi->state)) { 1006500b067cSJens Axboe WARN_ON(1); 1007500b067cSJens Axboe printk(KERN_ERR "bdi-%s not registered\n", 1008500b067cSJens Axboe bdi->name); 1009500b067cSJens Axboe } 101003ba3782SJens Axboe 101103ba3782SJens Axboe inode->dirtied_when = jiffies; 101203ba3782SJens Axboe list_move(&inode->i_list, &wb->b_dirty); 101303ba3782SJens Axboe } 101403ba3782SJens Axboe } 101503ba3782SJens Axboe out: 101603ba3782SJens Axboe spin_unlock(&inode_lock); 101703ba3782SJens Axboe } 101803ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty); 101903ba3782SJens Axboe 102066f3b8e2SJens Axboe /* 102166f3b8e2SJens Axboe * Write out a superblock's list of dirty inodes. A wait will be performed 102266f3b8e2SJens Axboe * upon no inodes, all inodes or the final one, depending upon sync_mode. 102366f3b8e2SJens Axboe * 102466f3b8e2SJens Axboe * If older_than_this is non-NULL, then only write out inodes which 102566f3b8e2SJens Axboe * had their first dirtying at a time earlier than *older_than_this. 102666f3b8e2SJens Axboe * 102766f3b8e2SJens Axboe * If `bdi' is non-zero then we're being asked to writeback a specific queue. 102866f3b8e2SJens Axboe * This function assumes that the blockdev superblock's inodes are backed by 102966f3b8e2SJens Axboe * a variety of queues, so all inodes are searched. For other superblocks, 103066f3b8e2SJens Axboe * assume that all inodes are backed by the same queue. 103166f3b8e2SJens Axboe * 103266f3b8e2SJens Axboe * The inodes to be written are parked on bdi->b_io. They are moved back onto 103366f3b8e2SJens Axboe * bdi->b_dirty as they are selected for writing. This way, none can be missed 103466f3b8e2SJens Axboe * on the writer throttling path, and we get decent balancing between many 103566f3b8e2SJens Axboe * throttled threads: we don't want them all piling up on inode_sync_wait. 103666f3b8e2SJens Axboe */ 1037b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb) 103866f3b8e2SJens Axboe { 103938f21977SNick Piggin struct inode *inode, *old_inode = NULL; 104038f21977SNick Piggin 104103ba3782SJens Axboe /* 104203ba3782SJens Axboe * We need to be protected against the filesystem going from 104303ba3782SJens Axboe * r/o to r/w or vice versa. 104403ba3782SJens Axboe */ 1045b6e51316SJens Axboe WARN_ON(!rwsem_is_locked(&sb->s_umount)); 104603ba3782SJens Axboe 104766f3b8e2SJens Axboe spin_lock(&inode_lock); 104866f3b8e2SJens Axboe 104938f21977SNick Piggin /* 105038f21977SNick Piggin * Data integrity sync. Must wait for all pages under writeback, 105138f21977SNick Piggin * because there may have been pages dirtied before our sync 105238f21977SNick Piggin * call, but which had writeout started before we write it out. 105338f21977SNick Piggin * In which case, the inode may not be on the dirty list, but 105438f21977SNick Piggin * we still have to wait for that writeout. 105538f21977SNick Piggin */ 1056b6e51316SJens Axboe list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 105738f21977SNick Piggin struct address_space *mapping; 105838f21977SNick Piggin 105903ba3782SJens Axboe if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW)) 106038f21977SNick Piggin continue; 106138f21977SNick Piggin mapping = inode->i_mapping; 106238f21977SNick Piggin if (mapping->nrpages == 0) 106338f21977SNick Piggin continue; 106438f21977SNick Piggin __iget(inode); 1065ae8547b0SHans Reiser spin_unlock(&inode_lock); 106638f21977SNick Piggin /* 106738f21977SNick Piggin * We hold a reference to 'inode' so it couldn't have 106838f21977SNick Piggin * been removed from s_inodes list while we dropped the 106938f21977SNick Piggin * inode_lock. We cannot iput the inode now as we can 107038f21977SNick Piggin * be holding the last reference and we cannot iput it 107138f21977SNick Piggin * under inode_lock. So we keep the reference and iput 107238f21977SNick Piggin * it later. 107338f21977SNick Piggin */ 107438f21977SNick Piggin iput(old_inode); 107538f21977SNick Piggin old_inode = inode; 107638f21977SNick Piggin 107738f21977SNick Piggin filemap_fdatawait(mapping); 107838f21977SNick Piggin 107938f21977SNick Piggin cond_resched(); 108038f21977SNick Piggin 108138f21977SNick Piggin spin_lock(&inode_lock); 108238f21977SNick Piggin } 108338f21977SNick Piggin spin_unlock(&inode_lock); 108438f21977SNick Piggin iput(old_inode); 108566f3b8e2SJens Axboe } 10861da177e4SLinus Torvalds 1087d8a8559cSJens Axboe /** 1088d8a8559cSJens Axboe * writeback_inodes_sb - writeback dirty inodes from given super_block 1089d8a8559cSJens Axboe * @sb: the superblock 10901da177e4SLinus Torvalds * 1091d8a8559cSJens Axboe * Start writeback on some inodes on this super_block. No guarantees are made 1092d8a8559cSJens Axboe * on how many (if any) will be written, and this function does not wait 1093d8a8559cSJens Axboe * for IO completion of submitted IO. The number of pages submitted is 1094d8a8559cSJens Axboe * returned. 10951da177e4SLinus Torvalds */ 1096b6e51316SJens Axboe void writeback_inodes_sb(struct super_block *sb) 10971da177e4SLinus Torvalds { 10980e3c9a22SJens Axboe unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY); 10990e3c9a22SJens Axboe unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS); 110083ba7b07SChristoph Hellwig DECLARE_COMPLETION_ONSTACK(done); 110183ba7b07SChristoph Hellwig struct wb_writeback_work work = { 11023c4d7165SChristoph Hellwig .sb = sb, 11033c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_NONE, 110483ba7b07SChristoph Hellwig .done = &done, 11053c4d7165SChristoph Hellwig }; 11060e3c9a22SJens Axboe 1107cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 1108cf37e972SChristoph Hellwig 110983ba7b07SChristoph Hellwig work.nr_pages = nr_dirty + nr_unstable + 11100e3c9a22SJens Axboe (inodes_stat.nr_inodes - inodes_stat.nr_unused); 11110e3c9a22SJens Axboe 111283ba7b07SChristoph Hellwig bdi_queue_work(sb->s_bdi, &work); 111383ba7b07SChristoph Hellwig wait_for_completion(&done); 11141da177e4SLinus Torvalds } 1115d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb); 1116d8a8559cSJens Axboe 1117d8a8559cSJens Axboe /** 111817bd55d0SEric Sandeen * writeback_inodes_sb_if_idle - start writeback if none underway 111917bd55d0SEric Sandeen * @sb: the superblock 112017bd55d0SEric Sandeen * 112117bd55d0SEric Sandeen * Invoke writeback_inodes_sb if no writeback is currently underway. 112217bd55d0SEric Sandeen * Returns 1 if writeback was started, 0 if not. 112317bd55d0SEric Sandeen */ 112417bd55d0SEric Sandeen int writeback_inodes_sb_if_idle(struct super_block *sb) 112517bd55d0SEric Sandeen { 112617bd55d0SEric Sandeen if (!writeback_in_progress(sb->s_bdi)) { 1127cf37e972SChristoph Hellwig down_read(&sb->s_umount); 112817bd55d0SEric Sandeen writeback_inodes_sb(sb); 1129cf37e972SChristoph Hellwig up_read(&sb->s_umount); 113017bd55d0SEric Sandeen return 1; 113117bd55d0SEric Sandeen } else 113217bd55d0SEric Sandeen return 0; 113317bd55d0SEric Sandeen } 113417bd55d0SEric Sandeen EXPORT_SYMBOL(writeback_inodes_sb_if_idle); 113517bd55d0SEric Sandeen 113617bd55d0SEric Sandeen /** 1137d8a8559cSJens Axboe * sync_inodes_sb - sync sb inode pages 1138d8a8559cSJens Axboe * @sb: the superblock 1139d8a8559cSJens Axboe * 1140d8a8559cSJens Axboe * This function writes and waits on any dirty inode belonging to this 1141d8a8559cSJens Axboe * super_block. The number of pages synced is returned. 1142d8a8559cSJens Axboe */ 1143b6e51316SJens Axboe void sync_inodes_sb(struct super_block *sb) 1144d8a8559cSJens Axboe { 114583ba7b07SChristoph Hellwig DECLARE_COMPLETION_ONSTACK(done); 114683ba7b07SChristoph Hellwig struct wb_writeback_work work = { 11473c4d7165SChristoph Hellwig .sb = sb, 11483c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_ALL, 11493c4d7165SChristoph Hellwig .nr_pages = LONG_MAX, 11503c4d7165SChristoph Hellwig .range_cyclic = 0, 115183ba7b07SChristoph Hellwig .done = &done, 11523c4d7165SChristoph Hellwig }; 11533c4d7165SChristoph Hellwig 1154cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 1155cf37e972SChristoph Hellwig 115683ba7b07SChristoph Hellwig bdi_queue_work(sb->s_bdi, &work); 115783ba7b07SChristoph Hellwig wait_for_completion(&done); 115883ba7b07SChristoph Hellwig 1159b6e51316SJens Axboe wait_sb_inodes(sb); 1160d8a8559cSJens Axboe } 1161d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb); 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds /** 11641da177e4SLinus Torvalds * write_inode_now - write an inode to disk 11651da177e4SLinus Torvalds * @inode: inode to write to disk 11661da177e4SLinus Torvalds * @sync: whether the write should be synchronous or not 11671da177e4SLinus Torvalds * 11687f04c26dSAndrea Arcangeli * This function commits an inode to disk immediately if it is dirty. This is 11697f04c26dSAndrea Arcangeli * primarily needed by knfsd. 11707f04c26dSAndrea Arcangeli * 11717f04c26dSAndrea Arcangeli * The caller must either have a ref on the inode or must have set I_WILL_FREE. 11721da177e4SLinus Torvalds */ 11731da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync) 11741da177e4SLinus Torvalds { 11751da177e4SLinus Torvalds int ret; 11761da177e4SLinus Torvalds struct writeback_control wbc = { 11771da177e4SLinus Torvalds .nr_to_write = LONG_MAX, 117818914b18SMike Galbraith .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 1179111ebb6eSOGAWA Hirofumi .range_start = 0, 1180111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 11811da177e4SLinus Torvalds }; 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds if (!mapping_cap_writeback_dirty(inode->i_mapping)) 118449364ce2SAndrew Morton wbc.nr_to_write = 0; 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds might_sleep(); 11871da177e4SLinus Torvalds spin_lock(&inode_lock); 118801c03194SChristoph Hellwig ret = writeback_single_inode(inode, &wbc); 11891da177e4SLinus Torvalds spin_unlock(&inode_lock); 11901da177e4SLinus Torvalds if (sync) 11911c0eeaf5SJoern Engel inode_sync_wait(inode); 11921da177e4SLinus Torvalds return ret; 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now); 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds /** 11971da177e4SLinus Torvalds * sync_inode - write an inode and its pages to disk. 11981da177e4SLinus Torvalds * @inode: the inode to sync 11991da177e4SLinus Torvalds * @wbc: controls the writeback mode 12001da177e4SLinus Torvalds * 12011da177e4SLinus Torvalds * sync_inode() will write an inode and its pages to disk. It will also 12021da177e4SLinus Torvalds * correctly update the inode on its superblock's dirty inode lists and will 12031da177e4SLinus Torvalds * update inode->i_state. 12041da177e4SLinus Torvalds * 12051da177e4SLinus Torvalds * The caller must have a ref on the inode. 12061da177e4SLinus Torvalds */ 12071da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc) 12081da177e4SLinus Torvalds { 12091da177e4SLinus Torvalds int ret; 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds spin_lock(&inode_lock); 121201c03194SChristoph Hellwig ret = writeback_single_inode(inode, wbc); 12131da177e4SLinus Torvalds spin_unlock(&inode_lock); 12141da177e4SLinus Torvalds return ret; 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode); 1217