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> 17630d9c47SPaul Gortmaker #include <linux/export.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> 23bc31b86aSWu Fengguang #include <linux/pagemap.h> 2403ba3782SJens Axboe #include <linux/kthread.h> 2503ba3782SJens Axboe #include <linux/freezer.h> 261da177e4SLinus Torvalds #include <linux/writeback.h> 271da177e4SLinus Torvalds #include <linux/blkdev.h> 281da177e4SLinus Torvalds #include <linux/backing-dev.h> 29455b2864SDave Chinner #include <linux/tracepoint.h> 3007f3f05cSDavid Howells #include "internal.h" 311da177e4SLinus Torvalds 32d0bceac7SJens Axboe /* 33bc31b86aSWu Fengguang * 4MB minimal write chunk size 34bc31b86aSWu Fengguang */ 35bc31b86aSWu Fengguang #define MIN_WRITEBACK_PAGES (4096UL >> (PAGE_CACHE_SHIFT - 10)) 36bc31b86aSWu Fengguang 37bc31b86aSWu Fengguang /* 38c4a77a6cSJens Axboe * Passed into wb_writeback(), essentially a subset of writeback_control 39c4a77a6cSJens Axboe */ 4083ba7b07SChristoph Hellwig struct wb_writeback_work { 41c4a77a6cSJens Axboe long nr_pages; 42c4a77a6cSJens Axboe struct super_block *sb; 43d46db3d5SWu Fengguang unsigned long *older_than_this; 44c4a77a6cSJens Axboe enum writeback_sync_modes sync_mode; 456e6938b6SWu Fengguang unsigned int tagged_writepages:1; 4652957fe1SH Hartley Sweeten unsigned int for_kupdate:1; 4752957fe1SH Hartley Sweeten unsigned int range_cyclic:1; 4852957fe1SH Hartley Sweeten unsigned int for_background:1; 490e175a18SCurt Wohlgemuth enum wb_reason reason; /* why was writeback initiated? */ 50c4a77a6cSJens Axboe 518010c3b6SJens Axboe struct list_head list; /* pending work list */ 5283ba7b07SChristoph Hellwig struct completion *done; /* set if the caller waits */ 5303ba3782SJens Axboe }; 5403ba3782SJens Axboe 55f11b00f3SAdrian Bunk /** 56f11b00f3SAdrian Bunk * writeback_in_progress - determine whether there is writeback in progress 57f11b00f3SAdrian Bunk * @bdi: the device's backing_dev_info structure. 58f11b00f3SAdrian Bunk * 5903ba3782SJens Axboe * Determine whether there is writeback waiting to be handled against a 6003ba3782SJens Axboe * backing device. 61f11b00f3SAdrian Bunk */ 62f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi) 63f11b00f3SAdrian Bunk { 6481d73a32SJan Kara return test_bit(BDI_writeback_running, &bdi->state); 65f11b00f3SAdrian Bunk } 6600d4e736STheodore Ts'o EXPORT_SYMBOL(writeback_in_progress); 67f11b00f3SAdrian Bunk 68692ebd17SJan Kara static inline struct backing_dev_info *inode_to_bdi(struct inode *inode) 69692ebd17SJan Kara { 70692ebd17SJan Kara struct super_block *sb = inode->i_sb; 71692ebd17SJan Kara 72aaead25bSChristoph Hellwig if (strcmp(sb->s_type->name, "bdev") == 0) 73aaead25bSChristoph Hellwig return inode->i_mapping->backing_dev_info; 74aaead25bSChristoph Hellwig 75692ebd17SJan Kara return sb->s_bdi; 76692ebd17SJan Kara } 77692ebd17SJan Kara 787ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head) 797ccf19a8SNick Piggin { 807ccf19a8SNick Piggin return list_entry(head, struct inode, i_wb_list); 817ccf19a8SNick Piggin } 827ccf19a8SNick Piggin 8315eb77a0SWu Fengguang /* 8415eb77a0SWu Fengguang * Include the creation of the trace points after defining the 8515eb77a0SWu Fengguang * wb_writeback_work structure and inline functions so that the definition 8615eb77a0SWu Fengguang * remains local to this file. 8715eb77a0SWu Fengguang */ 8815eb77a0SWu Fengguang #define CREATE_TRACE_POINTS 8915eb77a0SWu Fengguang #include <trace/events/writeback.h> 9015eb77a0SWu Fengguang 916585027aSJan Kara /* Wakeup flusher thread or forker thread to fork it. Requires bdi->wb_lock. */ 926585027aSJan Kara static void bdi_wakeup_flusher(struct backing_dev_info *bdi) 934195f73dSNick Piggin { 94fff5b85aSArtem Bityutskiy if (bdi->wb.task) { 95fff5b85aSArtem Bityutskiy wake_up_process(bdi->wb.task); 96fff5b85aSArtem Bityutskiy } else { 971da177e4SLinus Torvalds /* 98fff5b85aSArtem Bityutskiy * The bdi thread isn't there, wake up the forker thread which 99fff5b85aSArtem Bityutskiy * will create and run it. 1001da177e4SLinus Torvalds */ 10103ba3782SJens Axboe wake_up_process(default_backing_dev_info.wb.task); 1021da177e4SLinus Torvalds } 1036585027aSJan Kara } 1046585027aSJan Kara 1056585027aSJan Kara static void bdi_queue_work(struct backing_dev_info *bdi, 1066585027aSJan Kara struct wb_writeback_work *work) 1076585027aSJan Kara { 1086585027aSJan Kara trace_writeback_queue(bdi, work); 1096585027aSJan Kara 1106585027aSJan Kara spin_lock_bh(&bdi->wb_lock); 1116585027aSJan Kara list_add_tail(&work->list, &bdi->work_list); 1126585027aSJan Kara if (!bdi->wb.task) 1136585027aSJan Kara trace_writeback_nothread(bdi, work); 1146585027aSJan Kara bdi_wakeup_flusher(bdi); 1156467716aSArtem Bityutskiy spin_unlock_bh(&bdi->wb_lock); 11603ba3782SJens Axboe } 1171da177e4SLinus Torvalds 11883ba7b07SChristoph Hellwig static void 11983ba7b07SChristoph Hellwig __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages, 1200e175a18SCurt Wohlgemuth bool range_cyclic, enum wb_reason reason) 1211da177e4SLinus Torvalds { 12283ba7b07SChristoph Hellwig struct wb_writeback_work *work; 12303ba3782SJens Axboe 124bcddc3f0SJens Axboe /* 125bcddc3f0SJens Axboe * This is WB_SYNC_NONE writeback, so if allocation fails just 126bcddc3f0SJens Axboe * wakeup the thread for old dirty data writeback 127bcddc3f0SJens Axboe */ 12883ba7b07SChristoph Hellwig work = kzalloc(sizeof(*work), GFP_ATOMIC); 12983ba7b07SChristoph Hellwig if (!work) { 130455b2864SDave Chinner if (bdi->wb.task) { 131455b2864SDave Chinner trace_writeback_nowork(bdi); 13283ba7b07SChristoph Hellwig wake_up_process(bdi->wb.task); 133455b2864SDave Chinner } 13483ba7b07SChristoph Hellwig return; 13583ba7b07SChristoph Hellwig } 13683ba7b07SChristoph Hellwig 13783ba7b07SChristoph Hellwig work->sync_mode = WB_SYNC_NONE; 13883ba7b07SChristoph Hellwig work->nr_pages = nr_pages; 13983ba7b07SChristoph Hellwig work->range_cyclic = range_cyclic; 1400e175a18SCurt Wohlgemuth work->reason = reason; 14183ba7b07SChristoph Hellwig 142f11fcae8SJens Axboe bdi_queue_work(bdi, work); 14303ba3782SJens Axboe } 144b6e51316SJens Axboe 145b6e51316SJens Axboe /** 146b6e51316SJens Axboe * bdi_start_writeback - start writeback 147b6e51316SJens Axboe * @bdi: the backing device to write from 148b6e51316SJens Axboe * @nr_pages: the number of pages to write 149786228abSMarcos Paulo de Souza * @reason: reason why some writeback work was initiated 150b6e51316SJens Axboe * 151b6e51316SJens Axboe * Description: 152b6e51316SJens Axboe * This does WB_SYNC_NONE opportunistic writeback. The IO is only 15325985edcSLucas De Marchi * started when this function returns, we make no guarantees on 1540e3c9a22SJens Axboe * completion. Caller need not hold sb s_umount semaphore. 155b6e51316SJens Axboe * 156b6e51316SJens Axboe */ 1570e175a18SCurt Wohlgemuth void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages, 1580e175a18SCurt Wohlgemuth enum wb_reason reason) 159b6e51316SJens Axboe { 1600e175a18SCurt Wohlgemuth __bdi_start_writeback(bdi, nr_pages, true, reason); 161d3ddec76SWu Fengguang } 162d3ddec76SWu Fengguang 163c5444198SChristoph Hellwig /** 164c5444198SChristoph Hellwig * bdi_start_background_writeback - start background writeback 165c5444198SChristoph Hellwig * @bdi: the backing device to write from 166c5444198SChristoph Hellwig * 167c5444198SChristoph Hellwig * Description: 1686585027aSJan Kara * This makes sure WB_SYNC_NONE background writeback happens. When 1696585027aSJan Kara * this function returns, it is only guaranteed that for given BDI 1706585027aSJan Kara * some IO is happening if we are over background dirty threshold. 1716585027aSJan Kara * Caller need not hold sb s_umount semaphore. 172c5444198SChristoph Hellwig */ 173c5444198SChristoph Hellwig void bdi_start_background_writeback(struct backing_dev_info *bdi) 174c5444198SChristoph Hellwig { 1756585027aSJan Kara /* 1766585027aSJan Kara * We just wake up the flusher thread. It will perform background 1776585027aSJan Kara * writeback as soon as there is no other work to do. 1786585027aSJan Kara */ 17971927e84SWu Fengguang trace_writeback_wake_background(bdi); 1806585027aSJan Kara spin_lock_bh(&bdi->wb_lock); 1816585027aSJan Kara bdi_wakeup_flusher(bdi); 1826585027aSJan Kara spin_unlock_bh(&bdi->wb_lock); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds /* 186a66979abSDave Chinner * Remove the inode from the writeback list it is on. 187a66979abSDave Chinner */ 188a66979abSDave Chinner void inode_wb_list_del(struct inode *inode) 189a66979abSDave Chinner { 190f758eeabSChristoph Hellwig struct backing_dev_info *bdi = inode_to_bdi(inode); 191a66979abSDave Chinner 192f758eeabSChristoph Hellwig spin_lock(&bdi->wb.list_lock); 193f758eeabSChristoph Hellwig list_del_init(&inode->i_wb_list); 194f758eeabSChristoph Hellwig spin_unlock(&bdi->wb.list_lock); 195f758eeabSChristoph Hellwig } 196a66979abSDave Chinner 197a66979abSDave Chinner /* 1986610a0bcSAndrew Morton * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 1996610a0bcSAndrew Morton * furthest end of its superblock's dirty-inode list. 2006610a0bcSAndrew Morton * 2016610a0bcSAndrew Morton * Before stamping the inode's ->dirtied_when, we check to see whether it is 20266f3b8e2SJens Axboe * already the most-recently-dirtied inode on the b_dirty list. If that is 2036610a0bcSAndrew Morton * the case then the inode must have been redirtied while it was being written 2046610a0bcSAndrew Morton * out and we don't reset its dirtied_when. 2056610a0bcSAndrew Morton */ 206f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb) 2076610a0bcSAndrew Morton { 208f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 20903ba3782SJens Axboe if (!list_empty(&wb->b_dirty)) { 21066f3b8e2SJens Axboe struct inode *tail; 2116610a0bcSAndrew Morton 2127ccf19a8SNick Piggin tail = wb_inode(wb->b_dirty.next); 21366f3b8e2SJens Axboe if (time_before(inode->dirtied_when, tail->dirtied_when)) 2146610a0bcSAndrew Morton inode->dirtied_when = jiffies; 2156610a0bcSAndrew Morton } 2167ccf19a8SNick Piggin list_move(&inode->i_wb_list, &wb->b_dirty); 2176610a0bcSAndrew Morton } 2186610a0bcSAndrew Morton 2196610a0bcSAndrew Morton /* 22066f3b8e2SJens Axboe * requeue inode for re-scanning after bdi->b_io list is exhausted. 221c986d1e2SAndrew Morton */ 222f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb) 223c986d1e2SAndrew Morton { 224f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 2257ccf19a8SNick Piggin list_move(&inode->i_wb_list, &wb->b_more_io); 226c986d1e2SAndrew Morton } 227c986d1e2SAndrew Morton 2281c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode) 2291c0eeaf5SJoern Engel { 230365b94aeSJan Kara inode->i_state &= ~I_SYNC; 2314eff96ddSJan Kara /* If inode is clean an unused, put it into LRU now... */ 2324eff96ddSJan Kara inode_add_lru(inode); 233365b94aeSJan Kara /* Waiters must see I_SYNC cleared before being woken up */ 2341c0eeaf5SJoern Engel smp_mb(); 2351c0eeaf5SJoern Engel wake_up_bit(&inode->i_state, __I_SYNC); 2361c0eeaf5SJoern Engel } 2371c0eeaf5SJoern Engel 238d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t) 239d2caa3c5SJeff Layton { 240d2caa3c5SJeff Layton bool ret = time_after(inode->dirtied_when, t); 241d2caa3c5SJeff Layton #ifndef CONFIG_64BIT 242d2caa3c5SJeff Layton /* 243d2caa3c5SJeff Layton * For inodes being constantly redirtied, dirtied_when can get stuck. 244d2caa3c5SJeff Layton * It _appears_ to be in the future, but is actually in distant past. 245d2caa3c5SJeff Layton * This test is necessary to prevent such wrapped-around relative times 2465b0830cbSJens Axboe * from permanently stopping the whole bdi writeback. 247d2caa3c5SJeff Layton */ 248d2caa3c5SJeff Layton ret = ret && time_before_eq(inode->dirtied_when, jiffies); 249d2caa3c5SJeff Layton #endif 250d2caa3c5SJeff Layton return ret; 251d2caa3c5SJeff Layton } 252d2caa3c5SJeff Layton 253c986d1e2SAndrew Morton /* 2540e2f2b23SWang Sheng-Hui * Move expired (dirtied before work->older_than_this) dirty inodes from 255697e6fedSJan Kara * @delaying_queue to @dispatch_queue. 2562c136579SFengguang Wu */ 257e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue, 2582c136579SFengguang Wu struct list_head *dispatch_queue, 259ad4e38ddSCurt Wohlgemuth struct wb_writeback_work *work) 2602c136579SFengguang Wu { 2615c03449dSShaohua Li LIST_HEAD(tmp); 2625c03449dSShaohua Li struct list_head *pos, *node; 263cf137307SJens Axboe struct super_block *sb = NULL; 2645c03449dSShaohua Li struct inode *inode; 265cf137307SJens Axboe int do_sb_sort = 0; 266e84d0a4fSWu Fengguang int moved = 0; 2675c03449dSShaohua Li 2682c136579SFengguang Wu while (!list_empty(delaying_queue)) { 2697ccf19a8SNick Piggin inode = wb_inode(delaying_queue->prev); 270ad4e38ddSCurt Wohlgemuth if (work->older_than_this && 271ad4e38ddSCurt Wohlgemuth inode_dirtied_after(inode, *work->older_than_this)) 2722c136579SFengguang Wu break; 273cf137307SJens Axboe if (sb && sb != inode->i_sb) 274cf137307SJens Axboe do_sb_sort = 1; 275cf137307SJens Axboe sb = inode->i_sb; 2767ccf19a8SNick Piggin list_move(&inode->i_wb_list, &tmp); 277e84d0a4fSWu Fengguang moved++; 2785c03449dSShaohua Li } 2795c03449dSShaohua Li 280cf137307SJens Axboe /* just one sb in list, splice to dispatch_queue and we're done */ 281cf137307SJens Axboe if (!do_sb_sort) { 282cf137307SJens Axboe list_splice(&tmp, dispatch_queue); 283e84d0a4fSWu Fengguang goto out; 284cf137307SJens Axboe } 285cf137307SJens Axboe 2865c03449dSShaohua Li /* Move inodes from one superblock together */ 2875c03449dSShaohua Li while (!list_empty(&tmp)) { 2887ccf19a8SNick Piggin sb = wb_inode(tmp.prev)->i_sb; 2895c03449dSShaohua Li list_for_each_prev_safe(pos, node, &tmp) { 2907ccf19a8SNick Piggin inode = wb_inode(pos); 2915c03449dSShaohua Li if (inode->i_sb == sb) 2927ccf19a8SNick Piggin list_move(&inode->i_wb_list, dispatch_queue); 2932c136579SFengguang Wu } 2942c136579SFengguang Wu } 295e84d0a4fSWu Fengguang out: 296e84d0a4fSWu Fengguang return moved; 2975c03449dSShaohua Li } 2982c136579SFengguang Wu 2992c136579SFengguang Wu /* 3002c136579SFengguang Wu * Queue all expired dirty inodes for io, eldest first. 3014ea879b9SWu Fengguang * Before 3024ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 3034ea879b9SWu Fengguang * =============> gf edc BA 3044ea879b9SWu Fengguang * After 3054ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 3064ea879b9SWu Fengguang * =============> g fBAedc 3074ea879b9SWu Fengguang * | 3084ea879b9SWu Fengguang * +--> dequeue for IO 3092c136579SFengguang Wu */ 310ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work) 3112c136579SFengguang Wu { 312e84d0a4fSWu Fengguang int moved; 313f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 3144ea879b9SWu Fengguang list_splice_init(&wb->b_more_io, &wb->b_io); 315ad4e38ddSCurt Wohlgemuth moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, work); 316ad4e38ddSCurt Wohlgemuth trace_writeback_queue_io(wb, work, moved); 31766f3b8e2SJens Axboe } 31866f3b8e2SJens Axboe 319a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc) 32066f3b8e2SJens Axboe { 32103ba3782SJens Axboe if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) 322a9185b41SChristoph Hellwig return inode->i_sb->s_op->write_inode(inode, wbc); 32303ba3782SJens Axboe return 0; 32466f3b8e2SJens Axboe } 32508d8e974SFengguang Wu 3262c136579SFengguang Wu /* 327169ebd90SJan Kara * Wait for writeback on an inode to complete. Called with i_lock held. 328169ebd90SJan Kara * Caller must make sure inode cannot go away when we drop i_lock. 32901c03194SChristoph Hellwig */ 330169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode) 331169ebd90SJan Kara __releases(inode->i_lock) 332169ebd90SJan Kara __acquires(inode->i_lock) 33301c03194SChristoph Hellwig { 33401c03194SChristoph Hellwig DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); 33501c03194SChristoph Hellwig wait_queue_head_t *wqh; 33601c03194SChristoph Hellwig 33701c03194SChristoph Hellwig wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 33858a9d3d8SRichard Kennedy while (inode->i_state & I_SYNC) { 339250df6edSDave Chinner spin_unlock(&inode->i_lock); 34001c03194SChristoph Hellwig __wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE); 341250df6edSDave Chinner spin_lock(&inode->i_lock); 34258a9d3d8SRichard Kennedy } 34301c03194SChristoph Hellwig } 34401c03194SChristoph Hellwig 34501c03194SChristoph Hellwig /* 346169ebd90SJan Kara * Wait for writeback on an inode to complete. Caller must have inode pinned. 347169ebd90SJan Kara */ 348169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode) 349169ebd90SJan Kara { 350169ebd90SJan Kara spin_lock(&inode->i_lock); 351169ebd90SJan Kara __inode_wait_for_writeback(inode); 352169ebd90SJan Kara spin_unlock(&inode->i_lock); 353169ebd90SJan Kara } 354169ebd90SJan Kara 355169ebd90SJan Kara /* 356169ebd90SJan Kara * Sleep until I_SYNC is cleared. This function must be called with i_lock 357169ebd90SJan Kara * held and drops it. It is aimed for callers not holding any inode reference 358169ebd90SJan Kara * so once i_lock is dropped, inode can go away. 359169ebd90SJan Kara */ 360169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode) 361169ebd90SJan Kara __releases(inode->i_lock) 362169ebd90SJan Kara { 363169ebd90SJan Kara DEFINE_WAIT(wait); 364169ebd90SJan Kara wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 365169ebd90SJan Kara int sleep; 366169ebd90SJan Kara 367169ebd90SJan Kara prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); 368169ebd90SJan Kara sleep = inode->i_state & I_SYNC; 369169ebd90SJan Kara spin_unlock(&inode->i_lock); 370169ebd90SJan Kara if (sleep) 371169ebd90SJan Kara schedule(); 372169ebd90SJan Kara finish_wait(wqh, &wait); 373169ebd90SJan Kara } 374169ebd90SJan Kara 375169ebd90SJan Kara /* 376ccb26b5aSJan Kara * Find proper writeback list for the inode depending on its current state and 377ccb26b5aSJan Kara * possibly also change of its state while we were doing writeback. Here we 378ccb26b5aSJan Kara * handle things such as livelock prevention or fairness of writeback among 379ccb26b5aSJan Kara * inodes. This function can be called only by flusher thread - noone else 380ccb26b5aSJan Kara * processes all inodes in writeback lists and requeueing inodes behind flusher 381ccb26b5aSJan Kara * thread's back can have unexpected consequences. 382ccb26b5aSJan Kara */ 383ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb, 384ccb26b5aSJan Kara struct writeback_control *wbc) 385ccb26b5aSJan Kara { 386ccb26b5aSJan Kara if (inode->i_state & I_FREEING) 387ccb26b5aSJan Kara return; 388ccb26b5aSJan Kara 389ccb26b5aSJan Kara /* 390ccb26b5aSJan Kara * Sync livelock prevention. Each inode is tagged and synced in one 391ccb26b5aSJan Kara * shot. If still dirty, it will be redirty_tail()'ed below. Update 392ccb26b5aSJan Kara * the dirty time to prevent enqueue and sync it again. 393ccb26b5aSJan Kara */ 394ccb26b5aSJan Kara if ((inode->i_state & I_DIRTY) && 395ccb26b5aSJan Kara (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)) 396ccb26b5aSJan Kara inode->dirtied_when = jiffies; 397ccb26b5aSJan Kara 3984f8ad655SJan Kara if (wbc->pages_skipped) { 3994f8ad655SJan Kara /* 4004f8ad655SJan Kara * writeback is not making progress due to locked 4014f8ad655SJan Kara * buffers. Skip this inode for now. 4024f8ad655SJan Kara */ 4034f8ad655SJan Kara redirty_tail(inode, wb); 4044f8ad655SJan Kara return; 4054f8ad655SJan Kara } 4064f8ad655SJan Kara 407ccb26b5aSJan Kara if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) { 408ccb26b5aSJan Kara /* 409ccb26b5aSJan Kara * We didn't write back all the pages. nfs_writepages() 410ccb26b5aSJan Kara * sometimes bales out without doing anything. 411ccb26b5aSJan Kara */ 412ccb26b5aSJan Kara if (wbc->nr_to_write <= 0) { 413ccb26b5aSJan Kara /* Slice used up. Queue for next turn. */ 414ccb26b5aSJan Kara requeue_io(inode, wb); 415ccb26b5aSJan Kara } else { 416ccb26b5aSJan Kara /* 417ccb26b5aSJan Kara * Writeback blocked by something other than 418ccb26b5aSJan Kara * congestion. Delay the inode for some time to 419ccb26b5aSJan Kara * avoid spinning on the CPU (100% iowait) 420ccb26b5aSJan Kara * retrying writeback of the dirty page/inode 421ccb26b5aSJan Kara * that cannot be performed immediately. 422ccb26b5aSJan Kara */ 423ccb26b5aSJan Kara redirty_tail(inode, wb); 424ccb26b5aSJan Kara } 425ccb26b5aSJan Kara } else if (inode->i_state & I_DIRTY) { 426ccb26b5aSJan Kara /* 427ccb26b5aSJan Kara * Filesystems can dirty the inode during writeback operations, 428ccb26b5aSJan Kara * such as delayed allocation during submission or metadata 429ccb26b5aSJan Kara * updates after data IO completion. 430ccb26b5aSJan Kara */ 431ccb26b5aSJan Kara redirty_tail(inode, wb); 432ccb26b5aSJan Kara } else { 433ccb26b5aSJan Kara /* The inode is clean. Remove from writeback lists. */ 434ccb26b5aSJan Kara list_del_init(&inode->i_wb_list); 435ccb26b5aSJan Kara } 436ccb26b5aSJan Kara } 437ccb26b5aSJan Kara 438ccb26b5aSJan Kara /* 4394f8ad655SJan Kara * Write out an inode and its dirty pages. Do not update the writeback list 4404f8ad655SJan Kara * linkage. That is left to the caller. The caller is also responsible for 4414f8ad655SJan Kara * setting I_SYNC flag and calling inode_sync_complete() to clear it. 4421da177e4SLinus Torvalds */ 4431da177e4SLinus Torvalds static int 444cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) 4451da177e4SLinus Torvalds { 4461da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 447251d6a47SWu Fengguang long nr_to_write = wbc->nr_to_write; 44801c03194SChristoph Hellwig unsigned dirty; 4491da177e4SLinus Torvalds int ret; 4501da177e4SLinus Torvalds 4514f8ad655SJan Kara WARN_ON(!(inode->i_state & I_SYNC)); 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds ret = do_writepages(mapping, wbc); 4541da177e4SLinus Torvalds 45526821ed4SChristoph Hellwig /* 45626821ed4SChristoph Hellwig * Make sure to wait on the data before writing out the metadata. 45726821ed4SChristoph Hellwig * This is important for filesystems that modify metadata on data 45826821ed4SChristoph Hellwig * I/O completion. 45926821ed4SChristoph Hellwig */ 460a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) { 46126821ed4SChristoph Hellwig int err = filemap_fdatawait(mapping); 4621da177e4SLinus Torvalds if (ret == 0) 4631da177e4SLinus Torvalds ret = err; 4641da177e4SLinus Torvalds } 4651da177e4SLinus Torvalds 4665547e8aaSDmitry Monakhov /* 4675547e8aaSDmitry Monakhov * Some filesystems may redirty the inode during the writeback 4685547e8aaSDmitry Monakhov * due to delalloc, clear dirty metadata flags right before 4695547e8aaSDmitry Monakhov * write_inode() 4705547e8aaSDmitry Monakhov */ 471250df6edSDave Chinner spin_lock(&inode->i_lock); 4726290be1cSJan Kara /* Clear I_DIRTY_PAGES if we've written out all dirty pages */ 4736290be1cSJan Kara if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 4746290be1cSJan Kara inode->i_state &= ~I_DIRTY_PAGES; 4755547e8aaSDmitry Monakhov dirty = inode->i_state & I_DIRTY; 4765547e8aaSDmitry Monakhov inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC); 477250df6edSDave Chinner spin_unlock(&inode->i_lock); 47826821ed4SChristoph Hellwig /* Don't write the inode if only I_DIRTY_PAGES was set */ 47926821ed4SChristoph Hellwig if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { 480a9185b41SChristoph Hellwig int err = write_inode(inode, wbc); 4811da177e4SLinus Torvalds if (ret == 0) 4821da177e4SLinus Torvalds ret = err; 4831da177e4SLinus Torvalds } 4844f8ad655SJan Kara trace_writeback_single_inode(inode, wbc, nr_to_write); 4854f8ad655SJan Kara return ret; 4864f8ad655SJan Kara } 4874f8ad655SJan Kara 4884f8ad655SJan Kara /* 4894f8ad655SJan Kara * Write out an inode's dirty pages. Either the caller has an active reference 4904f8ad655SJan Kara * on the inode or the inode has I_WILL_FREE set. 4914f8ad655SJan Kara * 4924f8ad655SJan Kara * This function is designed to be called for writing back one inode which 4934f8ad655SJan Kara * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode() 4944f8ad655SJan Kara * and does more profound writeback list handling in writeback_sb_inodes(). 4954f8ad655SJan Kara */ 4964f8ad655SJan Kara static int 4974f8ad655SJan Kara writeback_single_inode(struct inode *inode, struct bdi_writeback *wb, 4984f8ad655SJan Kara struct writeback_control *wbc) 4994f8ad655SJan Kara { 5004f8ad655SJan Kara int ret = 0; 5014f8ad655SJan Kara 5024f8ad655SJan Kara spin_lock(&inode->i_lock); 5034f8ad655SJan Kara if (!atomic_read(&inode->i_count)) 5044f8ad655SJan Kara WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); 5054f8ad655SJan Kara else 5064f8ad655SJan Kara WARN_ON(inode->i_state & I_WILL_FREE); 5074f8ad655SJan Kara 5084f8ad655SJan Kara if (inode->i_state & I_SYNC) { 5094f8ad655SJan Kara if (wbc->sync_mode != WB_SYNC_ALL) 5104f8ad655SJan Kara goto out; 5114f8ad655SJan Kara /* 512169ebd90SJan Kara * It's a data-integrity sync. We must wait. Since callers hold 513169ebd90SJan Kara * inode reference or inode has I_WILL_FREE set, it cannot go 514169ebd90SJan Kara * away under us. 5154f8ad655SJan Kara */ 516169ebd90SJan Kara __inode_wait_for_writeback(inode); 5174f8ad655SJan Kara } 5184f8ad655SJan Kara WARN_ON(inode->i_state & I_SYNC); 5194f8ad655SJan Kara /* 5204f8ad655SJan Kara * Skip inode if it is clean. We don't want to mess with writeback 5214f8ad655SJan Kara * lists in this function since flusher thread may be doing for example 5224f8ad655SJan Kara * sync in parallel and if we move the inode, it could get skipped. So 5234f8ad655SJan Kara * here we make sure inode is on some writeback list and leave it there 5244f8ad655SJan Kara * unless we have completely cleaned the inode. 5254f8ad655SJan Kara */ 5264f8ad655SJan Kara if (!(inode->i_state & I_DIRTY)) 5274f8ad655SJan Kara goto out; 5284f8ad655SJan Kara inode->i_state |= I_SYNC; 5294f8ad655SJan Kara spin_unlock(&inode->i_lock); 5304f8ad655SJan Kara 531cd8ed2a4SYan Hong ret = __writeback_single_inode(inode, wbc); 5321da177e4SLinus Torvalds 533f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 534250df6edSDave Chinner spin_lock(&inode->i_lock); 5354f8ad655SJan Kara /* 5364f8ad655SJan Kara * If inode is clean, remove it from writeback lists. Otherwise don't 5374f8ad655SJan Kara * touch it. See comment above for explanation. 5384f8ad655SJan Kara */ 5394f8ad655SJan Kara if (!(inode->i_state & I_DIRTY)) 5404f8ad655SJan Kara list_del_init(&inode->i_wb_list); 5414f8ad655SJan Kara spin_unlock(&wb->list_lock); 5421c0eeaf5SJoern Engel inode_sync_complete(inode); 5434f8ad655SJan Kara out: 5444f8ad655SJan Kara spin_unlock(&inode->i_lock); 5451da177e4SLinus Torvalds return ret; 5461da177e4SLinus Torvalds } 5471da177e4SLinus Torvalds 5481a12d8bdSWu Fengguang static long writeback_chunk_size(struct backing_dev_info *bdi, 5491a12d8bdSWu Fengguang struct wb_writeback_work *work) 550d46db3d5SWu Fengguang { 551d46db3d5SWu Fengguang long pages; 552d46db3d5SWu Fengguang 553d46db3d5SWu Fengguang /* 554d46db3d5SWu Fengguang * WB_SYNC_ALL mode does livelock avoidance by syncing dirty 555d46db3d5SWu Fengguang * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX 556d46db3d5SWu Fengguang * here avoids calling into writeback_inodes_wb() more than once. 557d46db3d5SWu Fengguang * 558d46db3d5SWu Fengguang * The intended call sequence for WB_SYNC_ALL writeback is: 559d46db3d5SWu Fengguang * 560d46db3d5SWu Fengguang * wb_writeback() 561d46db3d5SWu Fengguang * writeback_sb_inodes() <== called only once 562d46db3d5SWu Fengguang * write_cache_pages() <== called once for each inode 563d46db3d5SWu Fengguang * (quickly) tag currently dirty pages 564d46db3d5SWu Fengguang * (maybe slowly) sync all tagged pages 565d46db3d5SWu Fengguang */ 566d46db3d5SWu Fengguang if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages) 567d46db3d5SWu Fengguang pages = LONG_MAX; 5681a12d8bdSWu Fengguang else { 5691a12d8bdSWu Fengguang pages = min(bdi->avg_write_bandwidth / 2, 5701a12d8bdSWu Fengguang global_dirty_limit / DIRTY_SCOPE); 5711a12d8bdSWu Fengguang pages = min(pages, work->nr_pages); 5721a12d8bdSWu Fengguang pages = round_down(pages + MIN_WRITEBACK_PAGES, 5731a12d8bdSWu Fengguang MIN_WRITEBACK_PAGES); 5741a12d8bdSWu Fengguang } 575d46db3d5SWu Fengguang 576d46db3d5SWu Fengguang return pages; 577d46db3d5SWu Fengguang } 578d46db3d5SWu Fengguang 57903ba3782SJens Axboe /* 580f11c9c5cSEdward Shishkin * Write a portion of b_io inodes which belong to @sb. 581edadfb10SChristoph Hellwig * 582d46db3d5SWu Fengguang * Return the number of pages and/or inodes written. 583f11c9c5cSEdward Shishkin */ 584d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb, 585d46db3d5SWu Fengguang struct bdi_writeback *wb, 586d46db3d5SWu Fengguang struct wb_writeback_work *work) 58703ba3782SJens Axboe { 588d46db3d5SWu Fengguang struct writeback_control wbc = { 589d46db3d5SWu Fengguang .sync_mode = work->sync_mode, 590d46db3d5SWu Fengguang .tagged_writepages = work->tagged_writepages, 591d46db3d5SWu Fengguang .for_kupdate = work->for_kupdate, 592d46db3d5SWu Fengguang .for_background = work->for_background, 593d46db3d5SWu Fengguang .range_cyclic = work->range_cyclic, 594d46db3d5SWu Fengguang .range_start = 0, 595d46db3d5SWu Fengguang .range_end = LLONG_MAX, 596d46db3d5SWu Fengguang }; 597d46db3d5SWu Fengguang unsigned long start_time = jiffies; 598d46db3d5SWu Fengguang long write_chunk; 599d46db3d5SWu Fengguang long wrote = 0; /* count both pages and inodes */ 600d46db3d5SWu Fengguang 60103ba3782SJens Axboe while (!list_empty(&wb->b_io)) { 6027ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 603edadfb10SChristoph Hellwig 604edadfb10SChristoph Hellwig if (inode->i_sb != sb) { 605d46db3d5SWu Fengguang if (work->sb) { 606edadfb10SChristoph Hellwig /* 607edadfb10SChristoph Hellwig * We only want to write back data for this 608edadfb10SChristoph Hellwig * superblock, move all inodes not belonging 609edadfb10SChristoph Hellwig * to it back onto the dirty list. 610edadfb10SChristoph Hellwig */ 611f758eeabSChristoph Hellwig redirty_tail(inode, wb); 61266f3b8e2SJens Axboe continue; 61366f3b8e2SJens Axboe } 614edadfb10SChristoph Hellwig 615edadfb10SChristoph Hellwig /* 616edadfb10SChristoph Hellwig * The inode belongs to a different superblock. 617edadfb10SChristoph Hellwig * Bounce back to the caller to unpin this and 618edadfb10SChristoph Hellwig * pin the next superblock. 619edadfb10SChristoph Hellwig */ 620d46db3d5SWu Fengguang break; 621edadfb10SChristoph Hellwig } 622edadfb10SChristoph Hellwig 6239843b76aSChristoph Hellwig /* 624331cbdeeSWanpeng Li * Don't bother with new inodes or inodes being freed, first 625331cbdeeSWanpeng Li * kind does not need periodic writeout yet, and for the latter 6269843b76aSChristoph Hellwig * kind writeout is handled by the freer. 6279843b76aSChristoph Hellwig */ 628250df6edSDave Chinner spin_lock(&inode->i_lock); 6299843b76aSChristoph Hellwig if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { 630250df6edSDave Chinner spin_unlock(&inode->i_lock); 631fcc5c222SWu Fengguang redirty_tail(inode, wb); 6327ef0d737SNick Piggin continue; 6337ef0d737SNick Piggin } 634cc1676d9SJan Kara if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) { 635cc1676d9SJan Kara /* 636cc1676d9SJan Kara * If this inode is locked for writeback and we are not 637cc1676d9SJan Kara * doing writeback-for-data-integrity, move it to 638cc1676d9SJan Kara * b_more_io so that writeback can proceed with the 639cc1676d9SJan Kara * other inodes on s_io. 640cc1676d9SJan Kara * 641cc1676d9SJan Kara * We'll have another go at writing back this inode 642cc1676d9SJan Kara * when we completed a full scan of b_io. 643cc1676d9SJan Kara */ 644cc1676d9SJan Kara spin_unlock(&inode->i_lock); 645cc1676d9SJan Kara requeue_io(inode, wb); 646cc1676d9SJan Kara trace_writeback_sb_inodes_requeue(inode); 647cc1676d9SJan Kara continue; 648cc1676d9SJan Kara } 649f0d07b7fSJan Kara spin_unlock(&wb->list_lock); 650f0d07b7fSJan Kara 6514f8ad655SJan Kara /* 6524f8ad655SJan Kara * We already requeued the inode if it had I_SYNC set and we 6534f8ad655SJan Kara * are doing WB_SYNC_NONE writeback. So this catches only the 6544f8ad655SJan Kara * WB_SYNC_ALL case. 6554f8ad655SJan Kara */ 656169ebd90SJan Kara if (inode->i_state & I_SYNC) { 657169ebd90SJan Kara /* Wait for I_SYNC. This function drops i_lock... */ 658169ebd90SJan Kara inode_sleep_on_writeback(inode); 659169ebd90SJan Kara /* Inode may be gone, start again */ 660ead188f9SJan Kara spin_lock(&wb->list_lock); 661169ebd90SJan Kara continue; 662169ebd90SJan Kara } 6634f8ad655SJan Kara inode->i_state |= I_SYNC; 6644f8ad655SJan Kara spin_unlock(&inode->i_lock); 665169ebd90SJan Kara 6661a12d8bdSWu Fengguang write_chunk = writeback_chunk_size(wb->bdi, work); 667d46db3d5SWu Fengguang wbc.nr_to_write = write_chunk; 668d46db3d5SWu Fengguang wbc.pages_skipped = 0; 669250df6edSDave Chinner 670169ebd90SJan Kara /* 671169ebd90SJan Kara * We use I_SYNC to pin the inode in memory. While it is set 672169ebd90SJan Kara * evict_inode() will wait so the inode cannot be freed. 673169ebd90SJan Kara */ 674cd8ed2a4SYan Hong __writeback_single_inode(inode, &wbc); 675d46db3d5SWu Fengguang 676d46db3d5SWu Fengguang work->nr_pages -= write_chunk - wbc.nr_to_write; 677d46db3d5SWu Fengguang wrote += write_chunk - wbc.nr_to_write; 6784f8ad655SJan Kara spin_lock(&wb->list_lock); 6794f8ad655SJan Kara spin_lock(&inode->i_lock); 680d46db3d5SWu Fengguang if (!(inode->i_state & I_DIRTY)) 681d46db3d5SWu Fengguang wrote++; 6824f8ad655SJan Kara requeue_inode(inode, wb, &wbc); 6834f8ad655SJan Kara inode_sync_complete(inode); 6840f1b1fd8SDave Chinner spin_unlock(&inode->i_lock); 685169ebd90SJan Kara cond_resched_lock(&wb->list_lock); 686d46db3d5SWu Fengguang /* 687d46db3d5SWu Fengguang * bail out to wb_writeback() often enough to check 688d46db3d5SWu Fengguang * background threshold and other termination conditions. 689d46db3d5SWu Fengguang */ 690d46db3d5SWu Fengguang if (wrote) { 691d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 692d46db3d5SWu Fengguang break; 693d46db3d5SWu Fengguang if (work->nr_pages <= 0) 694d46db3d5SWu Fengguang break; 6951da177e4SLinus Torvalds } 6968bc3be27SFengguang Wu } 697d46db3d5SWu Fengguang return wrote; 698f11c9c5cSEdward Shishkin } 69938f21977SNick Piggin 700d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb, 701d46db3d5SWu Fengguang struct wb_writeback_work *work) 702f11c9c5cSEdward Shishkin { 703d46db3d5SWu Fengguang unsigned long start_time = jiffies; 704d46db3d5SWu Fengguang long wrote = 0; 705f11c9c5cSEdward Shishkin 706f11c9c5cSEdward Shishkin while (!list_empty(&wb->b_io)) { 7077ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 708f11c9c5cSEdward Shishkin struct super_block *sb = inode->i_sb; 709f11c9c5cSEdward Shishkin 71012ad3ab6SDave Chinner if (!grab_super_passive(sb)) { 7110e995816SWu Fengguang /* 7120e995816SWu Fengguang * grab_super_passive() may fail consistently due to 7130e995816SWu Fengguang * s_umount being grabbed by someone else. Don't use 7140e995816SWu Fengguang * requeue_io() to avoid busy retrying the inode/sb. 7150e995816SWu Fengguang */ 7160e995816SWu Fengguang redirty_tail(inode, wb); 717d19de7edSChristoph Hellwig continue; 718334132aeSChristoph Hellwig } 719d46db3d5SWu Fengguang wrote += writeback_sb_inodes(sb, wb, work); 720d19de7edSChristoph Hellwig drop_super(sb); 721f11c9c5cSEdward Shishkin 722d46db3d5SWu Fengguang /* refer to the same tests at the end of writeback_sb_inodes */ 723d46db3d5SWu Fengguang if (wrote) { 724d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 725d46db3d5SWu Fengguang break; 726d46db3d5SWu Fengguang if (work->nr_pages <= 0) 727f11c9c5cSEdward Shishkin break; 728f11c9c5cSEdward Shishkin } 729d46db3d5SWu Fengguang } 73066f3b8e2SJens Axboe /* Leave any unwritten inodes on b_io */ 731d46db3d5SWu Fengguang return wrote; 73266f3b8e2SJens Axboe } 73366f3b8e2SJens Axboe 7340e175a18SCurt Wohlgemuth long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages, 7350e175a18SCurt Wohlgemuth enum wb_reason reason) 736edadfb10SChristoph Hellwig { 737d46db3d5SWu Fengguang struct wb_writeback_work work = { 738d46db3d5SWu Fengguang .nr_pages = nr_pages, 739d46db3d5SWu Fengguang .sync_mode = WB_SYNC_NONE, 740d46db3d5SWu Fengguang .range_cyclic = 1, 7410e175a18SCurt Wohlgemuth .reason = reason, 742d46db3d5SWu Fengguang }; 743edadfb10SChristoph Hellwig 744f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 745424b351fSWu Fengguang if (list_empty(&wb->b_io)) 746ad4e38ddSCurt Wohlgemuth queue_io(wb, &work); 747d46db3d5SWu Fengguang __writeback_inodes_wb(wb, &work); 748f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 749edadfb10SChristoph Hellwig 750d46db3d5SWu Fengguang return nr_pages - work.nr_pages; 75166f3b8e2SJens Axboe } 75266f3b8e2SJens Axboe 753b00949aaSWu Fengguang static bool over_bground_thresh(struct backing_dev_info *bdi) 75403ba3782SJens Axboe { 75503ba3782SJens Axboe unsigned long background_thresh, dirty_thresh; 75603ba3782SJens Axboe 75716c4042fSWu Fengguang global_dirty_limits(&background_thresh, &dirty_thresh); 75803ba3782SJens Axboe 759b00949aaSWu Fengguang if (global_page_state(NR_FILE_DIRTY) + 760b00949aaSWu Fengguang global_page_state(NR_UNSTABLE_NFS) > background_thresh) 761b00949aaSWu Fengguang return true; 762b00949aaSWu Fengguang 763b00949aaSWu Fengguang if (bdi_stat(bdi, BDI_RECLAIMABLE) > 764b00949aaSWu Fengguang bdi_dirty_limit(bdi, background_thresh)) 765b00949aaSWu Fengguang return true; 766b00949aaSWu Fengguang 767b00949aaSWu Fengguang return false; 76803ba3782SJens Axboe } 76903ba3782SJens Axboe 77003ba3782SJens Axboe /* 771e98be2d5SWu Fengguang * Called under wb->list_lock. If there are multiple wb per bdi, 772e98be2d5SWu Fengguang * only the flusher working on the first wb should do it. 773e98be2d5SWu Fengguang */ 774e98be2d5SWu Fengguang static void wb_update_bandwidth(struct bdi_writeback *wb, 775e98be2d5SWu Fengguang unsigned long start_time) 776e98be2d5SWu Fengguang { 777af6a3113SWu Fengguang __bdi_update_bandwidth(wb->bdi, 0, 0, 0, 0, 0, start_time); 778e98be2d5SWu Fengguang } 779e98be2d5SWu Fengguang 780e98be2d5SWu Fengguang /* 78103ba3782SJens Axboe * Explicit flushing or periodic writeback of "old" data. 78203ba3782SJens Axboe * 78303ba3782SJens Axboe * Define "old": the first time one of an inode's pages is dirtied, we mark the 78403ba3782SJens Axboe * dirtying-time in the inode's address_space. So this periodic writeback code 78503ba3782SJens Axboe * just walks the superblock inode list, writing back any inodes which are 78603ba3782SJens Axboe * older than a specific point in time. 78703ba3782SJens Axboe * 78803ba3782SJens Axboe * Try to run once per dirty_writeback_interval. But if a writeback event 78903ba3782SJens Axboe * takes longer than a dirty_writeback_interval interval, then leave a 79003ba3782SJens Axboe * one-second gap. 79103ba3782SJens Axboe * 79203ba3782SJens Axboe * older_than_this takes precedence over nr_to_write. So we'll only write back 79303ba3782SJens Axboe * all dirty pages if they are all attached to "old" mappings. 79403ba3782SJens Axboe */ 795c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb, 79683ba7b07SChristoph Hellwig struct wb_writeback_work *work) 79703ba3782SJens Axboe { 798e98be2d5SWu Fengguang unsigned long wb_start = jiffies; 799d46db3d5SWu Fengguang long nr_pages = work->nr_pages; 80003ba3782SJens Axboe unsigned long oldest_jif; 801a5989bdcSJan Kara struct inode *inode; 802d46db3d5SWu Fengguang long progress; 80303ba3782SJens Axboe 804e185dda8SWu Fengguang oldest_jif = jiffies; 805d46db3d5SWu Fengguang work->older_than_this = &oldest_jif; 80603ba3782SJens Axboe 807e8dfc305SWu Fengguang spin_lock(&wb->list_lock); 80803ba3782SJens Axboe for (;;) { 80903ba3782SJens Axboe /* 810d3ddec76SWu Fengguang * Stop writeback when nr_pages has been consumed 81103ba3782SJens Axboe */ 81283ba7b07SChristoph Hellwig if (work->nr_pages <= 0) 81303ba3782SJens Axboe break; 81403ba3782SJens Axboe 81503ba3782SJens Axboe /* 816aa373cf5SJan Kara * Background writeout and kupdate-style writeback may 817aa373cf5SJan Kara * run forever. Stop them if there is other work to do 818aa373cf5SJan Kara * so that e.g. sync can proceed. They'll be restarted 819aa373cf5SJan Kara * after the other works are all done. 820aa373cf5SJan Kara */ 821aa373cf5SJan Kara if ((work->for_background || work->for_kupdate) && 822aa373cf5SJan Kara !list_empty(&wb->bdi->work_list)) 823aa373cf5SJan Kara break; 824aa373cf5SJan Kara 825aa373cf5SJan Kara /* 826d3ddec76SWu Fengguang * For background writeout, stop when we are below the 827d3ddec76SWu Fengguang * background dirty threshold 82803ba3782SJens Axboe */ 829b00949aaSWu Fengguang if (work->for_background && !over_bground_thresh(wb->bdi)) 83003ba3782SJens Axboe break; 83103ba3782SJens Axboe 8321bc36b64SJan Kara /* 8331bc36b64SJan Kara * Kupdate and background works are special and we want to 8341bc36b64SJan Kara * include all inodes that need writing. Livelock avoidance is 8351bc36b64SJan Kara * handled by these works yielding to any other work so we are 8361bc36b64SJan Kara * safe. 8371bc36b64SJan Kara */ 838ba9aa839SWu Fengguang if (work->for_kupdate) { 839ba9aa839SWu Fengguang oldest_jif = jiffies - 840ba9aa839SWu Fengguang msecs_to_jiffies(dirty_expire_interval * 10); 8411bc36b64SJan Kara } else if (work->for_background) 8421bc36b64SJan Kara oldest_jif = jiffies; 843028c2dd1SDave Chinner 844d46db3d5SWu Fengguang trace_writeback_start(wb->bdi, work); 845e8dfc305SWu Fengguang if (list_empty(&wb->b_io)) 846ad4e38ddSCurt Wohlgemuth queue_io(wb, work); 84783ba7b07SChristoph Hellwig if (work->sb) 848d46db3d5SWu Fengguang progress = writeback_sb_inodes(work->sb, wb, work); 849edadfb10SChristoph Hellwig else 850d46db3d5SWu Fengguang progress = __writeback_inodes_wb(wb, work); 851d46db3d5SWu Fengguang trace_writeback_written(wb->bdi, work); 852028c2dd1SDave Chinner 853e98be2d5SWu Fengguang wb_update_bandwidth(wb, wb_start); 85403ba3782SJens Axboe 85503ba3782SJens Axboe /* 85671fd05a8SJens Axboe * Did we write something? Try for more 857e6fb6da2SWu Fengguang * 858e6fb6da2SWu Fengguang * Dirty inodes are moved to b_io for writeback in batches. 859e6fb6da2SWu Fengguang * The completion of the current batch does not necessarily 860e6fb6da2SWu Fengguang * mean the overall work is done. So we keep looping as long 861e6fb6da2SWu Fengguang * as made some progress on cleaning pages or inodes. 86271fd05a8SJens Axboe */ 863d46db3d5SWu Fengguang if (progress) 86403ba3782SJens Axboe continue; 865a5989bdcSJan Kara /* 866e6fb6da2SWu Fengguang * No more inodes for IO, bail 867a5989bdcSJan Kara */ 868b7a2441fSWu Fengguang if (list_empty(&wb->b_more_io)) 86903ba3782SJens Axboe break; 87003ba3782SJens Axboe /* 8718010c3b6SJens Axboe * Nothing written. Wait for some inode to 8728010c3b6SJens Axboe * become available for writeback. Otherwise 8738010c3b6SJens Axboe * we'll just busyloop. 87403ba3782SJens Axboe */ 87503ba3782SJens Axboe if (!list_empty(&wb->b_more_io)) { 876d46db3d5SWu Fengguang trace_writeback_wait(wb->bdi, work); 87703ba3782SJens Axboe inode = wb_inode(wb->b_more_io.prev); 878250df6edSDave Chinner spin_lock(&inode->i_lock); 879f0d07b7fSJan Kara spin_unlock(&wb->list_lock); 880169ebd90SJan Kara /* This function drops i_lock... */ 881169ebd90SJan Kara inode_sleep_on_writeback(inode); 882f0d07b7fSJan Kara spin_lock(&wb->list_lock); 88303ba3782SJens Axboe } 88403ba3782SJens Axboe } 885e8dfc305SWu Fengguang spin_unlock(&wb->list_lock); 88603ba3782SJens Axboe 887d46db3d5SWu Fengguang return nr_pages - work->nr_pages; 88803ba3782SJens Axboe } 88903ba3782SJens Axboe 89003ba3782SJens Axboe /* 89183ba7b07SChristoph Hellwig * Return the next wb_writeback_work struct that hasn't been processed yet. 89203ba3782SJens Axboe */ 89383ba7b07SChristoph Hellwig static struct wb_writeback_work * 89408852b6dSMinchan Kim get_next_work_item(struct backing_dev_info *bdi) 89503ba3782SJens Axboe { 89683ba7b07SChristoph Hellwig struct wb_writeback_work *work = NULL; 89703ba3782SJens Axboe 8986467716aSArtem Bityutskiy spin_lock_bh(&bdi->wb_lock); 89983ba7b07SChristoph Hellwig if (!list_empty(&bdi->work_list)) { 90083ba7b07SChristoph Hellwig work = list_entry(bdi->work_list.next, 90183ba7b07SChristoph Hellwig struct wb_writeback_work, list); 90283ba7b07SChristoph Hellwig list_del_init(&work->list); 90303ba3782SJens Axboe } 9046467716aSArtem Bityutskiy spin_unlock_bh(&bdi->wb_lock); 90583ba7b07SChristoph Hellwig return work; 90603ba3782SJens Axboe } 90703ba3782SJens Axboe 908cdf01dd5SLinus Torvalds /* 909cdf01dd5SLinus Torvalds * Add in the number of potentially dirty inodes, because each inode 910cdf01dd5SLinus Torvalds * write can dirty pagecache in the underlying blockdev. 911cdf01dd5SLinus Torvalds */ 912cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void) 913cdf01dd5SLinus Torvalds { 914cdf01dd5SLinus Torvalds return global_page_state(NR_FILE_DIRTY) + 915cdf01dd5SLinus Torvalds global_page_state(NR_UNSTABLE_NFS) + 916cdf01dd5SLinus Torvalds get_nr_dirty_inodes(); 917cdf01dd5SLinus Torvalds } 918cdf01dd5SLinus Torvalds 9196585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb) 9206585027aSJan Kara { 921b00949aaSWu Fengguang if (over_bground_thresh(wb->bdi)) { 9226585027aSJan Kara 9236585027aSJan Kara struct wb_writeback_work work = { 9246585027aSJan Kara .nr_pages = LONG_MAX, 9256585027aSJan Kara .sync_mode = WB_SYNC_NONE, 9266585027aSJan Kara .for_background = 1, 9276585027aSJan Kara .range_cyclic = 1, 9280e175a18SCurt Wohlgemuth .reason = WB_REASON_BACKGROUND, 9296585027aSJan Kara }; 9306585027aSJan Kara 9316585027aSJan Kara return wb_writeback(wb, &work); 9326585027aSJan Kara } 9336585027aSJan Kara 9346585027aSJan Kara return 0; 9356585027aSJan Kara } 9366585027aSJan Kara 93703ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb) 93803ba3782SJens Axboe { 93903ba3782SJens Axboe unsigned long expired; 94003ba3782SJens Axboe long nr_pages; 94103ba3782SJens Axboe 94269b62d01SJens Axboe /* 94369b62d01SJens Axboe * When set to zero, disable periodic writeback 94469b62d01SJens Axboe */ 94569b62d01SJens Axboe if (!dirty_writeback_interval) 94669b62d01SJens Axboe return 0; 94769b62d01SJens Axboe 94803ba3782SJens Axboe expired = wb->last_old_flush + 94903ba3782SJens Axboe msecs_to_jiffies(dirty_writeback_interval * 10); 95003ba3782SJens Axboe if (time_before(jiffies, expired)) 95103ba3782SJens Axboe return 0; 95203ba3782SJens Axboe 95303ba3782SJens Axboe wb->last_old_flush = jiffies; 954cdf01dd5SLinus Torvalds nr_pages = get_nr_dirty_pages(); 95503ba3782SJens Axboe 956c4a77a6cSJens Axboe if (nr_pages) { 95783ba7b07SChristoph Hellwig struct wb_writeback_work work = { 958c4a77a6cSJens Axboe .nr_pages = nr_pages, 959c4a77a6cSJens Axboe .sync_mode = WB_SYNC_NONE, 960c4a77a6cSJens Axboe .for_kupdate = 1, 961c4a77a6cSJens Axboe .range_cyclic = 1, 9620e175a18SCurt Wohlgemuth .reason = WB_REASON_PERIODIC, 963c4a77a6cSJens Axboe }; 964c4a77a6cSJens Axboe 96583ba7b07SChristoph Hellwig return wb_writeback(wb, &work); 966c4a77a6cSJens Axboe } 96703ba3782SJens Axboe 96803ba3782SJens Axboe return 0; 96903ba3782SJens Axboe } 97003ba3782SJens Axboe 97103ba3782SJens Axboe /* 97203ba3782SJens Axboe * Retrieve work items and do the writeback they describe 97303ba3782SJens Axboe */ 97403ba3782SJens Axboe long wb_do_writeback(struct bdi_writeback *wb, int force_wait) 97503ba3782SJens Axboe { 97603ba3782SJens Axboe struct backing_dev_info *bdi = wb->bdi; 97783ba7b07SChristoph Hellwig struct wb_writeback_work *work; 978c4a77a6cSJens Axboe long wrote = 0; 97903ba3782SJens Axboe 98081d73a32SJan Kara set_bit(BDI_writeback_running, &wb->bdi->state); 98108852b6dSMinchan Kim while ((work = get_next_work_item(bdi)) != NULL) { 98203ba3782SJens Axboe /* 98303ba3782SJens Axboe * Override sync mode, in case we must wait for completion 98483ba7b07SChristoph Hellwig * because this thread is exiting now. 98503ba3782SJens Axboe */ 98603ba3782SJens Axboe if (force_wait) 98783ba7b07SChristoph Hellwig work->sync_mode = WB_SYNC_ALL; 98883ba7b07SChristoph Hellwig 989455b2864SDave Chinner trace_writeback_exec(bdi, work); 990455b2864SDave Chinner 99183ba7b07SChristoph Hellwig wrote += wb_writeback(wb, work); 99203ba3782SJens Axboe 99303ba3782SJens Axboe /* 99483ba7b07SChristoph Hellwig * Notify the caller of completion if this is a synchronous 99583ba7b07SChristoph Hellwig * work item, otherwise just free it. 99603ba3782SJens Axboe */ 99783ba7b07SChristoph Hellwig if (work->done) 99883ba7b07SChristoph Hellwig complete(work->done); 99983ba7b07SChristoph Hellwig else 100083ba7b07SChristoph Hellwig kfree(work); 100103ba3782SJens Axboe } 100203ba3782SJens Axboe 100303ba3782SJens Axboe /* 100403ba3782SJens Axboe * Check for periodic writeback, kupdated() style 100503ba3782SJens Axboe */ 100603ba3782SJens Axboe wrote += wb_check_old_data_flush(wb); 10076585027aSJan Kara wrote += wb_check_background_flush(wb); 100881d73a32SJan Kara clear_bit(BDI_writeback_running, &wb->bdi->state); 100903ba3782SJens Axboe 101003ba3782SJens Axboe return wrote; 101103ba3782SJens Axboe } 101203ba3782SJens Axboe 101303ba3782SJens Axboe /* 101403ba3782SJens Axboe * Handle writeback of dirty data for the device backed by this bdi. Also 101503ba3782SJens Axboe * wakes up periodically and does kupdated style flushing. 101603ba3782SJens Axboe */ 101708243900SChristoph Hellwig int bdi_writeback_thread(void *data) 101803ba3782SJens Axboe { 101908243900SChristoph Hellwig struct bdi_writeback *wb = data; 102008243900SChristoph Hellwig struct backing_dev_info *bdi = wb->bdi; 102103ba3782SJens Axboe long pages_written; 102203ba3782SJens Axboe 1023766f9164SPeter Zijlstra current->flags |= PF_SWAPWRITE; 102408243900SChristoph Hellwig set_freezable(); 1025ecd58403SArtem Bityutskiy wb->last_active = jiffies; 102603ba3782SJens Axboe 102703ba3782SJens Axboe /* 102808243900SChristoph Hellwig * Our parent may run at a different priority, just set us to normal 102903ba3782SJens Axboe */ 103008243900SChristoph Hellwig set_user_nice(current, 0); 103108243900SChristoph Hellwig 1032455b2864SDave Chinner trace_writeback_thread_start(bdi); 1033455b2864SDave Chinner 10348a32c441STejun Heo while (!kthread_freezable_should_stop(NULL)) { 10356467716aSArtem Bityutskiy /* 10366467716aSArtem Bityutskiy * Remove own delayed wake-up timer, since we are already awake 10375aaea51dSYan Hong * and we'll take care of the periodic write-back. 10386467716aSArtem Bityutskiy */ 10396467716aSArtem Bityutskiy del_timer(&wb->wakeup_timer); 10406467716aSArtem Bityutskiy 104103ba3782SJens Axboe pages_written = wb_do_writeback(wb, 0); 104203ba3782SJens Axboe 1043455b2864SDave Chinner trace_writeback_pages_written(pages_written); 1044455b2864SDave Chinner 104503ba3782SJens Axboe if (pages_written) 1046ecd58403SArtem Bityutskiy wb->last_active = jiffies; 104703ba3782SJens Axboe 1048297252c8SArtem Bityutskiy set_current_state(TASK_INTERRUPTIBLE); 1049b76b4014SJ. Bruce Fields if (!list_empty(&bdi->work_list) || kthread_should_stop()) { 1050297252c8SArtem Bityutskiy __set_current_state(TASK_RUNNING); 1051297252c8SArtem Bityutskiy continue; 105203ba3782SJens Axboe } 105303ba3782SJens Axboe 1054253c34e9SArtem Bityutskiy if (wb_has_dirty_io(wb) && dirty_writeback_interval) 1055fff5b85aSArtem Bityutskiy schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10)); 1056253c34e9SArtem Bityutskiy else { 1057253c34e9SArtem Bityutskiy /* 1058253c34e9SArtem Bityutskiy * We have nothing to do, so can go sleep without any 1059253c34e9SArtem Bityutskiy * timeout and save power. When a work is queued or 1060253c34e9SArtem Bityutskiy * something is made dirty - we will be woken up. 1061253c34e9SArtem Bityutskiy */ 106269b62d01SJens Axboe schedule(); 1063f9eadbbdSJens Axboe } 106403ba3782SJens Axboe } 106503ba3782SJens Axboe 1066fff5b85aSArtem Bityutskiy /* Flush any work that raced with us exiting */ 106708243900SChristoph Hellwig if (!list_empty(&bdi->work_list)) 106808243900SChristoph Hellwig wb_do_writeback(wb, 1); 1069455b2864SDave Chinner 1070455b2864SDave Chinner trace_writeback_thread_stop(bdi); 107103ba3782SJens Axboe return 0; 107203ba3782SJens Axboe } 107303ba3782SJens Axboe 107408243900SChristoph Hellwig 107503ba3782SJens Axboe /* 107603ba3782SJens Axboe * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back 107703ba3782SJens Axboe * the whole world. 107803ba3782SJens Axboe */ 10790e175a18SCurt Wohlgemuth void wakeup_flusher_threads(long nr_pages, enum wb_reason reason) 108003ba3782SJens Axboe { 1081b8c2f347SChristoph Hellwig struct backing_dev_info *bdi; 1082b8c2f347SChristoph Hellwig 108383ba7b07SChristoph Hellwig if (!nr_pages) { 108483ba7b07SChristoph Hellwig nr_pages = global_page_state(NR_FILE_DIRTY) + 108503ba3782SJens Axboe global_page_state(NR_UNSTABLE_NFS); 1086b8c2f347SChristoph Hellwig } 1087b8c2f347SChristoph Hellwig 1088b8c2f347SChristoph Hellwig rcu_read_lock(); 1089b8c2f347SChristoph Hellwig list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 1090b8c2f347SChristoph Hellwig if (!bdi_has_dirty_io(bdi)) 1091b8c2f347SChristoph Hellwig continue; 10920e175a18SCurt Wohlgemuth __bdi_start_writeback(bdi, nr_pages, false, reason); 1093b8c2f347SChristoph Hellwig } 1094b8c2f347SChristoph Hellwig rcu_read_unlock(); 109503ba3782SJens Axboe } 109603ba3782SJens Axboe 109703ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode) 109803ba3782SJens Axboe { 109903ba3782SJens Axboe if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { 110003ba3782SJens Axboe struct dentry *dentry; 110103ba3782SJens Axboe const char *name = "?"; 110203ba3782SJens Axboe 110303ba3782SJens Axboe dentry = d_find_alias(inode); 110403ba3782SJens Axboe if (dentry) { 110503ba3782SJens Axboe spin_lock(&dentry->d_lock); 110603ba3782SJens Axboe name = (const char *) dentry->d_name.name; 110703ba3782SJens Axboe } 110803ba3782SJens Axboe printk(KERN_DEBUG 110903ba3782SJens Axboe "%s(%d): dirtied inode %lu (%s) on %s\n", 111003ba3782SJens Axboe current->comm, task_pid_nr(current), inode->i_ino, 111103ba3782SJens Axboe name, inode->i_sb->s_id); 111203ba3782SJens Axboe if (dentry) { 111303ba3782SJens Axboe spin_unlock(&dentry->d_lock); 111403ba3782SJens Axboe dput(dentry); 111503ba3782SJens Axboe } 111603ba3782SJens Axboe } 111703ba3782SJens Axboe } 111803ba3782SJens Axboe 111903ba3782SJens Axboe /** 112003ba3782SJens Axboe * __mark_inode_dirty - internal function 112103ba3782SJens Axboe * @inode: inode to mark 112203ba3782SJens Axboe * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) 112303ba3782SJens Axboe * Mark an inode as dirty. Callers should use mark_inode_dirty or 112403ba3782SJens Axboe * mark_inode_dirty_sync. 112503ba3782SJens Axboe * 112603ba3782SJens Axboe * Put the inode on the super block's dirty list. 112703ba3782SJens Axboe * 112803ba3782SJens Axboe * CAREFUL! We mark it dirty unconditionally, but move it onto the 112903ba3782SJens Axboe * dirty list only if it is hashed or if it refers to a blockdev. 113003ba3782SJens Axboe * If it was not hashed, it will never be added to the dirty list 113103ba3782SJens Axboe * even if it is later hashed, as it will have been marked dirty already. 113203ba3782SJens Axboe * 113303ba3782SJens Axboe * In short, make sure you hash any inodes _before_ you start marking 113403ba3782SJens Axboe * them dirty. 113503ba3782SJens Axboe * 113603ba3782SJens Axboe * Note that for blockdevs, inode->dirtied_when represents the dirtying time of 113703ba3782SJens Axboe * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of 113803ba3782SJens Axboe * the kernel-internal blockdev inode represents the dirtying time of the 113903ba3782SJens Axboe * blockdev's pages. This is why for I_DIRTY_PAGES we always use 114003ba3782SJens Axboe * page->mapping->host, so the page-dirtying time is recorded in the internal 114103ba3782SJens Axboe * blockdev inode. 114203ba3782SJens Axboe */ 114303ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags) 114403ba3782SJens Axboe { 114503ba3782SJens Axboe struct super_block *sb = inode->i_sb; 1146253c34e9SArtem Bityutskiy struct backing_dev_info *bdi = NULL; 114703ba3782SJens Axboe 114803ba3782SJens Axboe /* 114903ba3782SJens Axboe * Don't do this for I_DIRTY_PAGES - that doesn't actually 115003ba3782SJens Axboe * dirty the inode itself 115103ba3782SJens Axboe */ 115203ba3782SJens Axboe if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { 115303ba3782SJens Axboe if (sb->s_op->dirty_inode) 1154aa385729SChristoph Hellwig sb->s_op->dirty_inode(inode, flags); 115503ba3782SJens Axboe } 115603ba3782SJens Axboe 115703ba3782SJens Axboe /* 115803ba3782SJens Axboe * make sure that changes are seen by all cpus before we test i_state 115903ba3782SJens Axboe * -- mikulas 116003ba3782SJens Axboe */ 116103ba3782SJens Axboe smp_mb(); 116203ba3782SJens Axboe 116303ba3782SJens Axboe /* avoid the locking if we can */ 116403ba3782SJens Axboe if ((inode->i_state & flags) == flags) 116503ba3782SJens Axboe return; 116603ba3782SJens Axboe 116703ba3782SJens Axboe if (unlikely(block_dump)) 116803ba3782SJens Axboe block_dump___mark_inode_dirty(inode); 116903ba3782SJens Axboe 1170250df6edSDave Chinner spin_lock(&inode->i_lock); 117103ba3782SJens Axboe if ((inode->i_state & flags) != flags) { 117203ba3782SJens Axboe const int was_dirty = inode->i_state & I_DIRTY; 117303ba3782SJens Axboe 117403ba3782SJens Axboe inode->i_state |= flags; 117503ba3782SJens Axboe 117603ba3782SJens Axboe /* 117703ba3782SJens Axboe * If the inode is being synced, just update its dirty state. 117803ba3782SJens Axboe * The unlocker will place the inode on the appropriate 117903ba3782SJens Axboe * superblock list, based upon its state. 118003ba3782SJens Axboe */ 118103ba3782SJens Axboe if (inode->i_state & I_SYNC) 1182250df6edSDave Chinner goto out_unlock_inode; 118303ba3782SJens Axboe 118403ba3782SJens Axboe /* 118503ba3782SJens Axboe * Only add valid (hashed) inodes to the superblock's 118603ba3782SJens Axboe * dirty list. Add blockdev inodes as well. 118703ba3782SJens Axboe */ 118803ba3782SJens Axboe if (!S_ISBLK(inode->i_mode)) { 11891d3382cbSAl Viro if (inode_unhashed(inode)) 1190250df6edSDave Chinner goto out_unlock_inode; 119103ba3782SJens Axboe } 1192a4ffdde6SAl Viro if (inode->i_state & I_FREEING) 1193250df6edSDave Chinner goto out_unlock_inode; 119403ba3782SJens Axboe 119503ba3782SJens Axboe /* 119603ba3782SJens Axboe * If the inode was already on b_dirty/b_io/b_more_io, don't 119703ba3782SJens Axboe * reposition it (that would break b_dirty time-ordering). 119803ba3782SJens Axboe */ 119903ba3782SJens Axboe if (!was_dirty) { 1200a66979abSDave Chinner bool wakeup_bdi = false; 1201253c34e9SArtem Bityutskiy bdi = inode_to_bdi(inode); 1202500b067cSJens Axboe 1203253c34e9SArtem Bityutskiy if (bdi_cap_writeback_dirty(bdi)) { 1204253c34e9SArtem Bityutskiy WARN(!test_bit(BDI_registered, &bdi->state), 1205253c34e9SArtem Bityutskiy "bdi-%s not registered\n", bdi->name); 1206253c34e9SArtem Bityutskiy 1207253c34e9SArtem Bityutskiy /* 1208253c34e9SArtem Bityutskiy * If this is the first dirty inode for this 1209253c34e9SArtem Bityutskiy * bdi, we have to wake-up the corresponding 1210253c34e9SArtem Bityutskiy * bdi thread to make sure background 1211253c34e9SArtem Bityutskiy * write-back happens later. 1212253c34e9SArtem Bityutskiy */ 1213253c34e9SArtem Bityutskiy if (!wb_has_dirty_io(&bdi->wb)) 1214253c34e9SArtem Bityutskiy wakeup_bdi = true; 1215500b067cSJens Axboe } 121603ba3782SJens Axboe 1217a66979abSDave Chinner spin_unlock(&inode->i_lock); 1218f758eeabSChristoph Hellwig spin_lock(&bdi->wb.list_lock); 121903ba3782SJens Axboe inode->dirtied_when = jiffies; 12207ccf19a8SNick Piggin list_move(&inode->i_wb_list, &bdi->wb.b_dirty); 1221f758eeabSChristoph Hellwig spin_unlock(&bdi->wb.list_lock); 1222253c34e9SArtem Bityutskiy 1223253c34e9SArtem Bityutskiy if (wakeup_bdi) 12246467716aSArtem Bityutskiy bdi_wakeup_thread_delayed(bdi); 1225a66979abSDave Chinner return; 1226a66979abSDave Chinner } 1227a66979abSDave Chinner } 1228a66979abSDave Chinner out_unlock_inode: 1229a66979abSDave Chinner spin_unlock(&inode->i_lock); 1230a66979abSDave Chinner 123103ba3782SJens Axboe } 123203ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty); 123303ba3782SJens Axboe 1234b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb) 123566f3b8e2SJens Axboe { 123638f21977SNick Piggin struct inode *inode, *old_inode = NULL; 123738f21977SNick Piggin 123803ba3782SJens Axboe /* 123903ba3782SJens Axboe * We need to be protected against the filesystem going from 124003ba3782SJens Axboe * r/o to r/w or vice versa. 124103ba3782SJens Axboe */ 1242b6e51316SJens Axboe WARN_ON(!rwsem_is_locked(&sb->s_umount)); 124303ba3782SJens Axboe 124455fa6091SDave Chinner spin_lock(&inode_sb_list_lock); 124566f3b8e2SJens Axboe 124638f21977SNick Piggin /* 124738f21977SNick Piggin * Data integrity sync. Must wait for all pages under writeback, 124838f21977SNick Piggin * because there may have been pages dirtied before our sync 124938f21977SNick Piggin * call, but which had writeout started before we write it out. 125038f21977SNick Piggin * In which case, the inode may not be on the dirty list, but 125138f21977SNick Piggin * we still have to wait for that writeout. 125238f21977SNick Piggin */ 1253b6e51316SJens Axboe list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 1254250df6edSDave Chinner struct address_space *mapping = inode->i_mapping; 125538f21977SNick Piggin 1256250df6edSDave Chinner spin_lock(&inode->i_lock); 1257250df6edSDave Chinner if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) || 1258250df6edSDave Chinner (mapping->nrpages == 0)) { 1259250df6edSDave Chinner spin_unlock(&inode->i_lock); 126038f21977SNick Piggin continue; 1261250df6edSDave Chinner } 126238f21977SNick Piggin __iget(inode); 1263250df6edSDave Chinner spin_unlock(&inode->i_lock); 126455fa6091SDave Chinner spin_unlock(&inode_sb_list_lock); 126555fa6091SDave Chinner 126638f21977SNick Piggin /* 126755fa6091SDave Chinner * We hold a reference to 'inode' so it couldn't have been 126855fa6091SDave Chinner * removed from s_inodes list while we dropped the 126955fa6091SDave Chinner * inode_sb_list_lock. We cannot iput the inode now as we can 127055fa6091SDave Chinner * be holding the last reference and we cannot iput it under 127155fa6091SDave Chinner * inode_sb_list_lock. So we keep the reference and iput it 127255fa6091SDave Chinner * later. 127338f21977SNick Piggin */ 127438f21977SNick Piggin iput(old_inode); 127538f21977SNick Piggin old_inode = inode; 127638f21977SNick Piggin 127738f21977SNick Piggin filemap_fdatawait(mapping); 127838f21977SNick Piggin 127938f21977SNick Piggin cond_resched(); 128038f21977SNick Piggin 128155fa6091SDave Chinner spin_lock(&inode_sb_list_lock); 128238f21977SNick Piggin } 128355fa6091SDave Chinner spin_unlock(&inode_sb_list_lock); 128438f21977SNick Piggin iput(old_inode); 128566f3b8e2SJens Axboe } 12861da177e4SLinus Torvalds 1287d8a8559cSJens Axboe /** 12883259f8beSChris Mason * writeback_inodes_sb_nr - writeback dirty inodes from given super_block 1289d8a8559cSJens Axboe * @sb: the superblock 12903259f8beSChris Mason * @nr: the number of pages to write 1291786228abSMarcos Paulo de Souza * @reason: reason why some writeback work initiated 12921da177e4SLinus Torvalds * 1293d8a8559cSJens Axboe * Start writeback on some inodes on this super_block. No guarantees are made 1294d8a8559cSJens Axboe * on how many (if any) will be written, and this function does not wait 12953259f8beSChris Mason * for IO completion of submitted IO. 12961da177e4SLinus Torvalds */ 12970e175a18SCurt Wohlgemuth void writeback_inodes_sb_nr(struct super_block *sb, 12980e175a18SCurt Wohlgemuth unsigned long nr, 12990e175a18SCurt Wohlgemuth enum wb_reason reason) 13001da177e4SLinus Torvalds { 130183ba7b07SChristoph Hellwig DECLARE_COMPLETION_ONSTACK(done); 130283ba7b07SChristoph Hellwig struct wb_writeback_work work = { 13033c4d7165SChristoph Hellwig .sb = sb, 13043c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_NONE, 13056e6938b6SWu Fengguang .tagged_writepages = 1, 130683ba7b07SChristoph Hellwig .done = &done, 13073259f8beSChris Mason .nr_pages = nr, 13080e175a18SCurt Wohlgemuth .reason = reason, 13093c4d7165SChristoph Hellwig }; 13100e3c9a22SJens Axboe 13116eedc701SJan Kara if (sb->s_bdi == &noop_backing_dev_info) 13126eedc701SJan Kara return; 1313cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 131483ba7b07SChristoph Hellwig bdi_queue_work(sb->s_bdi, &work); 131583ba7b07SChristoph Hellwig wait_for_completion(&done); 13161da177e4SLinus Torvalds } 13173259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr); 13183259f8beSChris Mason 13193259f8beSChris Mason /** 13203259f8beSChris Mason * writeback_inodes_sb - writeback dirty inodes from given super_block 13213259f8beSChris Mason * @sb: the superblock 1322786228abSMarcos Paulo de Souza * @reason: reason why some writeback work was initiated 13233259f8beSChris Mason * 13243259f8beSChris Mason * Start writeback on some inodes on this super_block. No guarantees are made 13253259f8beSChris Mason * on how many (if any) will be written, and this function does not wait 13263259f8beSChris Mason * for IO completion of submitted IO. 13273259f8beSChris Mason */ 13280e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 13293259f8beSChris Mason { 13300e175a18SCurt Wohlgemuth return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); 13313259f8beSChris Mason } 1332d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb); 1333d8a8559cSJens Axboe 1334d8a8559cSJens Axboe /** 133517bd55d0SEric Sandeen * writeback_inodes_sb_if_idle - start writeback if none underway 133617bd55d0SEric Sandeen * @sb: the superblock 1337786228abSMarcos Paulo de Souza * @reason: reason why some writeback work was initiated 133817bd55d0SEric Sandeen * 133917bd55d0SEric Sandeen * Invoke writeback_inodes_sb if no writeback is currently underway. 134017bd55d0SEric Sandeen * Returns 1 if writeback was started, 0 if not. 134117bd55d0SEric Sandeen */ 13420e175a18SCurt Wohlgemuth int writeback_inodes_sb_if_idle(struct super_block *sb, enum wb_reason reason) 134317bd55d0SEric Sandeen { 134417bd55d0SEric Sandeen if (!writeback_in_progress(sb->s_bdi)) { 1345cf37e972SChristoph Hellwig down_read(&sb->s_umount); 13460e175a18SCurt Wohlgemuth writeback_inodes_sb(sb, reason); 1347cf37e972SChristoph Hellwig up_read(&sb->s_umount); 134817bd55d0SEric Sandeen return 1; 134917bd55d0SEric Sandeen } else 135017bd55d0SEric Sandeen return 0; 135117bd55d0SEric Sandeen } 135217bd55d0SEric Sandeen EXPORT_SYMBOL(writeback_inodes_sb_if_idle); 135317bd55d0SEric Sandeen 135417bd55d0SEric Sandeen /** 1355c097b2caSFengguang Wu * writeback_inodes_sb_nr_if_idle - start writeback if none underway 13563259f8beSChris Mason * @sb: the superblock 13573259f8beSChris Mason * @nr: the number of pages to write 1358786228abSMarcos Paulo de Souza * @reason: reason why some writeback work was initiated 13593259f8beSChris Mason * 13603259f8beSChris Mason * Invoke writeback_inodes_sb if no writeback is currently underway. 13613259f8beSChris Mason * Returns 1 if writeback was started, 0 if not. 13623259f8beSChris Mason */ 13633259f8beSChris Mason int writeback_inodes_sb_nr_if_idle(struct super_block *sb, 13640e175a18SCurt Wohlgemuth unsigned long nr, 13650e175a18SCurt Wohlgemuth enum wb_reason reason) 13663259f8beSChris Mason { 13673259f8beSChris Mason if (!writeback_in_progress(sb->s_bdi)) { 13683259f8beSChris Mason down_read(&sb->s_umount); 13690e175a18SCurt Wohlgemuth writeback_inodes_sb_nr(sb, nr, reason); 13703259f8beSChris Mason up_read(&sb->s_umount); 13713259f8beSChris Mason return 1; 13723259f8beSChris Mason } else 13733259f8beSChris Mason return 0; 13743259f8beSChris Mason } 13753259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr_if_idle); 13763259f8beSChris Mason 13773259f8beSChris Mason /** 1378d8a8559cSJens Axboe * sync_inodes_sb - sync sb inode pages 1379d8a8559cSJens Axboe * @sb: the superblock 1380d8a8559cSJens Axboe * 1381d8a8559cSJens Axboe * This function writes and waits on any dirty inode belonging to this 1382cb9ef8d5SStefan Hajnoczi * super_block. 1383d8a8559cSJens Axboe */ 1384b6e51316SJens Axboe void sync_inodes_sb(struct super_block *sb) 1385d8a8559cSJens Axboe { 138683ba7b07SChristoph Hellwig DECLARE_COMPLETION_ONSTACK(done); 138783ba7b07SChristoph Hellwig struct wb_writeback_work work = { 13883c4d7165SChristoph Hellwig .sb = sb, 13893c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_ALL, 13903c4d7165SChristoph Hellwig .nr_pages = LONG_MAX, 13913c4d7165SChristoph Hellwig .range_cyclic = 0, 139283ba7b07SChristoph Hellwig .done = &done, 13930e175a18SCurt Wohlgemuth .reason = WB_REASON_SYNC, 13943c4d7165SChristoph Hellwig }; 13953c4d7165SChristoph Hellwig 13966eedc701SJan Kara /* Nothing to do? */ 13976eedc701SJan Kara if (sb->s_bdi == &noop_backing_dev_info) 13986eedc701SJan Kara return; 1399cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 1400cf37e972SChristoph Hellwig 140183ba7b07SChristoph Hellwig bdi_queue_work(sb->s_bdi, &work); 140283ba7b07SChristoph Hellwig wait_for_completion(&done); 140383ba7b07SChristoph Hellwig 1404b6e51316SJens Axboe wait_sb_inodes(sb); 1405d8a8559cSJens Axboe } 1406d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb); 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds /** 14091da177e4SLinus Torvalds * write_inode_now - write an inode to disk 14101da177e4SLinus Torvalds * @inode: inode to write to disk 14111da177e4SLinus Torvalds * @sync: whether the write should be synchronous or not 14121da177e4SLinus Torvalds * 14137f04c26dSAndrea Arcangeli * This function commits an inode to disk immediately if it is dirty. This is 14147f04c26dSAndrea Arcangeli * primarily needed by knfsd. 14157f04c26dSAndrea Arcangeli * 14167f04c26dSAndrea Arcangeli * The caller must either have a ref on the inode or must have set I_WILL_FREE. 14171da177e4SLinus Torvalds */ 14181da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync) 14191da177e4SLinus Torvalds { 1420f758eeabSChristoph Hellwig struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 14211da177e4SLinus Torvalds struct writeback_control wbc = { 14221da177e4SLinus Torvalds .nr_to_write = LONG_MAX, 142318914b18SMike Galbraith .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 1424111ebb6eSOGAWA Hirofumi .range_start = 0, 1425111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 14261da177e4SLinus Torvalds }; 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds if (!mapping_cap_writeback_dirty(inode->i_mapping)) 142949364ce2SAndrew Morton wbc.nr_to_write = 0; 14301da177e4SLinus Torvalds 14311da177e4SLinus Torvalds might_sleep(); 14324f8ad655SJan Kara return writeback_single_inode(inode, wb, &wbc); 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now); 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds /** 14371da177e4SLinus Torvalds * sync_inode - write an inode and its pages to disk. 14381da177e4SLinus Torvalds * @inode: the inode to sync 14391da177e4SLinus Torvalds * @wbc: controls the writeback mode 14401da177e4SLinus Torvalds * 14411da177e4SLinus Torvalds * sync_inode() will write an inode and its pages to disk. It will also 14421da177e4SLinus Torvalds * correctly update the inode on its superblock's dirty inode lists and will 14431da177e4SLinus Torvalds * update inode->i_state. 14441da177e4SLinus Torvalds * 14451da177e4SLinus Torvalds * The caller must have a ref on the inode. 14461da177e4SLinus Torvalds */ 14471da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc) 14481da177e4SLinus Torvalds { 14494f8ad655SJan Kara return writeback_single_inode(inode, &inode_to_bdi(inode)->wb, wbc); 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode); 1452c3765016SChristoph Hellwig 1453c3765016SChristoph Hellwig /** 1454c691b9d9SAndrew Morton * sync_inode_metadata - write an inode to disk 1455c3765016SChristoph Hellwig * @inode: the inode to sync 1456c3765016SChristoph Hellwig * @wait: wait for I/O to complete. 1457c3765016SChristoph Hellwig * 1458c691b9d9SAndrew Morton * Write an inode to disk and adjust its dirty state after completion. 1459c3765016SChristoph Hellwig * 1460c3765016SChristoph Hellwig * Note: only writes the actual inode, no associated data or other metadata. 1461c3765016SChristoph Hellwig */ 1462c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait) 1463c3765016SChristoph Hellwig { 1464c3765016SChristoph Hellwig struct writeback_control wbc = { 1465c3765016SChristoph Hellwig .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, 1466c3765016SChristoph Hellwig .nr_to_write = 0, /* metadata-only */ 1467c3765016SChristoph Hellwig }; 1468c3765016SChristoph Hellwig 1469c3765016SChristoph Hellwig return sync_inode(inode, &wbc); 1470c3765016SChristoph Hellwig } 1471c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata); 1472