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> 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> 291da177e4SLinus Torvalds #include <linux/buffer_head.h> 30455b2864SDave Chinner #include <linux/tracepoint.h> 3107f3f05cSDavid Howells #include "internal.h" 321da177e4SLinus Torvalds 33d0bceac7SJens Axboe /* 34bc31b86aSWu Fengguang * 4MB minimal write chunk size 35bc31b86aSWu Fengguang */ 36bc31b86aSWu Fengguang #define MIN_WRITEBACK_PAGES (4096UL >> (PAGE_CACHE_SHIFT - 10)) 37bc31b86aSWu Fengguang 38bc31b86aSWu Fengguang /* 39c4a77a6cSJens Axboe * Passed into wb_writeback(), essentially a subset of writeback_control 40c4a77a6cSJens Axboe */ 4183ba7b07SChristoph Hellwig struct wb_writeback_work { 42c4a77a6cSJens Axboe long nr_pages; 43c4a77a6cSJens Axboe struct super_block *sb; 44d46db3d5SWu Fengguang unsigned long *older_than_this; 45c4a77a6cSJens Axboe enum writeback_sync_modes sync_mode; 466e6938b6SWu Fengguang unsigned int tagged_writepages:1; 4752957fe1SH Hartley Sweeten unsigned int for_kupdate:1; 4852957fe1SH Hartley Sweeten unsigned int range_cyclic:1; 4952957fe1SH Hartley Sweeten unsigned int for_background:1; 500e175a18SCurt Wohlgemuth enum wb_reason reason; /* why was writeback initiated? */ 51c4a77a6cSJens Axboe 528010c3b6SJens Axboe struct list_head list; /* pending work list */ 5383ba7b07SChristoph Hellwig struct completion *done; /* set if the caller waits */ 5403ba3782SJens Axboe }; 5503ba3782SJens Axboe 56455b2864SDave Chinner /* 57455b2864SDave Chinner * Include the creation of the trace points after defining the 58455b2864SDave Chinner * wb_writeback_work structure so that the definition remains local to this 59455b2864SDave Chinner * file. 60455b2864SDave Chinner */ 61455b2864SDave Chinner #define CREATE_TRACE_POINTS 62455b2864SDave Chinner #include <trace/events/writeback.h> 63455b2864SDave Chinner 64455b2864SDave Chinner /* 65455b2864SDave Chinner * We don't actually have pdflush, but this one is exported though /proc... 66455b2864SDave Chinner */ 67455b2864SDave Chinner int nr_pdflush_threads; 68455b2864SDave Chinner 69f11b00f3SAdrian Bunk /** 70f11b00f3SAdrian Bunk * writeback_in_progress - determine whether there is writeback in progress 71f11b00f3SAdrian Bunk * @bdi: the device's backing_dev_info structure. 72f11b00f3SAdrian Bunk * 7303ba3782SJens Axboe * Determine whether there is writeback waiting to be handled against a 7403ba3782SJens Axboe * backing device. 75f11b00f3SAdrian Bunk */ 76f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi) 77f11b00f3SAdrian Bunk { 7881d73a32SJan Kara return test_bit(BDI_writeback_running, &bdi->state); 79f11b00f3SAdrian Bunk } 80f11b00f3SAdrian Bunk 81692ebd17SJan Kara static inline struct backing_dev_info *inode_to_bdi(struct inode *inode) 82692ebd17SJan Kara { 83692ebd17SJan Kara struct super_block *sb = inode->i_sb; 84692ebd17SJan Kara 85aaead25bSChristoph Hellwig if (strcmp(sb->s_type->name, "bdev") == 0) 86aaead25bSChristoph Hellwig return inode->i_mapping->backing_dev_info; 87aaead25bSChristoph Hellwig 88692ebd17SJan Kara return sb->s_bdi; 89692ebd17SJan Kara } 90692ebd17SJan Kara 917ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head) 927ccf19a8SNick Piggin { 937ccf19a8SNick Piggin return list_entry(head, struct inode, i_wb_list); 947ccf19a8SNick Piggin } 957ccf19a8SNick Piggin 966585027aSJan Kara /* Wakeup flusher thread or forker thread to fork it. Requires bdi->wb_lock. */ 976585027aSJan Kara static void bdi_wakeup_flusher(struct backing_dev_info *bdi) 984195f73dSNick Piggin { 99fff5b85aSArtem Bityutskiy if (bdi->wb.task) { 100fff5b85aSArtem Bityutskiy wake_up_process(bdi->wb.task); 101fff5b85aSArtem Bityutskiy } else { 1021da177e4SLinus Torvalds /* 103fff5b85aSArtem Bityutskiy * The bdi thread isn't there, wake up the forker thread which 104fff5b85aSArtem Bityutskiy * will create and run it. 1051da177e4SLinus Torvalds */ 10603ba3782SJens Axboe wake_up_process(default_backing_dev_info.wb.task); 1071da177e4SLinus Torvalds } 1086585027aSJan Kara } 1096585027aSJan Kara 1106585027aSJan Kara static void bdi_queue_work(struct backing_dev_info *bdi, 1116585027aSJan Kara struct wb_writeback_work *work) 1126585027aSJan Kara { 1136585027aSJan Kara trace_writeback_queue(bdi, work); 1146585027aSJan Kara 1156585027aSJan Kara spin_lock_bh(&bdi->wb_lock); 1166585027aSJan Kara list_add_tail(&work->list, &bdi->work_list); 1176585027aSJan Kara if (!bdi->wb.task) 1186585027aSJan Kara trace_writeback_nothread(bdi, work); 1196585027aSJan Kara bdi_wakeup_flusher(bdi); 1206467716aSArtem Bityutskiy spin_unlock_bh(&bdi->wb_lock); 12103ba3782SJens Axboe } 1221da177e4SLinus Torvalds 12383ba7b07SChristoph Hellwig static void 12483ba7b07SChristoph Hellwig __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages, 1250e175a18SCurt Wohlgemuth bool range_cyclic, enum wb_reason reason) 1261da177e4SLinus Torvalds { 12783ba7b07SChristoph Hellwig struct wb_writeback_work *work; 12803ba3782SJens Axboe 129bcddc3f0SJens Axboe /* 130bcddc3f0SJens Axboe * This is WB_SYNC_NONE writeback, so if allocation fails just 131bcddc3f0SJens Axboe * wakeup the thread for old dirty data writeback 132bcddc3f0SJens Axboe */ 13383ba7b07SChristoph Hellwig work = kzalloc(sizeof(*work), GFP_ATOMIC); 13483ba7b07SChristoph Hellwig if (!work) { 135455b2864SDave Chinner if (bdi->wb.task) { 136455b2864SDave Chinner trace_writeback_nowork(bdi); 13783ba7b07SChristoph Hellwig wake_up_process(bdi->wb.task); 138455b2864SDave Chinner } 13983ba7b07SChristoph Hellwig return; 14083ba7b07SChristoph Hellwig } 14183ba7b07SChristoph Hellwig 14283ba7b07SChristoph Hellwig work->sync_mode = WB_SYNC_NONE; 14383ba7b07SChristoph Hellwig work->nr_pages = nr_pages; 14483ba7b07SChristoph Hellwig work->range_cyclic = range_cyclic; 1450e175a18SCurt Wohlgemuth work->reason = reason; 14683ba7b07SChristoph Hellwig 147f11fcae8SJens Axboe bdi_queue_work(bdi, work); 14803ba3782SJens Axboe } 149b6e51316SJens Axboe 150b6e51316SJens Axboe /** 151b6e51316SJens Axboe * bdi_start_writeback - start writeback 152b6e51316SJens Axboe * @bdi: the backing device to write from 153b6e51316SJens Axboe * @nr_pages: the number of pages to write 154786228abSMarcos Paulo de Souza * @reason: reason why some writeback work was initiated 155b6e51316SJens Axboe * 156b6e51316SJens Axboe * Description: 157b6e51316SJens Axboe * This does WB_SYNC_NONE opportunistic writeback. The IO is only 15825985edcSLucas De Marchi * started when this function returns, we make no guarantees on 1590e3c9a22SJens Axboe * completion. Caller need not hold sb s_umount semaphore. 160b6e51316SJens Axboe * 161b6e51316SJens Axboe */ 1620e175a18SCurt Wohlgemuth void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages, 1630e175a18SCurt Wohlgemuth enum wb_reason reason) 164b6e51316SJens Axboe { 1650e175a18SCurt Wohlgemuth __bdi_start_writeback(bdi, nr_pages, true, reason); 166d3ddec76SWu Fengguang } 167d3ddec76SWu Fengguang 168c5444198SChristoph Hellwig /** 169c5444198SChristoph Hellwig * bdi_start_background_writeback - start background writeback 170c5444198SChristoph Hellwig * @bdi: the backing device to write from 171c5444198SChristoph Hellwig * 172c5444198SChristoph Hellwig * Description: 1736585027aSJan Kara * This makes sure WB_SYNC_NONE background writeback happens. When 1746585027aSJan Kara * this function returns, it is only guaranteed that for given BDI 1756585027aSJan Kara * some IO is happening if we are over background dirty threshold. 1766585027aSJan Kara * Caller need not hold sb s_umount semaphore. 177c5444198SChristoph Hellwig */ 178c5444198SChristoph Hellwig void bdi_start_background_writeback(struct backing_dev_info *bdi) 179c5444198SChristoph Hellwig { 1806585027aSJan Kara /* 1816585027aSJan Kara * We just wake up the flusher thread. It will perform background 1826585027aSJan Kara * writeback as soon as there is no other work to do. 1836585027aSJan Kara */ 18471927e84SWu Fengguang trace_writeback_wake_background(bdi); 1856585027aSJan Kara spin_lock_bh(&bdi->wb_lock); 1866585027aSJan Kara bdi_wakeup_flusher(bdi); 1876585027aSJan Kara spin_unlock_bh(&bdi->wb_lock); 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds /* 191a66979abSDave Chinner * Remove the inode from the writeback list it is on. 192a66979abSDave Chinner */ 193a66979abSDave Chinner void inode_wb_list_del(struct inode *inode) 194a66979abSDave Chinner { 195f758eeabSChristoph Hellwig struct backing_dev_info *bdi = inode_to_bdi(inode); 196a66979abSDave Chinner 197f758eeabSChristoph Hellwig spin_lock(&bdi->wb.list_lock); 198f758eeabSChristoph Hellwig list_del_init(&inode->i_wb_list); 199f758eeabSChristoph Hellwig spin_unlock(&bdi->wb.list_lock); 200f758eeabSChristoph Hellwig } 201a66979abSDave Chinner 202a66979abSDave Chinner /* 2036610a0bcSAndrew Morton * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 2046610a0bcSAndrew Morton * furthest end of its superblock's dirty-inode list. 2056610a0bcSAndrew Morton * 2066610a0bcSAndrew Morton * Before stamping the inode's ->dirtied_when, we check to see whether it is 20766f3b8e2SJens Axboe * already the most-recently-dirtied inode on the b_dirty list. If that is 2086610a0bcSAndrew Morton * the case then the inode must have been redirtied while it was being written 2096610a0bcSAndrew Morton * out and we don't reset its dirtied_when. 2106610a0bcSAndrew Morton */ 211f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb) 2126610a0bcSAndrew Morton { 213f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 21403ba3782SJens Axboe if (!list_empty(&wb->b_dirty)) { 21566f3b8e2SJens Axboe struct inode *tail; 2166610a0bcSAndrew Morton 2177ccf19a8SNick Piggin tail = wb_inode(wb->b_dirty.next); 21866f3b8e2SJens Axboe if (time_before(inode->dirtied_when, tail->dirtied_when)) 2196610a0bcSAndrew Morton inode->dirtied_when = jiffies; 2206610a0bcSAndrew Morton } 2217ccf19a8SNick Piggin list_move(&inode->i_wb_list, &wb->b_dirty); 2226610a0bcSAndrew Morton } 2236610a0bcSAndrew Morton 2246610a0bcSAndrew Morton /* 22566f3b8e2SJens Axboe * requeue inode for re-scanning after bdi->b_io list is exhausted. 226c986d1e2SAndrew Morton */ 227f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb) 228c986d1e2SAndrew Morton { 229f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 2307ccf19a8SNick Piggin list_move(&inode->i_wb_list, &wb->b_more_io); 231c986d1e2SAndrew Morton } 232c986d1e2SAndrew Morton 2331c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode) 2341c0eeaf5SJoern Engel { 2351c0eeaf5SJoern Engel /* 236a66979abSDave Chinner * Prevent speculative execution through 237f758eeabSChristoph Hellwig * spin_unlock(&wb->list_lock); 2381c0eeaf5SJoern Engel */ 239a66979abSDave Chinner 2401c0eeaf5SJoern Engel smp_mb(); 2411c0eeaf5SJoern Engel wake_up_bit(&inode->i_state, __I_SYNC); 2421c0eeaf5SJoern Engel } 2431c0eeaf5SJoern Engel 244d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t) 245d2caa3c5SJeff Layton { 246d2caa3c5SJeff Layton bool ret = time_after(inode->dirtied_when, t); 247d2caa3c5SJeff Layton #ifndef CONFIG_64BIT 248d2caa3c5SJeff Layton /* 249d2caa3c5SJeff Layton * For inodes being constantly redirtied, dirtied_when can get stuck. 250d2caa3c5SJeff Layton * It _appears_ to be in the future, but is actually in distant past. 251d2caa3c5SJeff Layton * This test is necessary to prevent such wrapped-around relative times 2525b0830cbSJens Axboe * from permanently stopping the whole bdi writeback. 253d2caa3c5SJeff Layton */ 254d2caa3c5SJeff Layton ret = ret && time_before_eq(inode->dirtied_when, jiffies); 255d2caa3c5SJeff Layton #endif 256d2caa3c5SJeff Layton return ret; 257d2caa3c5SJeff Layton } 258d2caa3c5SJeff Layton 259c986d1e2SAndrew Morton /* 2602c136579SFengguang Wu * Move expired dirty inodes from @delaying_queue to @dispatch_queue. 2612c136579SFengguang Wu */ 262e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue, 2632c136579SFengguang Wu struct list_head *dispatch_queue, 264ad4e38ddSCurt Wohlgemuth struct wb_writeback_work *work) 2652c136579SFengguang Wu { 2665c03449dSShaohua Li LIST_HEAD(tmp); 2675c03449dSShaohua Li struct list_head *pos, *node; 268cf137307SJens Axboe struct super_block *sb = NULL; 2695c03449dSShaohua Li struct inode *inode; 270cf137307SJens Axboe int do_sb_sort = 0; 271e84d0a4fSWu Fengguang int moved = 0; 2725c03449dSShaohua Li 2732c136579SFengguang Wu while (!list_empty(delaying_queue)) { 2747ccf19a8SNick Piggin inode = wb_inode(delaying_queue->prev); 275ad4e38ddSCurt Wohlgemuth if (work->older_than_this && 276ad4e38ddSCurt Wohlgemuth inode_dirtied_after(inode, *work->older_than_this)) 2772c136579SFengguang Wu break; 278cf137307SJens Axboe if (sb && sb != inode->i_sb) 279cf137307SJens Axboe do_sb_sort = 1; 280cf137307SJens Axboe sb = inode->i_sb; 2817ccf19a8SNick Piggin list_move(&inode->i_wb_list, &tmp); 282e84d0a4fSWu Fengguang moved++; 2835c03449dSShaohua Li } 2845c03449dSShaohua Li 285cf137307SJens Axboe /* just one sb in list, splice to dispatch_queue and we're done */ 286cf137307SJens Axboe if (!do_sb_sort) { 287cf137307SJens Axboe list_splice(&tmp, dispatch_queue); 288e84d0a4fSWu Fengguang goto out; 289cf137307SJens Axboe } 290cf137307SJens Axboe 2915c03449dSShaohua Li /* Move inodes from one superblock together */ 2925c03449dSShaohua Li while (!list_empty(&tmp)) { 2937ccf19a8SNick Piggin sb = wb_inode(tmp.prev)->i_sb; 2945c03449dSShaohua Li list_for_each_prev_safe(pos, node, &tmp) { 2957ccf19a8SNick Piggin inode = wb_inode(pos); 2965c03449dSShaohua Li if (inode->i_sb == sb) 2977ccf19a8SNick Piggin list_move(&inode->i_wb_list, dispatch_queue); 2982c136579SFengguang Wu } 2992c136579SFengguang Wu } 300e84d0a4fSWu Fengguang out: 301e84d0a4fSWu Fengguang return moved; 3025c03449dSShaohua Li } 3032c136579SFengguang Wu 3042c136579SFengguang Wu /* 3052c136579SFengguang Wu * Queue all expired dirty inodes for io, eldest first. 3064ea879b9SWu Fengguang * Before 3074ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 3084ea879b9SWu Fengguang * =============> gf edc BA 3094ea879b9SWu Fengguang * After 3104ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 3114ea879b9SWu Fengguang * =============> g fBAedc 3124ea879b9SWu Fengguang * | 3134ea879b9SWu Fengguang * +--> dequeue for IO 3142c136579SFengguang Wu */ 315ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work) 3162c136579SFengguang Wu { 317e84d0a4fSWu Fengguang int moved; 318f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 3194ea879b9SWu Fengguang list_splice_init(&wb->b_more_io, &wb->b_io); 320ad4e38ddSCurt Wohlgemuth moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, work); 321ad4e38ddSCurt Wohlgemuth trace_writeback_queue_io(wb, work, moved); 32266f3b8e2SJens Axboe } 32366f3b8e2SJens Axboe 324a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc) 32566f3b8e2SJens Axboe { 32603ba3782SJens Axboe if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) 327a9185b41SChristoph Hellwig return inode->i_sb->s_op->write_inode(inode, wbc); 32803ba3782SJens Axboe return 0; 32966f3b8e2SJens Axboe } 33008d8e974SFengguang Wu 3312c136579SFengguang Wu /* 33201c03194SChristoph Hellwig * Wait for writeback on an inode to complete. 33301c03194SChristoph Hellwig */ 334f758eeabSChristoph Hellwig static void inode_wait_for_writeback(struct inode *inode, 335f758eeabSChristoph Hellwig struct bdi_writeback *wb) 33601c03194SChristoph Hellwig { 33701c03194SChristoph Hellwig DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); 33801c03194SChristoph Hellwig wait_queue_head_t *wqh; 33901c03194SChristoph Hellwig 34001c03194SChristoph Hellwig wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 34158a9d3d8SRichard Kennedy while (inode->i_state & I_SYNC) { 342250df6edSDave Chinner spin_unlock(&inode->i_lock); 343f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 34401c03194SChristoph Hellwig __wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE); 345f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 346250df6edSDave Chinner spin_lock(&inode->i_lock); 34758a9d3d8SRichard Kennedy } 34801c03194SChristoph Hellwig } 34901c03194SChristoph Hellwig 35001c03194SChristoph Hellwig /* 351f758eeabSChristoph Hellwig * Write out an inode's dirty pages. Called under wb->list_lock and 3520f1b1fd8SDave Chinner * inode->i_lock. Either the caller has an active reference on the inode or 3530f1b1fd8SDave Chinner * the inode has I_WILL_FREE set. 35401c03194SChristoph Hellwig * 3551da177e4SLinus Torvalds * If `wait' is set, wait on the writeout. 3561da177e4SLinus Torvalds * 3571da177e4SLinus Torvalds * The whole writeout design is quite complex and fragile. We want to avoid 3581da177e4SLinus Torvalds * starvation of particular inodes when others are being redirtied, prevent 3591da177e4SLinus Torvalds * livelocks, etc. 3601da177e4SLinus Torvalds */ 3611da177e4SLinus Torvalds static int 362f758eeabSChristoph Hellwig writeback_single_inode(struct inode *inode, struct bdi_writeback *wb, 363f758eeabSChristoph Hellwig struct writeback_control *wbc) 3641da177e4SLinus Torvalds { 3651da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 366251d6a47SWu Fengguang long nr_to_write = wbc->nr_to_write; 36701c03194SChristoph Hellwig unsigned dirty; 3681da177e4SLinus Torvalds int ret; 3691da177e4SLinus Torvalds 370f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 3710f1b1fd8SDave Chinner assert_spin_locked(&inode->i_lock); 3720f1b1fd8SDave Chinner 37301c03194SChristoph Hellwig if (!atomic_read(&inode->i_count)) 37401c03194SChristoph Hellwig WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); 37501c03194SChristoph Hellwig else 37601c03194SChristoph Hellwig WARN_ON(inode->i_state & I_WILL_FREE); 37701c03194SChristoph Hellwig 37801c03194SChristoph Hellwig if (inode->i_state & I_SYNC) { 37901c03194SChristoph Hellwig /* 38001c03194SChristoph Hellwig * If this inode is locked for writeback and we are not doing 38166f3b8e2SJens Axboe * writeback-for-data-integrity, move it to b_more_io so that 38201c03194SChristoph Hellwig * writeback can proceed with the other inodes on s_io. 38301c03194SChristoph Hellwig * 38401c03194SChristoph Hellwig * We'll have another go at writing back this inode when we 38566f3b8e2SJens Axboe * completed a full scan of b_io. 38601c03194SChristoph Hellwig */ 387a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) { 388f758eeabSChristoph Hellwig requeue_io(inode, wb); 389251d6a47SWu Fengguang trace_writeback_single_inode_requeue(inode, wbc, 390251d6a47SWu Fengguang nr_to_write); 39101c03194SChristoph Hellwig return 0; 39201c03194SChristoph Hellwig } 39301c03194SChristoph Hellwig 39401c03194SChristoph Hellwig /* 39501c03194SChristoph Hellwig * It's a data-integrity sync. We must wait. 39601c03194SChristoph Hellwig */ 397f758eeabSChristoph Hellwig inode_wait_for_writeback(inode, wb); 39801c03194SChristoph Hellwig } 39901c03194SChristoph Hellwig 4001c0eeaf5SJoern Engel BUG_ON(inode->i_state & I_SYNC); 4011da177e4SLinus Torvalds 4025547e8aaSDmitry Monakhov /* Set I_SYNC, reset I_DIRTY_PAGES */ 4031c0eeaf5SJoern Engel inode->i_state |= I_SYNC; 4045547e8aaSDmitry Monakhov inode->i_state &= ~I_DIRTY_PAGES; 405250df6edSDave Chinner spin_unlock(&inode->i_lock); 406f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds ret = do_writepages(mapping, wbc); 4091da177e4SLinus Torvalds 41026821ed4SChristoph Hellwig /* 41126821ed4SChristoph Hellwig * Make sure to wait on the data before writing out the metadata. 41226821ed4SChristoph Hellwig * This is important for filesystems that modify metadata on data 41326821ed4SChristoph Hellwig * I/O completion. 41426821ed4SChristoph Hellwig */ 415a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) { 41626821ed4SChristoph Hellwig int err = filemap_fdatawait(mapping); 4171da177e4SLinus Torvalds if (ret == 0) 4181da177e4SLinus Torvalds ret = err; 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 4215547e8aaSDmitry Monakhov /* 4225547e8aaSDmitry Monakhov * Some filesystems may redirty the inode during the writeback 4235547e8aaSDmitry Monakhov * due to delalloc, clear dirty metadata flags right before 4245547e8aaSDmitry Monakhov * write_inode() 4255547e8aaSDmitry Monakhov */ 426250df6edSDave Chinner spin_lock(&inode->i_lock); 4275547e8aaSDmitry Monakhov dirty = inode->i_state & I_DIRTY; 4285547e8aaSDmitry Monakhov inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC); 429250df6edSDave Chinner spin_unlock(&inode->i_lock); 43026821ed4SChristoph Hellwig /* Don't write the inode if only I_DIRTY_PAGES was set */ 43126821ed4SChristoph Hellwig if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { 432a9185b41SChristoph Hellwig int err = write_inode(inode, wbc); 4331da177e4SLinus Torvalds if (ret == 0) 4341da177e4SLinus Torvalds ret = err; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 437f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 438250df6edSDave Chinner spin_lock(&inode->i_lock); 4391c0eeaf5SJoern Engel inode->i_state &= ~I_SYNC; 440a4ffdde6SAl Viro if (!(inode->i_state & I_FREEING)) { 44194c3dcbbSWu Fengguang /* 44294c3dcbbSWu Fengguang * Sync livelock prevention. Each inode is tagged and synced in 44394c3dcbbSWu Fengguang * one shot. If still dirty, it will be redirty_tail()'ed below. 44494c3dcbbSWu Fengguang * Update the dirty time to prevent enqueue and sync it again. 44594c3dcbbSWu Fengguang */ 44694c3dcbbSWu Fengguang if ((inode->i_state & I_DIRTY) && 44794c3dcbbSWu Fengguang (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)) 44894c3dcbbSWu Fengguang inode->dirtied_when = jiffies; 44994c3dcbbSWu Fengguang 45023539afcSWu Fengguang if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 4511da177e4SLinus Torvalds /* 4521da177e4SLinus Torvalds * We didn't write back all the pages. nfs_writepages() 453a50aeb40SWu Fengguang * sometimes bales out without doing anything. 4541da177e4SLinus Torvalds */ 4551da177e4SLinus Torvalds inode->i_state |= I_DIRTY_PAGES; 4568bc3be27SFengguang Wu if (wbc->nr_to_write <= 0) { 4578bc3be27SFengguang Wu /* 4588bc3be27SFengguang Wu * slice used up: queue for next turn 4598bc3be27SFengguang Wu */ 460f758eeabSChristoph Hellwig requeue_io(inode, wb); 4611da177e4SLinus Torvalds } else { 4621da177e4SLinus Torvalds /* 463a50aeb40SWu Fengguang * Writeback blocked by something other than 464a50aeb40SWu Fengguang * congestion. Delay the inode for some time to 465a50aeb40SWu Fengguang * avoid spinning on the CPU (100% iowait) 466a50aeb40SWu Fengguang * retrying writeback of the dirty page/inode 467a50aeb40SWu Fengguang * that cannot be performed immediately. 4688bc3be27SFengguang Wu */ 469f758eeabSChristoph Hellwig redirty_tail(inode, wb); 4708bc3be27SFengguang Wu } 47123539afcSWu Fengguang } else if (inode->i_state & I_DIRTY) { 47223539afcSWu Fengguang /* 47323539afcSWu Fengguang * Filesystems can dirty the inode during writeback 47423539afcSWu Fengguang * operations, such as delayed allocation during 47523539afcSWu Fengguang * submission or metadata updates after data IO 47623539afcSWu Fengguang * completion. 47723539afcSWu Fengguang */ 478f758eeabSChristoph Hellwig redirty_tail(inode, wb); 4791da177e4SLinus Torvalds } else { 4801da177e4SLinus Torvalds /* 4819e38d86fSNick Piggin * The inode is clean. At this point we either have 4829e38d86fSNick Piggin * a reference to the inode or it's on it's way out. 4839e38d86fSNick Piggin * No need to add it back to the LRU. 4841da177e4SLinus Torvalds */ 4857ccf19a8SNick Piggin list_del_init(&inode->i_wb_list); 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds } 4881c0eeaf5SJoern Engel inode_sync_complete(inode); 489251d6a47SWu Fengguang trace_writeback_single_inode(inode, wbc, nr_to_write); 4901da177e4SLinus Torvalds return ret; 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds 4931a12d8bdSWu Fengguang static long writeback_chunk_size(struct backing_dev_info *bdi, 4941a12d8bdSWu Fengguang struct wb_writeback_work *work) 495d46db3d5SWu Fengguang { 496d46db3d5SWu Fengguang long pages; 497d46db3d5SWu Fengguang 498d46db3d5SWu Fengguang /* 499d46db3d5SWu Fengguang * WB_SYNC_ALL mode does livelock avoidance by syncing dirty 500d46db3d5SWu Fengguang * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX 501d46db3d5SWu Fengguang * here avoids calling into writeback_inodes_wb() more than once. 502d46db3d5SWu Fengguang * 503d46db3d5SWu Fengguang * The intended call sequence for WB_SYNC_ALL writeback is: 504d46db3d5SWu Fengguang * 505d46db3d5SWu Fengguang * wb_writeback() 506d46db3d5SWu Fengguang * writeback_sb_inodes() <== called only once 507d46db3d5SWu Fengguang * write_cache_pages() <== called once for each inode 508d46db3d5SWu Fengguang * (quickly) tag currently dirty pages 509d46db3d5SWu Fengguang * (maybe slowly) sync all tagged pages 510d46db3d5SWu Fengguang */ 511d46db3d5SWu Fengguang if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages) 512d46db3d5SWu Fengguang pages = LONG_MAX; 5131a12d8bdSWu Fengguang else { 5141a12d8bdSWu Fengguang pages = min(bdi->avg_write_bandwidth / 2, 5151a12d8bdSWu Fengguang global_dirty_limit / DIRTY_SCOPE); 5161a12d8bdSWu Fengguang pages = min(pages, work->nr_pages); 5171a12d8bdSWu Fengguang pages = round_down(pages + MIN_WRITEBACK_PAGES, 5181a12d8bdSWu Fengguang MIN_WRITEBACK_PAGES); 5191a12d8bdSWu Fengguang } 520d46db3d5SWu Fengguang 521d46db3d5SWu Fengguang return pages; 522d46db3d5SWu Fengguang } 523d46db3d5SWu Fengguang 52403ba3782SJens Axboe /* 525f11c9c5cSEdward Shishkin * Write a portion of b_io inodes which belong to @sb. 526edadfb10SChristoph Hellwig * 527edadfb10SChristoph Hellwig * If @only_this_sb is true, then find and write all such 528f11c9c5cSEdward Shishkin * inodes. Otherwise write only ones which go sequentially 529f11c9c5cSEdward Shishkin * in reverse order. 530edadfb10SChristoph Hellwig * 531d46db3d5SWu Fengguang * Return the number of pages and/or inodes written. 532f11c9c5cSEdward Shishkin */ 533d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb, 534d46db3d5SWu Fengguang struct bdi_writeback *wb, 535d46db3d5SWu Fengguang struct wb_writeback_work *work) 53603ba3782SJens Axboe { 537d46db3d5SWu Fengguang struct writeback_control wbc = { 538d46db3d5SWu Fengguang .sync_mode = work->sync_mode, 539d46db3d5SWu Fengguang .tagged_writepages = work->tagged_writepages, 540d46db3d5SWu Fengguang .for_kupdate = work->for_kupdate, 541d46db3d5SWu Fengguang .for_background = work->for_background, 542d46db3d5SWu Fengguang .range_cyclic = work->range_cyclic, 543d46db3d5SWu Fengguang .range_start = 0, 544d46db3d5SWu Fengguang .range_end = LLONG_MAX, 545d46db3d5SWu Fengguang }; 546d46db3d5SWu Fengguang unsigned long start_time = jiffies; 547d46db3d5SWu Fengguang long write_chunk; 548d46db3d5SWu Fengguang long wrote = 0; /* count both pages and inodes */ 549d46db3d5SWu Fengguang 55003ba3782SJens Axboe while (!list_empty(&wb->b_io)) { 5517ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 552edadfb10SChristoph Hellwig 553edadfb10SChristoph Hellwig if (inode->i_sb != sb) { 554d46db3d5SWu Fengguang if (work->sb) { 555edadfb10SChristoph Hellwig /* 556edadfb10SChristoph Hellwig * We only want to write back data for this 557edadfb10SChristoph Hellwig * superblock, move all inodes not belonging 558edadfb10SChristoph Hellwig * to it back onto the dirty list. 559edadfb10SChristoph Hellwig */ 560f758eeabSChristoph Hellwig redirty_tail(inode, wb); 56166f3b8e2SJens Axboe continue; 56266f3b8e2SJens Axboe } 563edadfb10SChristoph Hellwig 564edadfb10SChristoph Hellwig /* 565edadfb10SChristoph Hellwig * The inode belongs to a different superblock. 566edadfb10SChristoph Hellwig * Bounce back to the caller to unpin this and 567edadfb10SChristoph Hellwig * pin the next superblock. 568edadfb10SChristoph Hellwig */ 569d46db3d5SWu Fengguang break; 570edadfb10SChristoph Hellwig } 571edadfb10SChristoph Hellwig 5729843b76aSChristoph Hellwig /* 5739843b76aSChristoph Hellwig * Don't bother with new inodes or inodes beeing freed, first 5749843b76aSChristoph Hellwig * kind does not need peridic writeout yet, and for the latter 5759843b76aSChristoph Hellwig * kind writeout is handled by the freer. 5769843b76aSChristoph Hellwig */ 577250df6edSDave Chinner spin_lock(&inode->i_lock); 5789843b76aSChristoph Hellwig if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { 579250df6edSDave Chinner spin_unlock(&inode->i_lock); 580fcc5c222SWu Fengguang redirty_tail(inode, wb); 5817ef0d737SNick Piggin continue; 5827ef0d737SNick Piggin } 5831da177e4SLinus Torvalds __iget(inode); 5841a12d8bdSWu Fengguang write_chunk = writeback_chunk_size(wb->bdi, work); 585d46db3d5SWu Fengguang wbc.nr_to_write = write_chunk; 586d46db3d5SWu Fengguang wbc.pages_skipped = 0; 587250df6edSDave Chinner 588d46db3d5SWu Fengguang writeback_single_inode(inode, wb, &wbc); 589d46db3d5SWu Fengguang 590d46db3d5SWu Fengguang work->nr_pages -= write_chunk - wbc.nr_to_write; 591d46db3d5SWu Fengguang wrote += write_chunk - wbc.nr_to_write; 592d46db3d5SWu Fengguang if (!(inode->i_state & I_DIRTY)) 593d46db3d5SWu Fengguang wrote++; 594d46db3d5SWu Fengguang if (wbc.pages_skipped) { 5951da177e4SLinus Torvalds /* 5961da177e4SLinus Torvalds * writeback is not making progress due to locked 5971da177e4SLinus Torvalds * buffers. Skip this inode for now. 5981da177e4SLinus Torvalds */ 599f758eeabSChristoph Hellwig redirty_tail(inode, wb); 6001da177e4SLinus Torvalds } 6010f1b1fd8SDave Chinner spin_unlock(&inode->i_lock); 602f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 6031da177e4SLinus Torvalds iput(inode); 6044ffc8444SOGAWA Hirofumi cond_resched(); 605f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 606d46db3d5SWu Fengguang /* 607d46db3d5SWu Fengguang * bail out to wb_writeback() often enough to check 608d46db3d5SWu Fengguang * background threshold and other termination conditions. 609d46db3d5SWu Fengguang */ 610d46db3d5SWu Fengguang if (wrote) { 611d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 612d46db3d5SWu Fengguang break; 613d46db3d5SWu Fengguang if (work->nr_pages <= 0) 614d46db3d5SWu Fengguang break; 6151da177e4SLinus Torvalds } 6168bc3be27SFengguang Wu } 617d46db3d5SWu Fengguang return wrote; 618f11c9c5cSEdward Shishkin } 61938f21977SNick Piggin 620d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb, 621d46db3d5SWu Fengguang struct wb_writeback_work *work) 622f11c9c5cSEdward Shishkin { 623d46db3d5SWu Fengguang unsigned long start_time = jiffies; 624d46db3d5SWu Fengguang long wrote = 0; 625f11c9c5cSEdward Shishkin 626f11c9c5cSEdward Shishkin while (!list_empty(&wb->b_io)) { 6277ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 628f11c9c5cSEdward Shishkin struct super_block *sb = inode->i_sb; 629f11c9c5cSEdward Shishkin 63012ad3ab6SDave Chinner if (!grab_super_passive(sb)) { 6310e995816SWu Fengguang /* 6320e995816SWu Fengguang * grab_super_passive() may fail consistently due to 6330e995816SWu Fengguang * s_umount being grabbed by someone else. Don't use 6340e995816SWu Fengguang * requeue_io() to avoid busy retrying the inode/sb. 6350e995816SWu Fengguang */ 6360e995816SWu Fengguang redirty_tail(inode, wb); 637d19de7edSChristoph Hellwig continue; 638334132aeSChristoph Hellwig } 639d46db3d5SWu Fengguang wrote += writeback_sb_inodes(sb, wb, work); 640d19de7edSChristoph Hellwig drop_super(sb); 641f11c9c5cSEdward Shishkin 642d46db3d5SWu Fengguang /* refer to the same tests at the end of writeback_sb_inodes */ 643d46db3d5SWu Fengguang if (wrote) { 644d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 645d46db3d5SWu Fengguang break; 646d46db3d5SWu Fengguang if (work->nr_pages <= 0) 647f11c9c5cSEdward Shishkin break; 648f11c9c5cSEdward Shishkin } 649d46db3d5SWu Fengguang } 65066f3b8e2SJens Axboe /* Leave any unwritten inodes on b_io */ 651d46db3d5SWu Fengguang return wrote; 65266f3b8e2SJens Axboe } 65366f3b8e2SJens Axboe 6540e175a18SCurt Wohlgemuth long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages, 6550e175a18SCurt Wohlgemuth enum wb_reason reason) 656edadfb10SChristoph Hellwig { 657d46db3d5SWu Fengguang struct wb_writeback_work work = { 658d46db3d5SWu Fengguang .nr_pages = nr_pages, 659d46db3d5SWu Fengguang .sync_mode = WB_SYNC_NONE, 660d46db3d5SWu Fengguang .range_cyclic = 1, 6610e175a18SCurt Wohlgemuth .reason = reason, 662d46db3d5SWu Fengguang }; 663edadfb10SChristoph Hellwig 664f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 665424b351fSWu Fengguang if (list_empty(&wb->b_io)) 666ad4e38ddSCurt Wohlgemuth queue_io(wb, &work); 667d46db3d5SWu Fengguang __writeback_inodes_wb(wb, &work); 668f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 669edadfb10SChristoph Hellwig 670d46db3d5SWu Fengguang return nr_pages - work.nr_pages; 67166f3b8e2SJens Axboe } 67266f3b8e2SJens Axboe 673b00949aaSWu Fengguang static bool over_bground_thresh(struct backing_dev_info *bdi) 67403ba3782SJens Axboe { 67503ba3782SJens Axboe unsigned long background_thresh, dirty_thresh; 67603ba3782SJens Axboe 67716c4042fSWu Fengguang global_dirty_limits(&background_thresh, &dirty_thresh); 67803ba3782SJens Axboe 679b00949aaSWu Fengguang if (global_page_state(NR_FILE_DIRTY) + 680b00949aaSWu Fengguang global_page_state(NR_UNSTABLE_NFS) > background_thresh) 681b00949aaSWu Fengguang return true; 682b00949aaSWu Fengguang 683b00949aaSWu Fengguang if (bdi_stat(bdi, BDI_RECLAIMABLE) > 684b00949aaSWu Fengguang bdi_dirty_limit(bdi, background_thresh)) 685b00949aaSWu Fengguang return true; 686b00949aaSWu Fengguang 687b00949aaSWu Fengguang return false; 68803ba3782SJens Axboe } 68903ba3782SJens Axboe 69003ba3782SJens Axboe /* 691e98be2d5SWu Fengguang * Called under wb->list_lock. If there are multiple wb per bdi, 692e98be2d5SWu Fengguang * only the flusher working on the first wb should do it. 693e98be2d5SWu Fengguang */ 694e98be2d5SWu Fengguang static void wb_update_bandwidth(struct bdi_writeback *wb, 695e98be2d5SWu Fengguang unsigned long start_time) 696e98be2d5SWu Fengguang { 697af6a3113SWu Fengguang __bdi_update_bandwidth(wb->bdi, 0, 0, 0, 0, 0, start_time); 698e98be2d5SWu Fengguang } 699e98be2d5SWu Fengguang 700e98be2d5SWu Fengguang /* 70103ba3782SJens Axboe * Explicit flushing or periodic writeback of "old" data. 70203ba3782SJens Axboe * 70303ba3782SJens Axboe * Define "old": the first time one of an inode's pages is dirtied, we mark the 70403ba3782SJens Axboe * dirtying-time in the inode's address_space. So this periodic writeback code 70503ba3782SJens Axboe * just walks the superblock inode list, writing back any inodes which are 70603ba3782SJens Axboe * older than a specific point in time. 70703ba3782SJens Axboe * 70803ba3782SJens Axboe * Try to run once per dirty_writeback_interval. But if a writeback event 70903ba3782SJens Axboe * takes longer than a dirty_writeback_interval interval, then leave a 71003ba3782SJens Axboe * one-second gap. 71103ba3782SJens Axboe * 71203ba3782SJens Axboe * older_than_this takes precedence over nr_to_write. So we'll only write back 71303ba3782SJens Axboe * all dirty pages if they are all attached to "old" mappings. 71403ba3782SJens Axboe */ 715c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb, 71683ba7b07SChristoph Hellwig struct wb_writeback_work *work) 71703ba3782SJens Axboe { 718e98be2d5SWu Fengguang unsigned long wb_start = jiffies; 719d46db3d5SWu Fengguang long nr_pages = work->nr_pages; 72003ba3782SJens Axboe unsigned long oldest_jif; 721a5989bdcSJan Kara struct inode *inode; 722d46db3d5SWu Fengguang long progress; 72303ba3782SJens Axboe 724e185dda8SWu Fengguang oldest_jif = jiffies; 725d46db3d5SWu Fengguang work->older_than_this = &oldest_jif; 72603ba3782SJens Axboe 727e8dfc305SWu Fengguang spin_lock(&wb->list_lock); 72803ba3782SJens Axboe for (;;) { 72903ba3782SJens Axboe /* 730d3ddec76SWu Fengguang * Stop writeback when nr_pages has been consumed 73103ba3782SJens Axboe */ 73283ba7b07SChristoph Hellwig if (work->nr_pages <= 0) 73303ba3782SJens Axboe break; 73403ba3782SJens Axboe 73503ba3782SJens Axboe /* 736aa373cf5SJan Kara * Background writeout and kupdate-style writeback may 737aa373cf5SJan Kara * run forever. Stop them if there is other work to do 738aa373cf5SJan Kara * so that e.g. sync can proceed. They'll be restarted 739aa373cf5SJan Kara * after the other works are all done. 740aa373cf5SJan Kara */ 741aa373cf5SJan Kara if ((work->for_background || work->for_kupdate) && 742aa373cf5SJan Kara !list_empty(&wb->bdi->work_list)) 743aa373cf5SJan Kara break; 744aa373cf5SJan Kara 745aa373cf5SJan Kara /* 746d3ddec76SWu Fengguang * For background writeout, stop when we are below the 747d3ddec76SWu Fengguang * background dirty threshold 74803ba3782SJens Axboe */ 749b00949aaSWu Fengguang if (work->for_background && !over_bground_thresh(wb->bdi)) 75003ba3782SJens Axboe break; 75103ba3782SJens Axboe 7521bc36b64SJan Kara /* 7531bc36b64SJan Kara * Kupdate and background works are special and we want to 7541bc36b64SJan Kara * include all inodes that need writing. Livelock avoidance is 7551bc36b64SJan Kara * handled by these works yielding to any other work so we are 7561bc36b64SJan Kara * safe. 7571bc36b64SJan Kara */ 758ba9aa839SWu Fengguang if (work->for_kupdate) { 759ba9aa839SWu Fengguang oldest_jif = jiffies - 760ba9aa839SWu Fengguang msecs_to_jiffies(dirty_expire_interval * 10); 7611bc36b64SJan Kara } else if (work->for_background) 7621bc36b64SJan Kara oldest_jif = jiffies; 763028c2dd1SDave Chinner 764d46db3d5SWu Fengguang trace_writeback_start(wb->bdi, work); 765e8dfc305SWu Fengguang if (list_empty(&wb->b_io)) 766ad4e38ddSCurt Wohlgemuth queue_io(wb, work); 76783ba7b07SChristoph Hellwig if (work->sb) 768d46db3d5SWu Fengguang progress = writeback_sb_inodes(work->sb, wb, work); 769edadfb10SChristoph Hellwig else 770d46db3d5SWu Fengguang progress = __writeback_inodes_wb(wb, work); 771d46db3d5SWu Fengguang trace_writeback_written(wb->bdi, work); 772028c2dd1SDave Chinner 773e98be2d5SWu Fengguang wb_update_bandwidth(wb, wb_start); 77403ba3782SJens Axboe 77503ba3782SJens Axboe /* 77671fd05a8SJens Axboe * Did we write something? Try for more 777e6fb6da2SWu Fengguang * 778e6fb6da2SWu Fengguang * Dirty inodes are moved to b_io for writeback in batches. 779e6fb6da2SWu Fengguang * The completion of the current batch does not necessarily 780e6fb6da2SWu Fengguang * mean the overall work is done. So we keep looping as long 781e6fb6da2SWu Fengguang * as made some progress on cleaning pages or inodes. 78271fd05a8SJens Axboe */ 783d46db3d5SWu Fengguang if (progress) 78403ba3782SJens Axboe continue; 785a5989bdcSJan Kara /* 786e6fb6da2SWu Fengguang * No more inodes for IO, bail 787a5989bdcSJan Kara */ 788b7a2441fSWu Fengguang if (list_empty(&wb->b_more_io)) 78903ba3782SJens Axboe break; 79003ba3782SJens Axboe /* 7918010c3b6SJens Axboe * Nothing written. Wait for some inode to 7928010c3b6SJens Axboe * become available for writeback. Otherwise 7938010c3b6SJens Axboe * we'll just busyloop. 79403ba3782SJens Axboe */ 79503ba3782SJens Axboe if (!list_empty(&wb->b_more_io)) { 796d46db3d5SWu Fengguang trace_writeback_wait(wb->bdi, work); 79703ba3782SJens Axboe inode = wb_inode(wb->b_more_io.prev); 798250df6edSDave Chinner spin_lock(&inode->i_lock); 799f758eeabSChristoph Hellwig inode_wait_for_writeback(inode, wb); 800250df6edSDave Chinner spin_unlock(&inode->i_lock); 80103ba3782SJens Axboe } 80203ba3782SJens Axboe } 803e8dfc305SWu Fengguang spin_unlock(&wb->list_lock); 80403ba3782SJens Axboe 805d46db3d5SWu Fengguang return nr_pages - work->nr_pages; 80603ba3782SJens Axboe } 80703ba3782SJens Axboe 80803ba3782SJens Axboe /* 80983ba7b07SChristoph Hellwig * Return the next wb_writeback_work struct that hasn't been processed yet. 81003ba3782SJens Axboe */ 81183ba7b07SChristoph Hellwig static struct wb_writeback_work * 81208852b6dSMinchan Kim get_next_work_item(struct backing_dev_info *bdi) 81303ba3782SJens Axboe { 81483ba7b07SChristoph Hellwig struct wb_writeback_work *work = NULL; 81503ba3782SJens Axboe 8166467716aSArtem Bityutskiy spin_lock_bh(&bdi->wb_lock); 81783ba7b07SChristoph Hellwig if (!list_empty(&bdi->work_list)) { 81883ba7b07SChristoph Hellwig work = list_entry(bdi->work_list.next, 81983ba7b07SChristoph Hellwig struct wb_writeback_work, list); 82083ba7b07SChristoph Hellwig list_del_init(&work->list); 82103ba3782SJens Axboe } 8226467716aSArtem Bityutskiy spin_unlock_bh(&bdi->wb_lock); 82383ba7b07SChristoph Hellwig return work; 82403ba3782SJens Axboe } 82503ba3782SJens Axboe 826cdf01dd5SLinus Torvalds /* 827cdf01dd5SLinus Torvalds * Add in the number of potentially dirty inodes, because each inode 828cdf01dd5SLinus Torvalds * write can dirty pagecache in the underlying blockdev. 829cdf01dd5SLinus Torvalds */ 830cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void) 831cdf01dd5SLinus Torvalds { 832cdf01dd5SLinus Torvalds return global_page_state(NR_FILE_DIRTY) + 833cdf01dd5SLinus Torvalds global_page_state(NR_UNSTABLE_NFS) + 834cdf01dd5SLinus Torvalds get_nr_dirty_inodes(); 835cdf01dd5SLinus Torvalds } 836cdf01dd5SLinus Torvalds 8376585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb) 8386585027aSJan Kara { 839b00949aaSWu Fengguang if (over_bground_thresh(wb->bdi)) { 8406585027aSJan Kara 8416585027aSJan Kara struct wb_writeback_work work = { 8426585027aSJan Kara .nr_pages = LONG_MAX, 8436585027aSJan Kara .sync_mode = WB_SYNC_NONE, 8446585027aSJan Kara .for_background = 1, 8456585027aSJan Kara .range_cyclic = 1, 8460e175a18SCurt Wohlgemuth .reason = WB_REASON_BACKGROUND, 8476585027aSJan Kara }; 8486585027aSJan Kara 8496585027aSJan Kara return wb_writeback(wb, &work); 8506585027aSJan Kara } 8516585027aSJan Kara 8526585027aSJan Kara return 0; 8536585027aSJan Kara } 8546585027aSJan Kara 85503ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb) 85603ba3782SJens Axboe { 85703ba3782SJens Axboe unsigned long expired; 85803ba3782SJens Axboe long nr_pages; 85903ba3782SJens Axboe 86069b62d01SJens Axboe /* 86169b62d01SJens Axboe * When set to zero, disable periodic writeback 86269b62d01SJens Axboe */ 86369b62d01SJens Axboe if (!dirty_writeback_interval) 86469b62d01SJens Axboe return 0; 86569b62d01SJens Axboe 86603ba3782SJens Axboe expired = wb->last_old_flush + 86703ba3782SJens Axboe msecs_to_jiffies(dirty_writeback_interval * 10); 86803ba3782SJens Axboe if (time_before(jiffies, expired)) 86903ba3782SJens Axboe return 0; 87003ba3782SJens Axboe 87103ba3782SJens Axboe wb->last_old_flush = jiffies; 872cdf01dd5SLinus Torvalds nr_pages = get_nr_dirty_pages(); 87303ba3782SJens Axboe 874c4a77a6cSJens Axboe if (nr_pages) { 87583ba7b07SChristoph Hellwig struct wb_writeback_work work = { 876c4a77a6cSJens Axboe .nr_pages = nr_pages, 877c4a77a6cSJens Axboe .sync_mode = WB_SYNC_NONE, 878c4a77a6cSJens Axboe .for_kupdate = 1, 879c4a77a6cSJens Axboe .range_cyclic = 1, 8800e175a18SCurt Wohlgemuth .reason = WB_REASON_PERIODIC, 881c4a77a6cSJens Axboe }; 882c4a77a6cSJens Axboe 88383ba7b07SChristoph Hellwig return wb_writeback(wb, &work); 884c4a77a6cSJens Axboe } 88503ba3782SJens Axboe 88603ba3782SJens Axboe return 0; 88703ba3782SJens Axboe } 88803ba3782SJens Axboe 88903ba3782SJens Axboe /* 89003ba3782SJens Axboe * Retrieve work items and do the writeback they describe 89103ba3782SJens Axboe */ 89203ba3782SJens Axboe long wb_do_writeback(struct bdi_writeback *wb, int force_wait) 89303ba3782SJens Axboe { 89403ba3782SJens Axboe struct backing_dev_info *bdi = wb->bdi; 89583ba7b07SChristoph Hellwig struct wb_writeback_work *work; 896c4a77a6cSJens Axboe long wrote = 0; 89703ba3782SJens Axboe 89881d73a32SJan Kara set_bit(BDI_writeback_running, &wb->bdi->state); 89908852b6dSMinchan Kim while ((work = get_next_work_item(bdi)) != NULL) { 90003ba3782SJens Axboe /* 90103ba3782SJens Axboe * Override sync mode, in case we must wait for completion 90283ba7b07SChristoph Hellwig * because this thread is exiting now. 90303ba3782SJens Axboe */ 90403ba3782SJens Axboe if (force_wait) 90583ba7b07SChristoph Hellwig work->sync_mode = WB_SYNC_ALL; 90683ba7b07SChristoph Hellwig 907455b2864SDave Chinner trace_writeback_exec(bdi, work); 908455b2864SDave Chinner 90983ba7b07SChristoph Hellwig wrote += wb_writeback(wb, work); 91003ba3782SJens Axboe 91103ba3782SJens Axboe /* 91283ba7b07SChristoph Hellwig * Notify the caller of completion if this is a synchronous 91383ba7b07SChristoph Hellwig * work item, otherwise just free it. 91403ba3782SJens Axboe */ 91583ba7b07SChristoph Hellwig if (work->done) 91683ba7b07SChristoph Hellwig complete(work->done); 91783ba7b07SChristoph Hellwig else 91883ba7b07SChristoph Hellwig kfree(work); 91903ba3782SJens Axboe } 92003ba3782SJens Axboe 92103ba3782SJens Axboe /* 92203ba3782SJens Axboe * Check for periodic writeback, kupdated() style 92303ba3782SJens Axboe */ 92403ba3782SJens Axboe wrote += wb_check_old_data_flush(wb); 9256585027aSJan Kara wrote += wb_check_background_flush(wb); 92681d73a32SJan Kara clear_bit(BDI_writeback_running, &wb->bdi->state); 92703ba3782SJens Axboe 92803ba3782SJens Axboe return wrote; 92903ba3782SJens Axboe } 93003ba3782SJens Axboe 93103ba3782SJens Axboe /* 93203ba3782SJens Axboe * Handle writeback of dirty data for the device backed by this bdi. Also 93303ba3782SJens Axboe * wakes up periodically and does kupdated style flushing. 93403ba3782SJens Axboe */ 93508243900SChristoph Hellwig int bdi_writeback_thread(void *data) 93603ba3782SJens Axboe { 93708243900SChristoph Hellwig struct bdi_writeback *wb = data; 93808243900SChristoph Hellwig struct backing_dev_info *bdi = wb->bdi; 93903ba3782SJens Axboe long pages_written; 94003ba3782SJens Axboe 941766f9164SPeter Zijlstra current->flags |= PF_SWAPWRITE; 94208243900SChristoph Hellwig set_freezable(); 943ecd58403SArtem Bityutskiy wb->last_active = jiffies; 94403ba3782SJens Axboe 94503ba3782SJens Axboe /* 94608243900SChristoph Hellwig * Our parent may run at a different priority, just set us to normal 94703ba3782SJens Axboe */ 94808243900SChristoph Hellwig set_user_nice(current, 0); 94908243900SChristoph Hellwig 950455b2864SDave Chinner trace_writeback_thread_start(bdi); 951455b2864SDave Chinner 95203ba3782SJens Axboe while (!kthread_should_stop()) { 9536467716aSArtem Bityutskiy /* 9546467716aSArtem Bityutskiy * Remove own delayed wake-up timer, since we are already awake 9556467716aSArtem Bityutskiy * and we'll take care of the preriodic write-back. 9566467716aSArtem Bityutskiy */ 9576467716aSArtem Bityutskiy del_timer(&wb->wakeup_timer); 9586467716aSArtem Bityutskiy 95903ba3782SJens Axboe pages_written = wb_do_writeback(wb, 0); 96003ba3782SJens Axboe 961455b2864SDave Chinner trace_writeback_pages_written(pages_written); 962455b2864SDave Chinner 96303ba3782SJens Axboe if (pages_written) 964ecd58403SArtem Bityutskiy wb->last_active = jiffies; 96503ba3782SJens Axboe 966297252c8SArtem Bityutskiy set_current_state(TASK_INTERRUPTIBLE); 967b76b4014SJ. Bruce Fields if (!list_empty(&bdi->work_list) || kthread_should_stop()) { 968297252c8SArtem Bityutskiy __set_current_state(TASK_RUNNING); 969297252c8SArtem Bityutskiy continue; 97003ba3782SJens Axboe } 97103ba3782SJens Axboe 972253c34e9SArtem Bityutskiy if (wb_has_dirty_io(wb) && dirty_writeback_interval) 973fff5b85aSArtem Bityutskiy schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10)); 974253c34e9SArtem Bityutskiy else { 975253c34e9SArtem Bityutskiy /* 976253c34e9SArtem Bityutskiy * We have nothing to do, so can go sleep without any 977253c34e9SArtem Bityutskiy * timeout and save power. When a work is queued or 978253c34e9SArtem Bityutskiy * something is made dirty - we will be woken up. 979253c34e9SArtem Bityutskiy */ 98069b62d01SJens Axboe schedule(); 981f9eadbbdSJens Axboe } 98269b62d01SJens Axboe 98303ba3782SJens Axboe try_to_freeze(); 98403ba3782SJens Axboe } 98503ba3782SJens Axboe 986fff5b85aSArtem Bityutskiy /* Flush any work that raced with us exiting */ 98708243900SChristoph Hellwig if (!list_empty(&bdi->work_list)) 98808243900SChristoph Hellwig wb_do_writeback(wb, 1); 989455b2864SDave Chinner 990455b2864SDave Chinner trace_writeback_thread_stop(bdi); 99103ba3782SJens Axboe return 0; 99203ba3782SJens Axboe } 99303ba3782SJens Axboe 99408243900SChristoph Hellwig 99503ba3782SJens Axboe /* 99603ba3782SJens Axboe * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back 99703ba3782SJens Axboe * the whole world. 99803ba3782SJens Axboe */ 9990e175a18SCurt Wohlgemuth void wakeup_flusher_threads(long nr_pages, enum wb_reason reason) 100003ba3782SJens Axboe { 1001b8c2f347SChristoph Hellwig struct backing_dev_info *bdi; 1002b8c2f347SChristoph Hellwig 100383ba7b07SChristoph Hellwig if (!nr_pages) { 100483ba7b07SChristoph Hellwig nr_pages = global_page_state(NR_FILE_DIRTY) + 100503ba3782SJens Axboe global_page_state(NR_UNSTABLE_NFS); 1006b8c2f347SChristoph Hellwig } 1007b8c2f347SChristoph Hellwig 1008b8c2f347SChristoph Hellwig rcu_read_lock(); 1009b8c2f347SChristoph Hellwig list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 1010b8c2f347SChristoph Hellwig if (!bdi_has_dirty_io(bdi)) 1011b8c2f347SChristoph Hellwig continue; 10120e175a18SCurt Wohlgemuth __bdi_start_writeback(bdi, nr_pages, false, reason); 1013b8c2f347SChristoph Hellwig } 1014b8c2f347SChristoph Hellwig rcu_read_unlock(); 101503ba3782SJens Axboe } 101603ba3782SJens Axboe 101703ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode) 101803ba3782SJens Axboe { 101903ba3782SJens Axboe if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { 102003ba3782SJens Axboe struct dentry *dentry; 102103ba3782SJens Axboe const char *name = "?"; 102203ba3782SJens Axboe 102303ba3782SJens Axboe dentry = d_find_alias(inode); 102403ba3782SJens Axboe if (dentry) { 102503ba3782SJens Axboe spin_lock(&dentry->d_lock); 102603ba3782SJens Axboe name = (const char *) dentry->d_name.name; 102703ba3782SJens Axboe } 102803ba3782SJens Axboe printk(KERN_DEBUG 102903ba3782SJens Axboe "%s(%d): dirtied inode %lu (%s) on %s\n", 103003ba3782SJens Axboe current->comm, task_pid_nr(current), inode->i_ino, 103103ba3782SJens Axboe name, inode->i_sb->s_id); 103203ba3782SJens Axboe if (dentry) { 103303ba3782SJens Axboe spin_unlock(&dentry->d_lock); 103403ba3782SJens Axboe dput(dentry); 103503ba3782SJens Axboe } 103603ba3782SJens Axboe } 103703ba3782SJens Axboe } 103803ba3782SJens Axboe 103903ba3782SJens Axboe /** 104003ba3782SJens Axboe * __mark_inode_dirty - internal function 104103ba3782SJens Axboe * @inode: inode to mark 104203ba3782SJens Axboe * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) 104303ba3782SJens Axboe * Mark an inode as dirty. Callers should use mark_inode_dirty or 104403ba3782SJens Axboe * mark_inode_dirty_sync. 104503ba3782SJens Axboe * 104603ba3782SJens Axboe * Put the inode on the super block's dirty list. 104703ba3782SJens Axboe * 104803ba3782SJens Axboe * CAREFUL! We mark it dirty unconditionally, but move it onto the 104903ba3782SJens Axboe * dirty list only if it is hashed or if it refers to a blockdev. 105003ba3782SJens Axboe * If it was not hashed, it will never be added to the dirty list 105103ba3782SJens Axboe * even if it is later hashed, as it will have been marked dirty already. 105203ba3782SJens Axboe * 105303ba3782SJens Axboe * In short, make sure you hash any inodes _before_ you start marking 105403ba3782SJens Axboe * them dirty. 105503ba3782SJens Axboe * 105603ba3782SJens Axboe * Note that for blockdevs, inode->dirtied_when represents the dirtying time of 105703ba3782SJens Axboe * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of 105803ba3782SJens Axboe * the kernel-internal blockdev inode represents the dirtying time of the 105903ba3782SJens Axboe * blockdev's pages. This is why for I_DIRTY_PAGES we always use 106003ba3782SJens Axboe * page->mapping->host, so the page-dirtying time is recorded in the internal 106103ba3782SJens Axboe * blockdev inode. 106203ba3782SJens Axboe */ 106303ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags) 106403ba3782SJens Axboe { 106503ba3782SJens Axboe struct super_block *sb = inode->i_sb; 1066253c34e9SArtem Bityutskiy struct backing_dev_info *bdi = NULL; 106703ba3782SJens Axboe 106803ba3782SJens Axboe /* 106903ba3782SJens Axboe * Don't do this for I_DIRTY_PAGES - that doesn't actually 107003ba3782SJens Axboe * dirty the inode itself 107103ba3782SJens Axboe */ 107203ba3782SJens Axboe if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { 107303ba3782SJens Axboe if (sb->s_op->dirty_inode) 1074aa385729SChristoph Hellwig sb->s_op->dirty_inode(inode, flags); 107503ba3782SJens Axboe } 107603ba3782SJens Axboe 107703ba3782SJens Axboe /* 107803ba3782SJens Axboe * make sure that changes are seen by all cpus before we test i_state 107903ba3782SJens Axboe * -- mikulas 108003ba3782SJens Axboe */ 108103ba3782SJens Axboe smp_mb(); 108203ba3782SJens Axboe 108303ba3782SJens Axboe /* avoid the locking if we can */ 108403ba3782SJens Axboe if ((inode->i_state & flags) == flags) 108503ba3782SJens Axboe return; 108603ba3782SJens Axboe 108703ba3782SJens Axboe if (unlikely(block_dump)) 108803ba3782SJens Axboe block_dump___mark_inode_dirty(inode); 108903ba3782SJens Axboe 1090250df6edSDave Chinner spin_lock(&inode->i_lock); 109103ba3782SJens Axboe if ((inode->i_state & flags) != flags) { 109203ba3782SJens Axboe const int was_dirty = inode->i_state & I_DIRTY; 109303ba3782SJens Axboe 109403ba3782SJens Axboe inode->i_state |= flags; 109503ba3782SJens Axboe 109603ba3782SJens Axboe /* 109703ba3782SJens Axboe * If the inode is being synced, just update its dirty state. 109803ba3782SJens Axboe * The unlocker will place the inode on the appropriate 109903ba3782SJens Axboe * superblock list, based upon its state. 110003ba3782SJens Axboe */ 110103ba3782SJens Axboe if (inode->i_state & I_SYNC) 1102250df6edSDave Chinner goto out_unlock_inode; 110303ba3782SJens Axboe 110403ba3782SJens Axboe /* 110503ba3782SJens Axboe * Only add valid (hashed) inodes to the superblock's 110603ba3782SJens Axboe * dirty list. Add blockdev inodes as well. 110703ba3782SJens Axboe */ 110803ba3782SJens Axboe if (!S_ISBLK(inode->i_mode)) { 11091d3382cbSAl Viro if (inode_unhashed(inode)) 1110250df6edSDave Chinner goto out_unlock_inode; 111103ba3782SJens Axboe } 1112a4ffdde6SAl Viro if (inode->i_state & I_FREEING) 1113250df6edSDave Chinner goto out_unlock_inode; 111403ba3782SJens Axboe 111503ba3782SJens Axboe /* 111603ba3782SJens Axboe * If the inode was already on b_dirty/b_io/b_more_io, don't 111703ba3782SJens Axboe * reposition it (that would break b_dirty time-ordering). 111803ba3782SJens Axboe */ 111903ba3782SJens Axboe if (!was_dirty) { 1120a66979abSDave Chinner bool wakeup_bdi = false; 1121253c34e9SArtem Bityutskiy bdi = inode_to_bdi(inode); 1122500b067cSJens Axboe 1123253c34e9SArtem Bityutskiy if (bdi_cap_writeback_dirty(bdi)) { 1124253c34e9SArtem Bityutskiy WARN(!test_bit(BDI_registered, &bdi->state), 1125253c34e9SArtem Bityutskiy "bdi-%s not registered\n", bdi->name); 1126253c34e9SArtem Bityutskiy 1127253c34e9SArtem Bityutskiy /* 1128253c34e9SArtem Bityutskiy * If this is the first dirty inode for this 1129253c34e9SArtem Bityutskiy * bdi, we have to wake-up the corresponding 1130253c34e9SArtem Bityutskiy * bdi thread to make sure background 1131253c34e9SArtem Bityutskiy * write-back happens later. 1132253c34e9SArtem Bityutskiy */ 1133253c34e9SArtem Bityutskiy if (!wb_has_dirty_io(&bdi->wb)) 1134253c34e9SArtem Bityutskiy wakeup_bdi = true; 1135500b067cSJens Axboe } 113603ba3782SJens Axboe 1137a66979abSDave Chinner spin_unlock(&inode->i_lock); 1138f758eeabSChristoph Hellwig spin_lock(&bdi->wb.list_lock); 113903ba3782SJens Axboe inode->dirtied_when = jiffies; 11407ccf19a8SNick Piggin list_move(&inode->i_wb_list, &bdi->wb.b_dirty); 1141f758eeabSChristoph Hellwig spin_unlock(&bdi->wb.list_lock); 1142253c34e9SArtem Bityutskiy 1143253c34e9SArtem Bityutskiy if (wakeup_bdi) 11446467716aSArtem Bityutskiy bdi_wakeup_thread_delayed(bdi); 1145a66979abSDave Chinner return; 1146a66979abSDave Chinner } 1147a66979abSDave Chinner } 1148a66979abSDave Chinner out_unlock_inode: 1149a66979abSDave Chinner spin_unlock(&inode->i_lock); 1150a66979abSDave Chinner 115103ba3782SJens Axboe } 115203ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty); 115303ba3782SJens Axboe 115466f3b8e2SJens Axboe /* 115566f3b8e2SJens Axboe * Write out a superblock's list of dirty inodes. A wait will be performed 115666f3b8e2SJens Axboe * upon no inodes, all inodes or the final one, depending upon sync_mode. 115766f3b8e2SJens Axboe * 115866f3b8e2SJens Axboe * If older_than_this is non-NULL, then only write out inodes which 115966f3b8e2SJens Axboe * had their first dirtying at a time earlier than *older_than_this. 116066f3b8e2SJens Axboe * 116166f3b8e2SJens Axboe * If `bdi' is non-zero then we're being asked to writeback a specific queue. 116266f3b8e2SJens Axboe * This function assumes that the blockdev superblock's inodes are backed by 116366f3b8e2SJens Axboe * a variety of queues, so all inodes are searched. For other superblocks, 116466f3b8e2SJens Axboe * assume that all inodes are backed by the same queue. 116566f3b8e2SJens Axboe * 116666f3b8e2SJens Axboe * The inodes to be written are parked on bdi->b_io. They are moved back onto 116766f3b8e2SJens Axboe * bdi->b_dirty as they are selected for writing. This way, none can be missed 116866f3b8e2SJens Axboe * on the writer throttling path, and we get decent balancing between many 116966f3b8e2SJens Axboe * throttled threads: we don't want them all piling up on inode_sync_wait. 117066f3b8e2SJens Axboe */ 1171b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb) 117266f3b8e2SJens Axboe { 117338f21977SNick Piggin struct inode *inode, *old_inode = NULL; 117438f21977SNick Piggin 117503ba3782SJens Axboe /* 117603ba3782SJens Axboe * We need to be protected against the filesystem going from 117703ba3782SJens Axboe * r/o to r/w or vice versa. 117803ba3782SJens Axboe */ 1179b6e51316SJens Axboe WARN_ON(!rwsem_is_locked(&sb->s_umount)); 118003ba3782SJens Axboe 118155fa6091SDave Chinner spin_lock(&inode_sb_list_lock); 118266f3b8e2SJens Axboe 118338f21977SNick Piggin /* 118438f21977SNick Piggin * Data integrity sync. Must wait for all pages under writeback, 118538f21977SNick Piggin * because there may have been pages dirtied before our sync 118638f21977SNick Piggin * call, but which had writeout started before we write it out. 118738f21977SNick Piggin * In which case, the inode may not be on the dirty list, but 118838f21977SNick Piggin * we still have to wait for that writeout. 118938f21977SNick Piggin */ 1190b6e51316SJens Axboe list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 1191250df6edSDave Chinner struct address_space *mapping = inode->i_mapping; 119238f21977SNick Piggin 1193250df6edSDave Chinner spin_lock(&inode->i_lock); 1194250df6edSDave Chinner if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) || 1195250df6edSDave Chinner (mapping->nrpages == 0)) { 1196250df6edSDave Chinner spin_unlock(&inode->i_lock); 119738f21977SNick Piggin continue; 1198250df6edSDave Chinner } 119938f21977SNick Piggin __iget(inode); 1200250df6edSDave Chinner spin_unlock(&inode->i_lock); 120155fa6091SDave Chinner spin_unlock(&inode_sb_list_lock); 120255fa6091SDave Chinner 120338f21977SNick Piggin /* 120455fa6091SDave Chinner * We hold a reference to 'inode' so it couldn't have been 120555fa6091SDave Chinner * removed from s_inodes list while we dropped the 120655fa6091SDave Chinner * inode_sb_list_lock. We cannot iput the inode now as we can 120755fa6091SDave Chinner * be holding the last reference and we cannot iput it under 120855fa6091SDave Chinner * inode_sb_list_lock. So we keep the reference and iput it 120955fa6091SDave Chinner * later. 121038f21977SNick Piggin */ 121138f21977SNick Piggin iput(old_inode); 121238f21977SNick Piggin old_inode = inode; 121338f21977SNick Piggin 121438f21977SNick Piggin filemap_fdatawait(mapping); 121538f21977SNick Piggin 121638f21977SNick Piggin cond_resched(); 121738f21977SNick Piggin 121855fa6091SDave Chinner spin_lock(&inode_sb_list_lock); 121938f21977SNick Piggin } 122055fa6091SDave Chinner spin_unlock(&inode_sb_list_lock); 122138f21977SNick Piggin iput(old_inode); 122266f3b8e2SJens Axboe } 12231da177e4SLinus Torvalds 1224d8a8559cSJens Axboe /** 12253259f8beSChris Mason * writeback_inodes_sb_nr - writeback dirty inodes from given super_block 1226d8a8559cSJens Axboe * @sb: the superblock 12273259f8beSChris Mason * @nr: the number of pages to write 1228786228abSMarcos Paulo de Souza * @reason: reason why some writeback work initiated 12291da177e4SLinus Torvalds * 1230d8a8559cSJens Axboe * Start writeback on some inodes on this super_block. No guarantees are made 1231d8a8559cSJens Axboe * on how many (if any) will be written, and this function does not wait 12323259f8beSChris Mason * for IO completion of submitted IO. 12331da177e4SLinus Torvalds */ 12340e175a18SCurt Wohlgemuth void writeback_inodes_sb_nr(struct super_block *sb, 12350e175a18SCurt Wohlgemuth unsigned long nr, 12360e175a18SCurt Wohlgemuth enum wb_reason reason) 12371da177e4SLinus Torvalds { 123883ba7b07SChristoph Hellwig DECLARE_COMPLETION_ONSTACK(done); 123983ba7b07SChristoph Hellwig struct wb_writeback_work work = { 12403c4d7165SChristoph Hellwig .sb = sb, 12413c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_NONE, 12426e6938b6SWu Fengguang .tagged_writepages = 1, 124383ba7b07SChristoph Hellwig .done = &done, 12443259f8beSChris Mason .nr_pages = nr, 12450e175a18SCurt Wohlgemuth .reason = reason, 12463c4d7165SChristoph Hellwig }; 12470e3c9a22SJens Axboe 1248cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 124983ba7b07SChristoph Hellwig bdi_queue_work(sb->s_bdi, &work); 125083ba7b07SChristoph Hellwig wait_for_completion(&done); 12511da177e4SLinus Torvalds } 12523259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr); 12533259f8beSChris Mason 12543259f8beSChris Mason /** 12553259f8beSChris Mason * writeback_inodes_sb - writeback dirty inodes from given super_block 12563259f8beSChris Mason * @sb: the superblock 1257786228abSMarcos Paulo de Souza * @reason: reason why some writeback work was initiated 12583259f8beSChris Mason * 12593259f8beSChris Mason * Start writeback on some inodes on this super_block. No guarantees are made 12603259f8beSChris Mason * on how many (if any) will be written, and this function does not wait 12613259f8beSChris Mason * for IO completion of submitted IO. 12623259f8beSChris Mason */ 12630e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 12643259f8beSChris Mason { 12650e175a18SCurt Wohlgemuth return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); 12663259f8beSChris Mason } 1267d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb); 1268d8a8559cSJens Axboe 1269d8a8559cSJens Axboe /** 127017bd55d0SEric Sandeen * writeback_inodes_sb_if_idle - start writeback if none underway 127117bd55d0SEric Sandeen * @sb: the superblock 1272786228abSMarcos Paulo de Souza * @reason: reason why some writeback work was initiated 127317bd55d0SEric Sandeen * 127417bd55d0SEric Sandeen * Invoke writeback_inodes_sb if no writeback is currently underway. 127517bd55d0SEric Sandeen * Returns 1 if writeback was started, 0 if not. 127617bd55d0SEric Sandeen */ 12770e175a18SCurt Wohlgemuth int writeback_inodes_sb_if_idle(struct super_block *sb, enum wb_reason reason) 127817bd55d0SEric Sandeen { 127917bd55d0SEric Sandeen if (!writeback_in_progress(sb->s_bdi)) { 1280cf37e972SChristoph Hellwig down_read(&sb->s_umount); 12810e175a18SCurt Wohlgemuth writeback_inodes_sb(sb, reason); 1282cf37e972SChristoph Hellwig up_read(&sb->s_umount); 128317bd55d0SEric Sandeen return 1; 128417bd55d0SEric Sandeen } else 128517bd55d0SEric Sandeen return 0; 128617bd55d0SEric Sandeen } 128717bd55d0SEric Sandeen EXPORT_SYMBOL(writeback_inodes_sb_if_idle); 128817bd55d0SEric Sandeen 128917bd55d0SEric Sandeen /** 12903259f8beSChris Mason * writeback_inodes_sb_if_idle - start writeback if none underway 12913259f8beSChris Mason * @sb: the superblock 12923259f8beSChris Mason * @nr: the number of pages to write 1293786228abSMarcos Paulo de Souza * @reason: reason why some writeback work was initiated 12943259f8beSChris Mason * 12953259f8beSChris Mason * Invoke writeback_inodes_sb if no writeback is currently underway. 12963259f8beSChris Mason * Returns 1 if writeback was started, 0 if not. 12973259f8beSChris Mason */ 12983259f8beSChris Mason int writeback_inodes_sb_nr_if_idle(struct super_block *sb, 12990e175a18SCurt Wohlgemuth unsigned long nr, 13000e175a18SCurt Wohlgemuth enum wb_reason reason) 13013259f8beSChris Mason { 13023259f8beSChris Mason if (!writeback_in_progress(sb->s_bdi)) { 13033259f8beSChris Mason down_read(&sb->s_umount); 13040e175a18SCurt Wohlgemuth writeback_inodes_sb_nr(sb, nr, reason); 13053259f8beSChris Mason up_read(&sb->s_umount); 13063259f8beSChris Mason return 1; 13073259f8beSChris Mason } else 13083259f8beSChris Mason return 0; 13093259f8beSChris Mason } 13103259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr_if_idle); 13113259f8beSChris Mason 13123259f8beSChris Mason /** 1313d8a8559cSJens Axboe * sync_inodes_sb - sync sb inode pages 1314d8a8559cSJens Axboe * @sb: the superblock 1315d8a8559cSJens Axboe * 1316d8a8559cSJens Axboe * This function writes and waits on any dirty inode belonging to this 1317cb9ef8d5SStefan Hajnoczi * super_block. 1318d8a8559cSJens Axboe */ 1319b6e51316SJens Axboe void sync_inodes_sb(struct super_block *sb) 1320d8a8559cSJens Axboe { 132183ba7b07SChristoph Hellwig DECLARE_COMPLETION_ONSTACK(done); 132283ba7b07SChristoph Hellwig struct wb_writeback_work work = { 13233c4d7165SChristoph Hellwig .sb = sb, 13243c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_ALL, 13253c4d7165SChristoph Hellwig .nr_pages = LONG_MAX, 13263c4d7165SChristoph Hellwig .range_cyclic = 0, 132783ba7b07SChristoph Hellwig .done = &done, 13280e175a18SCurt Wohlgemuth .reason = WB_REASON_SYNC, 13293c4d7165SChristoph Hellwig }; 13303c4d7165SChristoph Hellwig 1331cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 1332cf37e972SChristoph Hellwig 133383ba7b07SChristoph Hellwig bdi_queue_work(sb->s_bdi, &work); 133483ba7b07SChristoph Hellwig wait_for_completion(&done); 133583ba7b07SChristoph Hellwig 1336b6e51316SJens Axboe wait_sb_inodes(sb); 1337d8a8559cSJens Axboe } 1338d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb); 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds /** 13411da177e4SLinus Torvalds * write_inode_now - write an inode to disk 13421da177e4SLinus Torvalds * @inode: inode to write to disk 13431da177e4SLinus Torvalds * @sync: whether the write should be synchronous or not 13441da177e4SLinus Torvalds * 13457f04c26dSAndrea Arcangeli * This function commits an inode to disk immediately if it is dirty. This is 13467f04c26dSAndrea Arcangeli * primarily needed by knfsd. 13477f04c26dSAndrea Arcangeli * 13487f04c26dSAndrea Arcangeli * The caller must either have a ref on the inode or must have set I_WILL_FREE. 13491da177e4SLinus Torvalds */ 13501da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync) 13511da177e4SLinus Torvalds { 1352f758eeabSChristoph Hellwig struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 13531da177e4SLinus Torvalds int ret; 13541da177e4SLinus Torvalds struct writeback_control wbc = { 13551da177e4SLinus Torvalds .nr_to_write = LONG_MAX, 135618914b18SMike Galbraith .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 1357111ebb6eSOGAWA Hirofumi .range_start = 0, 1358111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 13591da177e4SLinus Torvalds }; 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds if (!mapping_cap_writeback_dirty(inode->i_mapping)) 136249364ce2SAndrew Morton wbc.nr_to_write = 0; 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds might_sleep(); 1365f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 13660f1b1fd8SDave Chinner spin_lock(&inode->i_lock); 1367f758eeabSChristoph Hellwig ret = writeback_single_inode(inode, wb, &wbc); 13680f1b1fd8SDave Chinner spin_unlock(&inode->i_lock); 1369f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 13701da177e4SLinus Torvalds if (sync) 13711c0eeaf5SJoern Engel inode_sync_wait(inode); 13721da177e4SLinus Torvalds return ret; 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now); 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds /** 13771da177e4SLinus Torvalds * sync_inode - write an inode and its pages to disk. 13781da177e4SLinus Torvalds * @inode: the inode to sync 13791da177e4SLinus Torvalds * @wbc: controls the writeback mode 13801da177e4SLinus Torvalds * 13811da177e4SLinus Torvalds * sync_inode() will write an inode and its pages to disk. It will also 13821da177e4SLinus Torvalds * correctly update the inode on its superblock's dirty inode lists and will 13831da177e4SLinus Torvalds * update inode->i_state. 13841da177e4SLinus Torvalds * 13851da177e4SLinus Torvalds * The caller must have a ref on the inode. 13861da177e4SLinus Torvalds */ 13871da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc) 13881da177e4SLinus Torvalds { 1389f758eeabSChristoph Hellwig struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 13901da177e4SLinus Torvalds int ret; 13911da177e4SLinus Torvalds 1392f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 13930f1b1fd8SDave Chinner spin_lock(&inode->i_lock); 1394f758eeabSChristoph Hellwig ret = writeback_single_inode(inode, wb, wbc); 13950f1b1fd8SDave Chinner spin_unlock(&inode->i_lock); 1396f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 13971da177e4SLinus Torvalds return ret; 13981da177e4SLinus Torvalds } 13991da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode); 1400c3765016SChristoph Hellwig 1401c3765016SChristoph Hellwig /** 1402c691b9d9SAndrew Morton * sync_inode_metadata - write an inode to disk 1403c3765016SChristoph Hellwig * @inode: the inode to sync 1404c3765016SChristoph Hellwig * @wait: wait for I/O to complete. 1405c3765016SChristoph Hellwig * 1406c691b9d9SAndrew Morton * Write an inode to disk and adjust its dirty state after completion. 1407c3765016SChristoph Hellwig * 1408c3765016SChristoph Hellwig * Note: only writes the actual inode, no associated data or other metadata. 1409c3765016SChristoph Hellwig */ 1410c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait) 1411c3765016SChristoph Hellwig { 1412c3765016SChristoph Hellwig struct writeback_control wbc = { 1413c3765016SChristoph Hellwig .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, 1414c3765016SChristoph Hellwig .nr_to_write = 0, /* metadata-only */ 1415c3765016SChristoph Hellwig }; 1416c3765016SChristoph Hellwig 1417c3765016SChristoph Hellwig return sync_inode(inode, &wbc); 1418c3765016SChristoph Hellwig } 1419c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata); 1420