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> 251da177e4SLinus Torvalds #include <linux/writeback.h> 261da177e4SLinus Torvalds #include <linux/blkdev.h> 271da177e4SLinus Torvalds #include <linux/backing-dev.h> 28455b2864SDave Chinner #include <linux/tracepoint.h> 29719ea2fbSAl Viro #include <linux/device.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; 430dc83bd3SJan Kara 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; 497747bd4bSDave Chinner unsigned int for_sync:1; /* sync(2) WB_SYNC_ALL writeback */ 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 56a2f48706STheodore Ts'o /* 57a2f48706STheodore Ts'o * If an inode is constantly having its pages dirtied, but then the 58a2f48706STheodore Ts'o * updates stop dirtytime_expire_interval seconds in the past, it's 59a2f48706STheodore Ts'o * possible for the worst case time between when an inode has its 60a2f48706STheodore Ts'o * timestamps updated and when they finally get written out to be two 61a2f48706STheodore Ts'o * dirtytime_expire_intervals. We set the default to 12 hours (in 62a2f48706STheodore Ts'o * seconds), which means most of the time inodes will have their 63a2f48706STheodore Ts'o * timestamps written to disk after 12 hours, but in the worst case a 64a2f48706STheodore Ts'o * few inodes might not their timestamps updated for 24 hours. 65a2f48706STheodore Ts'o */ 66a2f48706STheodore Ts'o unsigned int dirtytime_expire_interval = 12 * 60 * 60; 67a2f48706STheodore Ts'o 68f11b00f3SAdrian Bunk /** 69f11b00f3SAdrian Bunk * writeback_in_progress - determine whether there is writeback in progress 70f11b00f3SAdrian Bunk * @bdi: the device's backing_dev_info structure. 71f11b00f3SAdrian Bunk * 7203ba3782SJens Axboe * Determine whether there is writeback waiting to be handled against a 7303ba3782SJens Axboe * backing device. 74f11b00f3SAdrian Bunk */ 75f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi) 76f11b00f3SAdrian Bunk { 774452226eSTejun Heo return test_bit(WB_writeback_running, &bdi->wb.state); 78f11b00f3SAdrian Bunk } 7900d4e736STheodore Ts'o EXPORT_SYMBOL(writeback_in_progress); 80f11b00f3SAdrian Bunk 817ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head) 827ccf19a8SNick Piggin { 837ccf19a8SNick Piggin return list_entry(head, struct inode, i_wb_list); 847ccf19a8SNick Piggin } 857ccf19a8SNick Piggin 8615eb77a0SWu Fengguang /* 8715eb77a0SWu Fengguang * Include the creation of the trace points after defining the 8815eb77a0SWu Fengguang * wb_writeback_work structure and inline functions so that the definition 8915eb77a0SWu Fengguang * remains local to this file. 9015eb77a0SWu Fengguang */ 9115eb77a0SWu Fengguang #define CREATE_TRACE_POINTS 9215eb77a0SWu Fengguang #include <trace/events/writeback.h> 9315eb77a0SWu Fengguang 94774016b2SSteven Whitehouse EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage); 95774016b2SSteven Whitehouse 96d6c10f1fSTejun Heo static bool wb_io_lists_populated(struct bdi_writeback *wb) 97d6c10f1fSTejun Heo { 98d6c10f1fSTejun Heo if (wb_has_dirty_io(wb)) { 99d6c10f1fSTejun Heo return false; 100d6c10f1fSTejun Heo } else { 101d6c10f1fSTejun Heo set_bit(WB_has_dirty_io, &wb->state); 10295a46c65STejun Heo WARN_ON_ONCE(!wb->avg_write_bandwidth); 103766a9d6eSTejun Heo atomic_long_add(wb->avg_write_bandwidth, 104766a9d6eSTejun Heo &wb->bdi->tot_write_bandwidth); 105d6c10f1fSTejun Heo return true; 106d6c10f1fSTejun Heo } 107d6c10f1fSTejun Heo } 108d6c10f1fSTejun Heo 109d6c10f1fSTejun Heo static void wb_io_lists_depopulated(struct bdi_writeback *wb) 110d6c10f1fSTejun Heo { 111d6c10f1fSTejun Heo if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) && 112766a9d6eSTejun Heo list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) { 113d6c10f1fSTejun Heo clear_bit(WB_has_dirty_io, &wb->state); 11495a46c65STejun Heo WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth, 11595a46c65STejun Heo &wb->bdi->tot_write_bandwidth) < 0); 116766a9d6eSTejun Heo } 117d6c10f1fSTejun Heo } 118d6c10f1fSTejun Heo 119d6c10f1fSTejun Heo /** 120d6c10f1fSTejun Heo * inode_wb_list_move_locked - move an inode onto a bdi_writeback IO list 121d6c10f1fSTejun Heo * @inode: inode to be moved 122d6c10f1fSTejun Heo * @wb: target bdi_writeback 123d6c10f1fSTejun Heo * @head: one of @wb->b_{dirty|io|more_io} 124d6c10f1fSTejun Heo * 125d6c10f1fSTejun Heo * Move @inode->i_wb_list to @list of @wb and set %WB_has_dirty_io. 126d6c10f1fSTejun Heo * Returns %true if @inode is the first occupant of the !dirty_time IO 127d6c10f1fSTejun Heo * lists; otherwise, %false. 128d6c10f1fSTejun Heo */ 129d6c10f1fSTejun Heo static bool inode_wb_list_move_locked(struct inode *inode, 130d6c10f1fSTejun Heo struct bdi_writeback *wb, 131d6c10f1fSTejun Heo struct list_head *head) 132d6c10f1fSTejun Heo { 133d6c10f1fSTejun Heo assert_spin_locked(&wb->list_lock); 134d6c10f1fSTejun Heo 135d6c10f1fSTejun Heo list_move(&inode->i_wb_list, head); 136d6c10f1fSTejun Heo 137d6c10f1fSTejun Heo /* dirty_time doesn't count as dirty_io until expiration */ 138d6c10f1fSTejun Heo if (head != &wb->b_dirty_time) 139d6c10f1fSTejun Heo return wb_io_lists_populated(wb); 140d6c10f1fSTejun Heo 141d6c10f1fSTejun Heo wb_io_lists_depopulated(wb); 142d6c10f1fSTejun Heo return false; 143d6c10f1fSTejun Heo } 144d6c10f1fSTejun Heo 145d6c10f1fSTejun Heo /** 146d6c10f1fSTejun Heo * inode_wb_list_del_locked - remove an inode from its bdi_writeback IO list 147d6c10f1fSTejun Heo * @inode: inode to be removed 148d6c10f1fSTejun Heo * @wb: bdi_writeback @inode is being removed from 149d6c10f1fSTejun Heo * 150d6c10f1fSTejun Heo * Remove @inode which may be on one of @wb->b_{dirty|io|more_io} lists and 151d6c10f1fSTejun Heo * clear %WB_has_dirty_io if all are empty afterwards. 152d6c10f1fSTejun Heo */ 153d6c10f1fSTejun Heo static void inode_wb_list_del_locked(struct inode *inode, 154d6c10f1fSTejun Heo struct bdi_writeback *wb) 155d6c10f1fSTejun Heo { 156d6c10f1fSTejun Heo assert_spin_locked(&wb->list_lock); 157d6c10f1fSTejun Heo 158d6c10f1fSTejun Heo list_del_init(&inode->i_wb_list); 159d6c10f1fSTejun Heo wb_io_lists_depopulated(wb); 160d6c10f1fSTejun Heo } 161d6c10f1fSTejun Heo 162f0054bb1STejun Heo static void wb_wakeup(struct bdi_writeback *wb) 1635acda9d1SJan Kara { 164f0054bb1STejun Heo spin_lock_bh(&wb->work_lock); 165f0054bb1STejun Heo if (test_bit(WB_registered, &wb->state)) 166f0054bb1STejun Heo mod_delayed_work(bdi_wq, &wb->dwork, 0); 167f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 1685acda9d1SJan Kara } 1695acda9d1SJan Kara 170f0054bb1STejun Heo static void wb_queue_work(struct bdi_writeback *wb, 1716585027aSJan Kara struct wb_writeback_work *work) 1726585027aSJan Kara { 173f0054bb1STejun Heo trace_writeback_queue(wb->bdi, work); 1746585027aSJan Kara 175f0054bb1STejun Heo spin_lock_bh(&wb->work_lock); 176f0054bb1STejun Heo if (!test_bit(WB_registered, &wb->state)) { 1775acda9d1SJan Kara if (work->done) 1785acda9d1SJan Kara complete(work->done); 1795acda9d1SJan Kara goto out_unlock; 1805acda9d1SJan Kara } 181f0054bb1STejun Heo list_add_tail(&work->list, &wb->work_list); 182f0054bb1STejun Heo mod_delayed_work(bdi_wq, &wb->dwork, 0); 1835acda9d1SJan Kara out_unlock: 184f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 18503ba3782SJens Axboe } 1861da177e4SLinus Torvalds 187703c2708STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 188703c2708STejun Heo 189703c2708STejun Heo /** 190703c2708STejun Heo * inode_congested - test whether an inode is congested 191703c2708STejun Heo * @inode: inode to test for congestion 192703c2708STejun Heo * @cong_bits: mask of WB_[a]sync_congested bits to test 193703c2708STejun Heo * 194703c2708STejun Heo * Tests whether @inode is congested. @cong_bits is the mask of congestion 195703c2708STejun Heo * bits to test and the return value is the mask of set bits. 196703c2708STejun Heo * 197703c2708STejun Heo * If cgroup writeback is enabled for @inode, the congestion state is 198703c2708STejun Heo * determined by whether the cgwb (cgroup bdi_writeback) for the blkcg 199703c2708STejun Heo * associated with @inode is congested; otherwise, the root wb's congestion 200703c2708STejun Heo * state is used. 201703c2708STejun Heo */ 202703c2708STejun Heo int inode_congested(struct inode *inode, int cong_bits) 203703c2708STejun Heo { 204703c2708STejun Heo if (inode) { 205703c2708STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 206703c2708STejun Heo if (wb) 207703c2708STejun Heo return wb_congested(wb, cong_bits); 208703c2708STejun Heo } 209703c2708STejun Heo 210703c2708STejun Heo return wb_congested(&inode_to_bdi(inode)->wb, cong_bits); 211703c2708STejun Heo } 212703c2708STejun Heo EXPORT_SYMBOL_GPL(inode_congested); 213703c2708STejun Heo 214703c2708STejun Heo #endif /* CONFIG_CGROUP_WRITEBACK */ 215703c2708STejun Heo 216c00ddad3STejun Heo void wb_start_writeback(struct bdi_writeback *wb, long nr_pages, 217c00ddad3STejun Heo bool range_cyclic, enum wb_reason reason) 218b6e51316SJens Axboe { 219c00ddad3STejun Heo struct wb_writeback_work *work; 220c00ddad3STejun Heo 221c00ddad3STejun Heo if (!wb_has_dirty_io(wb)) 222c00ddad3STejun Heo return; 223c00ddad3STejun Heo 224c00ddad3STejun Heo /* 225c00ddad3STejun Heo * This is WB_SYNC_NONE writeback, so if allocation fails just 226c00ddad3STejun Heo * wakeup the thread for old dirty data writeback 227c00ddad3STejun Heo */ 228c00ddad3STejun Heo work = kzalloc(sizeof(*work), GFP_ATOMIC); 229c00ddad3STejun Heo if (!work) { 230c00ddad3STejun Heo trace_writeback_nowork(wb->bdi); 231c00ddad3STejun Heo wb_wakeup(wb); 232c00ddad3STejun Heo return; 233c00ddad3STejun Heo } 234c00ddad3STejun Heo 235c00ddad3STejun Heo work->sync_mode = WB_SYNC_NONE; 236c00ddad3STejun Heo work->nr_pages = nr_pages; 237c00ddad3STejun Heo work->range_cyclic = range_cyclic; 238c00ddad3STejun Heo work->reason = reason; 239c00ddad3STejun Heo 240c00ddad3STejun Heo wb_queue_work(wb, work); 241d3ddec76SWu Fengguang } 242d3ddec76SWu Fengguang 243c5444198SChristoph Hellwig /** 244c5444198SChristoph Hellwig * bdi_start_background_writeback - start background writeback 245c5444198SChristoph Hellwig * @bdi: the backing device to write from 246c5444198SChristoph Hellwig * 247c5444198SChristoph Hellwig * Description: 2486585027aSJan Kara * This makes sure WB_SYNC_NONE background writeback happens. When 2496585027aSJan Kara * this function returns, it is only guaranteed that for given BDI 2506585027aSJan Kara * some IO is happening if we are over background dirty threshold. 2516585027aSJan Kara * Caller need not hold sb s_umount semaphore. 252c5444198SChristoph Hellwig */ 253c5444198SChristoph Hellwig void bdi_start_background_writeback(struct backing_dev_info *bdi) 254c5444198SChristoph Hellwig { 2556585027aSJan Kara /* 2566585027aSJan Kara * We just wake up the flusher thread. It will perform background 2576585027aSJan Kara * writeback as soon as there is no other work to do. 2586585027aSJan Kara */ 25971927e84SWu Fengguang trace_writeback_wake_background(bdi); 260f0054bb1STejun Heo wb_wakeup(&bdi->wb); 2611da177e4SLinus Torvalds } 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds /* 264a66979abSDave Chinner * Remove the inode from the writeback list it is on. 265a66979abSDave Chinner */ 266a66979abSDave Chinner void inode_wb_list_del(struct inode *inode) 267a66979abSDave Chinner { 26852ebea74STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 269a66979abSDave Chinner 27052ebea74STejun Heo spin_lock(&wb->list_lock); 271d6c10f1fSTejun Heo inode_wb_list_del_locked(inode, wb); 27252ebea74STejun Heo spin_unlock(&wb->list_lock); 273f758eeabSChristoph Hellwig } 274a66979abSDave Chinner 275a66979abSDave Chinner /* 2766610a0bcSAndrew Morton * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 2776610a0bcSAndrew Morton * furthest end of its superblock's dirty-inode list. 2786610a0bcSAndrew Morton * 2796610a0bcSAndrew Morton * Before stamping the inode's ->dirtied_when, we check to see whether it is 28066f3b8e2SJens Axboe * already the most-recently-dirtied inode on the b_dirty list. If that is 2816610a0bcSAndrew Morton * the case then the inode must have been redirtied while it was being written 2826610a0bcSAndrew Morton * out and we don't reset its dirtied_when. 2836610a0bcSAndrew Morton */ 284f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb) 2856610a0bcSAndrew Morton { 28603ba3782SJens Axboe if (!list_empty(&wb->b_dirty)) { 28766f3b8e2SJens Axboe struct inode *tail; 2886610a0bcSAndrew Morton 2897ccf19a8SNick Piggin tail = wb_inode(wb->b_dirty.next); 29066f3b8e2SJens Axboe if (time_before(inode->dirtied_when, tail->dirtied_when)) 2916610a0bcSAndrew Morton inode->dirtied_when = jiffies; 2926610a0bcSAndrew Morton } 293d6c10f1fSTejun Heo inode_wb_list_move_locked(inode, wb, &wb->b_dirty); 2946610a0bcSAndrew Morton } 2956610a0bcSAndrew Morton 2966610a0bcSAndrew Morton /* 29766f3b8e2SJens Axboe * requeue inode for re-scanning after bdi->b_io list is exhausted. 298c986d1e2SAndrew Morton */ 299f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb) 300c986d1e2SAndrew Morton { 301d6c10f1fSTejun Heo inode_wb_list_move_locked(inode, wb, &wb->b_more_io); 302c986d1e2SAndrew Morton } 303c986d1e2SAndrew Morton 3041c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode) 3051c0eeaf5SJoern Engel { 306365b94aeSJan Kara inode->i_state &= ~I_SYNC; 3074eff96ddSJan Kara /* If inode is clean an unused, put it into LRU now... */ 3084eff96ddSJan Kara inode_add_lru(inode); 309365b94aeSJan Kara /* Waiters must see I_SYNC cleared before being woken up */ 3101c0eeaf5SJoern Engel smp_mb(); 3111c0eeaf5SJoern Engel wake_up_bit(&inode->i_state, __I_SYNC); 3121c0eeaf5SJoern Engel } 3131c0eeaf5SJoern Engel 314d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t) 315d2caa3c5SJeff Layton { 316d2caa3c5SJeff Layton bool ret = time_after(inode->dirtied_when, t); 317d2caa3c5SJeff Layton #ifndef CONFIG_64BIT 318d2caa3c5SJeff Layton /* 319d2caa3c5SJeff Layton * For inodes being constantly redirtied, dirtied_when can get stuck. 320d2caa3c5SJeff Layton * It _appears_ to be in the future, but is actually in distant past. 321d2caa3c5SJeff Layton * This test is necessary to prevent such wrapped-around relative times 3225b0830cbSJens Axboe * from permanently stopping the whole bdi writeback. 323d2caa3c5SJeff Layton */ 324d2caa3c5SJeff Layton ret = ret && time_before_eq(inode->dirtied_when, jiffies); 325d2caa3c5SJeff Layton #endif 326d2caa3c5SJeff Layton return ret; 327d2caa3c5SJeff Layton } 328d2caa3c5SJeff Layton 3290ae45f63STheodore Ts'o #define EXPIRE_DIRTY_ATIME 0x0001 3300ae45f63STheodore Ts'o 331c986d1e2SAndrew Morton /* 3320e2f2b23SWang Sheng-Hui * Move expired (dirtied before work->older_than_this) dirty inodes from 333697e6fedSJan Kara * @delaying_queue to @dispatch_queue. 3342c136579SFengguang Wu */ 335e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue, 3362c136579SFengguang Wu struct list_head *dispatch_queue, 3370ae45f63STheodore Ts'o int flags, 338ad4e38ddSCurt Wohlgemuth struct wb_writeback_work *work) 3392c136579SFengguang Wu { 3400ae45f63STheodore Ts'o unsigned long *older_than_this = NULL; 3410ae45f63STheodore Ts'o unsigned long expire_time; 3425c03449dSShaohua Li LIST_HEAD(tmp); 3435c03449dSShaohua Li struct list_head *pos, *node; 344cf137307SJens Axboe struct super_block *sb = NULL; 3455c03449dSShaohua Li struct inode *inode; 346cf137307SJens Axboe int do_sb_sort = 0; 347e84d0a4fSWu Fengguang int moved = 0; 3485c03449dSShaohua Li 3490ae45f63STheodore Ts'o if ((flags & EXPIRE_DIRTY_ATIME) == 0) 3500ae45f63STheodore Ts'o older_than_this = work->older_than_this; 351a2f48706STheodore Ts'o else if (!work->for_sync) { 352a2f48706STheodore Ts'o expire_time = jiffies - (dirtytime_expire_interval * HZ); 3530ae45f63STheodore Ts'o older_than_this = &expire_time; 3540ae45f63STheodore Ts'o } 3552c136579SFengguang Wu while (!list_empty(delaying_queue)) { 3567ccf19a8SNick Piggin inode = wb_inode(delaying_queue->prev); 3570ae45f63STheodore Ts'o if (older_than_this && 3580ae45f63STheodore Ts'o inode_dirtied_after(inode, *older_than_this)) 3592c136579SFengguang Wu break; 360a8855990SJan Kara list_move(&inode->i_wb_list, &tmp); 361a8855990SJan Kara moved++; 3620ae45f63STheodore Ts'o if (flags & EXPIRE_DIRTY_ATIME) 3630ae45f63STheodore Ts'o set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state); 364a8855990SJan Kara if (sb_is_blkdev_sb(inode->i_sb)) 365a8855990SJan Kara continue; 366cf137307SJens Axboe if (sb && sb != inode->i_sb) 367cf137307SJens Axboe do_sb_sort = 1; 368cf137307SJens Axboe sb = inode->i_sb; 3695c03449dSShaohua Li } 3705c03449dSShaohua Li 371cf137307SJens Axboe /* just one sb in list, splice to dispatch_queue and we're done */ 372cf137307SJens Axboe if (!do_sb_sort) { 373cf137307SJens Axboe list_splice(&tmp, dispatch_queue); 374e84d0a4fSWu Fengguang goto out; 375cf137307SJens Axboe } 376cf137307SJens Axboe 3775c03449dSShaohua Li /* Move inodes from one superblock together */ 3785c03449dSShaohua Li while (!list_empty(&tmp)) { 3797ccf19a8SNick Piggin sb = wb_inode(tmp.prev)->i_sb; 3805c03449dSShaohua Li list_for_each_prev_safe(pos, node, &tmp) { 3817ccf19a8SNick Piggin inode = wb_inode(pos); 3825c03449dSShaohua Li if (inode->i_sb == sb) 3837ccf19a8SNick Piggin list_move(&inode->i_wb_list, dispatch_queue); 3842c136579SFengguang Wu } 3852c136579SFengguang Wu } 386e84d0a4fSWu Fengguang out: 387e84d0a4fSWu Fengguang return moved; 3885c03449dSShaohua Li } 3892c136579SFengguang Wu 3902c136579SFengguang Wu /* 3912c136579SFengguang Wu * Queue all expired dirty inodes for io, eldest first. 3924ea879b9SWu Fengguang * Before 3934ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 3944ea879b9SWu Fengguang * =============> gf edc BA 3954ea879b9SWu Fengguang * After 3964ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 3974ea879b9SWu Fengguang * =============> g fBAedc 3984ea879b9SWu Fengguang * | 3994ea879b9SWu Fengguang * +--> dequeue for IO 4002c136579SFengguang Wu */ 401ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work) 4022c136579SFengguang Wu { 403e84d0a4fSWu Fengguang int moved; 4040ae45f63STheodore Ts'o 405f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 4064ea879b9SWu Fengguang list_splice_init(&wb->b_more_io, &wb->b_io); 4070ae45f63STheodore Ts'o moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work); 4080ae45f63STheodore Ts'o moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io, 4090ae45f63STheodore Ts'o EXPIRE_DIRTY_ATIME, work); 410d6c10f1fSTejun Heo if (moved) 411d6c10f1fSTejun Heo wb_io_lists_populated(wb); 412ad4e38ddSCurt Wohlgemuth trace_writeback_queue_io(wb, work, moved); 41366f3b8e2SJens Axboe } 41466f3b8e2SJens Axboe 415a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc) 41666f3b8e2SJens Axboe { 4179fb0a7daSTejun Heo int ret; 4189fb0a7daSTejun Heo 4199fb0a7daSTejun Heo if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) { 4209fb0a7daSTejun Heo trace_writeback_write_inode_start(inode, wbc); 4219fb0a7daSTejun Heo ret = inode->i_sb->s_op->write_inode(inode, wbc); 4229fb0a7daSTejun Heo trace_writeback_write_inode(inode, wbc); 4239fb0a7daSTejun Heo return ret; 4249fb0a7daSTejun Heo } 42503ba3782SJens Axboe return 0; 42666f3b8e2SJens Axboe } 42708d8e974SFengguang Wu 4282c136579SFengguang Wu /* 429169ebd90SJan Kara * Wait for writeback on an inode to complete. Called with i_lock held. 430169ebd90SJan Kara * Caller must make sure inode cannot go away when we drop i_lock. 43101c03194SChristoph Hellwig */ 432169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode) 433169ebd90SJan Kara __releases(inode->i_lock) 434169ebd90SJan Kara __acquires(inode->i_lock) 43501c03194SChristoph Hellwig { 43601c03194SChristoph Hellwig DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); 43701c03194SChristoph Hellwig wait_queue_head_t *wqh; 43801c03194SChristoph Hellwig 43901c03194SChristoph Hellwig wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 44058a9d3d8SRichard Kennedy while (inode->i_state & I_SYNC) { 441250df6edSDave Chinner spin_unlock(&inode->i_lock); 44274316201SNeilBrown __wait_on_bit(wqh, &wq, bit_wait, 44374316201SNeilBrown TASK_UNINTERRUPTIBLE); 444250df6edSDave Chinner spin_lock(&inode->i_lock); 44558a9d3d8SRichard Kennedy } 44601c03194SChristoph Hellwig } 44701c03194SChristoph Hellwig 44801c03194SChristoph Hellwig /* 449169ebd90SJan Kara * Wait for writeback on an inode to complete. Caller must have inode pinned. 450169ebd90SJan Kara */ 451169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode) 452169ebd90SJan Kara { 453169ebd90SJan Kara spin_lock(&inode->i_lock); 454169ebd90SJan Kara __inode_wait_for_writeback(inode); 455169ebd90SJan Kara spin_unlock(&inode->i_lock); 456169ebd90SJan Kara } 457169ebd90SJan Kara 458169ebd90SJan Kara /* 459169ebd90SJan Kara * Sleep until I_SYNC is cleared. This function must be called with i_lock 460169ebd90SJan Kara * held and drops it. It is aimed for callers not holding any inode reference 461169ebd90SJan Kara * so once i_lock is dropped, inode can go away. 462169ebd90SJan Kara */ 463169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode) 464169ebd90SJan Kara __releases(inode->i_lock) 465169ebd90SJan Kara { 466169ebd90SJan Kara DEFINE_WAIT(wait); 467169ebd90SJan Kara wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 468169ebd90SJan Kara int sleep; 469169ebd90SJan Kara 470169ebd90SJan Kara prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); 471169ebd90SJan Kara sleep = inode->i_state & I_SYNC; 472169ebd90SJan Kara spin_unlock(&inode->i_lock); 473169ebd90SJan Kara if (sleep) 474169ebd90SJan Kara schedule(); 475169ebd90SJan Kara finish_wait(wqh, &wait); 476169ebd90SJan Kara } 477169ebd90SJan Kara 478169ebd90SJan Kara /* 479ccb26b5aSJan Kara * Find proper writeback list for the inode depending on its current state and 480ccb26b5aSJan Kara * possibly also change of its state while we were doing writeback. Here we 481ccb26b5aSJan Kara * handle things such as livelock prevention or fairness of writeback among 482ccb26b5aSJan Kara * inodes. This function can be called only by flusher thread - noone else 483ccb26b5aSJan Kara * processes all inodes in writeback lists and requeueing inodes behind flusher 484ccb26b5aSJan Kara * thread's back can have unexpected consequences. 485ccb26b5aSJan Kara */ 486ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb, 487ccb26b5aSJan Kara struct writeback_control *wbc) 488ccb26b5aSJan Kara { 489ccb26b5aSJan Kara if (inode->i_state & I_FREEING) 490ccb26b5aSJan Kara return; 491ccb26b5aSJan Kara 492ccb26b5aSJan Kara /* 493ccb26b5aSJan Kara * Sync livelock prevention. Each inode is tagged and synced in one 494ccb26b5aSJan Kara * shot. If still dirty, it will be redirty_tail()'ed below. Update 495ccb26b5aSJan Kara * the dirty time to prevent enqueue and sync it again. 496ccb26b5aSJan Kara */ 497ccb26b5aSJan Kara if ((inode->i_state & I_DIRTY) && 498ccb26b5aSJan Kara (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)) 499ccb26b5aSJan Kara inode->dirtied_when = jiffies; 500ccb26b5aSJan Kara 5014f8ad655SJan Kara if (wbc->pages_skipped) { 5024f8ad655SJan Kara /* 5034f8ad655SJan Kara * writeback is not making progress due to locked 5044f8ad655SJan Kara * buffers. Skip this inode for now. 5054f8ad655SJan Kara */ 5064f8ad655SJan Kara redirty_tail(inode, wb); 5074f8ad655SJan Kara return; 5084f8ad655SJan Kara } 5094f8ad655SJan Kara 510ccb26b5aSJan Kara if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) { 511ccb26b5aSJan Kara /* 512ccb26b5aSJan Kara * We didn't write back all the pages. nfs_writepages() 513ccb26b5aSJan Kara * sometimes bales out without doing anything. 514ccb26b5aSJan Kara */ 515ccb26b5aSJan Kara if (wbc->nr_to_write <= 0) { 516ccb26b5aSJan Kara /* Slice used up. Queue for next turn. */ 517ccb26b5aSJan Kara requeue_io(inode, wb); 518ccb26b5aSJan Kara } else { 519ccb26b5aSJan Kara /* 520ccb26b5aSJan Kara * Writeback blocked by something other than 521ccb26b5aSJan Kara * congestion. Delay the inode for some time to 522ccb26b5aSJan Kara * avoid spinning on the CPU (100% iowait) 523ccb26b5aSJan Kara * retrying writeback of the dirty page/inode 524ccb26b5aSJan Kara * that cannot be performed immediately. 525ccb26b5aSJan Kara */ 526ccb26b5aSJan Kara redirty_tail(inode, wb); 527ccb26b5aSJan Kara } 528ccb26b5aSJan Kara } else if (inode->i_state & I_DIRTY) { 529ccb26b5aSJan Kara /* 530ccb26b5aSJan Kara * Filesystems can dirty the inode during writeback operations, 531ccb26b5aSJan Kara * such as delayed allocation during submission or metadata 532ccb26b5aSJan Kara * updates after data IO completion. 533ccb26b5aSJan Kara */ 534ccb26b5aSJan Kara redirty_tail(inode, wb); 5350ae45f63STheodore Ts'o } else if (inode->i_state & I_DIRTY_TIME) { 536a2f48706STheodore Ts'o inode->dirtied_when = jiffies; 537d6c10f1fSTejun Heo inode_wb_list_move_locked(inode, wb, &wb->b_dirty_time); 538ccb26b5aSJan Kara } else { 539ccb26b5aSJan Kara /* The inode is clean. Remove from writeback lists. */ 540d6c10f1fSTejun Heo inode_wb_list_del_locked(inode, wb); 541ccb26b5aSJan Kara } 542ccb26b5aSJan Kara } 543ccb26b5aSJan Kara 544ccb26b5aSJan Kara /* 5454f8ad655SJan Kara * Write out an inode and its dirty pages. Do not update the writeback list 5464f8ad655SJan Kara * linkage. That is left to the caller. The caller is also responsible for 5474f8ad655SJan Kara * setting I_SYNC flag and calling inode_sync_complete() to clear it. 5481da177e4SLinus Torvalds */ 5491da177e4SLinus Torvalds static int 550cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) 5511da177e4SLinus Torvalds { 5521da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 553251d6a47SWu Fengguang long nr_to_write = wbc->nr_to_write; 55401c03194SChristoph Hellwig unsigned dirty; 5551da177e4SLinus Torvalds int ret; 5561da177e4SLinus Torvalds 5574f8ad655SJan Kara WARN_ON(!(inode->i_state & I_SYNC)); 5581da177e4SLinus Torvalds 5599fb0a7daSTejun Heo trace_writeback_single_inode_start(inode, wbc, nr_to_write); 5609fb0a7daSTejun Heo 5611da177e4SLinus Torvalds ret = do_writepages(mapping, wbc); 5621da177e4SLinus Torvalds 56326821ed4SChristoph Hellwig /* 56426821ed4SChristoph Hellwig * Make sure to wait on the data before writing out the metadata. 56526821ed4SChristoph Hellwig * This is important for filesystems that modify metadata on data 5667747bd4bSDave Chinner * I/O completion. We don't do it for sync(2) writeback because it has a 5677747bd4bSDave Chinner * separate, external IO completion path and ->sync_fs for guaranteeing 5687747bd4bSDave Chinner * inode metadata is written back correctly. 56926821ed4SChristoph Hellwig */ 5707747bd4bSDave Chinner if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) { 57126821ed4SChristoph Hellwig int err = filemap_fdatawait(mapping); 5721da177e4SLinus Torvalds if (ret == 0) 5731da177e4SLinus Torvalds ret = err; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5765547e8aaSDmitry Monakhov /* 5775547e8aaSDmitry Monakhov * Some filesystems may redirty the inode during the writeback 5785547e8aaSDmitry Monakhov * due to delalloc, clear dirty metadata flags right before 5795547e8aaSDmitry Monakhov * write_inode() 5805547e8aaSDmitry Monakhov */ 581250df6edSDave Chinner spin_lock(&inode->i_lock); 5829c6ac78eSTejun Heo 5835547e8aaSDmitry Monakhov dirty = inode->i_state & I_DIRTY; 584a2f48706STheodore Ts'o if (inode->i_state & I_DIRTY_TIME) { 585a2f48706STheodore Ts'o if ((dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) || 586a2f48706STheodore Ts'o unlikely(inode->i_state & I_DIRTY_TIME_EXPIRED) || 587a2f48706STheodore Ts'o unlikely(time_after(jiffies, 588a2f48706STheodore Ts'o (inode->dirtied_time_when + 589a2f48706STheodore Ts'o dirtytime_expire_interval * HZ)))) { 5900ae45f63STheodore Ts'o dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED; 5910ae45f63STheodore Ts'o trace_writeback_lazytime(inode); 5920ae45f63STheodore Ts'o } 593a2f48706STheodore Ts'o } else 594a2f48706STheodore Ts'o inode->i_state &= ~I_DIRTY_TIME_EXPIRED; 5950ae45f63STheodore Ts'o inode->i_state &= ~dirty; 5969c6ac78eSTejun Heo 5979c6ac78eSTejun Heo /* 5989c6ac78eSTejun Heo * Paired with smp_mb() in __mark_inode_dirty(). This allows 5999c6ac78eSTejun Heo * __mark_inode_dirty() to test i_state without grabbing i_lock - 6009c6ac78eSTejun Heo * either they see the I_DIRTY bits cleared or we see the dirtied 6019c6ac78eSTejun Heo * inode. 6029c6ac78eSTejun Heo * 6039c6ac78eSTejun Heo * I_DIRTY_PAGES is always cleared together above even if @mapping 6049c6ac78eSTejun Heo * still has dirty pages. The flag is reinstated after smp_mb() if 6059c6ac78eSTejun Heo * necessary. This guarantees that either __mark_inode_dirty() 6069c6ac78eSTejun Heo * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY. 6079c6ac78eSTejun Heo */ 6089c6ac78eSTejun Heo smp_mb(); 6099c6ac78eSTejun Heo 6109c6ac78eSTejun Heo if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 6119c6ac78eSTejun Heo inode->i_state |= I_DIRTY_PAGES; 6129c6ac78eSTejun Heo 613250df6edSDave Chinner spin_unlock(&inode->i_lock); 6149c6ac78eSTejun Heo 6150ae45f63STheodore Ts'o if (dirty & I_DIRTY_TIME) 6160ae45f63STheodore Ts'o mark_inode_dirty_sync(inode); 61726821ed4SChristoph Hellwig /* Don't write the inode if only I_DIRTY_PAGES was set */ 6180ae45f63STheodore Ts'o if (dirty & ~I_DIRTY_PAGES) { 619a9185b41SChristoph Hellwig int err = write_inode(inode, wbc); 6201da177e4SLinus Torvalds if (ret == 0) 6211da177e4SLinus Torvalds ret = err; 6221da177e4SLinus Torvalds } 6234f8ad655SJan Kara trace_writeback_single_inode(inode, wbc, nr_to_write); 6244f8ad655SJan Kara return ret; 6254f8ad655SJan Kara } 6264f8ad655SJan Kara 6274f8ad655SJan Kara /* 6284f8ad655SJan Kara * Write out an inode's dirty pages. Either the caller has an active reference 6294f8ad655SJan Kara * on the inode or the inode has I_WILL_FREE set. 6304f8ad655SJan Kara * 6314f8ad655SJan Kara * This function is designed to be called for writing back one inode which 6324f8ad655SJan Kara * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode() 6334f8ad655SJan Kara * and does more profound writeback list handling in writeback_sb_inodes(). 6344f8ad655SJan Kara */ 6354f8ad655SJan Kara static int 6364f8ad655SJan Kara writeback_single_inode(struct inode *inode, struct bdi_writeback *wb, 6374f8ad655SJan Kara struct writeback_control *wbc) 6384f8ad655SJan Kara { 6394f8ad655SJan Kara int ret = 0; 6404f8ad655SJan Kara 6414f8ad655SJan Kara spin_lock(&inode->i_lock); 6424f8ad655SJan Kara if (!atomic_read(&inode->i_count)) 6434f8ad655SJan Kara WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); 6444f8ad655SJan Kara else 6454f8ad655SJan Kara WARN_ON(inode->i_state & I_WILL_FREE); 6464f8ad655SJan Kara 6474f8ad655SJan Kara if (inode->i_state & I_SYNC) { 6484f8ad655SJan Kara if (wbc->sync_mode != WB_SYNC_ALL) 6494f8ad655SJan Kara goto out; 6504f8ad655SJan Kara /* 651169ebd90SJan Kara * It's a data-integrity sync. We must wait. Since callers hold 652169ebd90SJan Kara * inode reference or inode has I_WILL_FREE set, it cannot go 653169ebd90SJan Kara * away under us. 6544f8ad655SJan Kara */ 655169ebd90SJan Kara __inode_wait_for_writeback(inode); 6564f8ad655SJan Kara } 6574f8ad655SJan Kara WARN_ON(inode->i_state & I_SYNC); 6584f8ad655SJan Kara /* 659f9b0e058SJan Kara * Skip inode if it is clean and we have no outstanding writeback in 660f9b0e058SJan Kara * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this 661f9b0e058SJan Kara * function since flusher thread may be doing for example sync in 662f9b0e058SJan Kara * parallel and if we move the inode, it could get skipped. So here we 663f9b0e058SJan Kara * make sure inode is on some writeback list and leave it there unless 664f9b0e058SJan Kara * we have completely cleaned the inode. 6654f8ad655SJan Kara */ 6660ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL) && 667f9b0e058SJan Kara (wbc->sync_mode != WB_SYNC_ALL || 668f9b0e058SJan Kara !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))) 6694f8ad655SJan Kara goto out; 6704f8ad655SJan Kara inode->i_state |= I_SYNC; 6714f8ad655SJan Kara spin_unlock(&inode->i_lock); 6724f8ad655SJan Kara 673cd8ed2a4SYan Hong ret = __writeback_single_inode(inode, wbc); 6741da177e4SLinus Torvalds 675f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 676250df6edSDave Chinner spin_lock(&inode->i_lock); 6774f8ad655SJan Kara /* 6784f8ad655SJan Kara * If inode is clean, remove it from writeback lists. Otherwise don't 6794f8ad655SJan Kara * touch it. See comment above for explanation. 6804f8ad655SJan Kara */ 6810ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 682d6c10f1fSTejun Heo inode_wb_list_del_locked(inode, wb); 6834f8ad655SJan Kara spin_unlock(&wb->list_lock); 6841c0eeaf5SJoern Engel inode_sync_complete(inode); 6854f8ad655SJan Kara out: 6864f8ad655SJan Kara spin_unlock(&inode->i_lock); 6871da177e4SLinus Torvalds return ret; 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds 690a88a341aSTejun Heo static long writeback_chunk_size(struct bdi_writeback *wb, 6911a12d8bdSWu Fengguang struct wb_writeback_work *work) 692d46db3d5SWu Fengguang { 693d46db3d5SWu Fengguang long pages; 694d46db3d5SWu Fengguang 695d46db3d5SWu Fengguang /* 696d46db3d5SWu Fengguang * WB_SYNC_ALL mode does livelock avoidance by syncing dirty 697d46db3d5SWu Fengguang * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX 698d46db3d5SWu Fengguang * here avoids calling into writeback_inodes_wb() more than once. 699d46db3d5SWu Fengguang * 700d46db3d5SWu Fengguang * The intended call sequence for WB_SYNC_ALL writeback is: 701d46db3d5SWu Fengguang * 702d46db3d5SWu Fengguang * wb_writeback() 703d46db3d5SWu Fengguang * writeback_sb_inodes() <== called only once 704d46db3d5SWu Fengguang * write_cache_pages() <== called once for each inode 705d46db3d5SWu Fengguang * (quickly) tag currently dirty pages 706d46db3d5SWu Fengguang * (maybe slowly) sync all tagged pages 707d46db3d5SWu Fengguang */ 708d46db3d5SWu Fengguang if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages) 709d46db3d5SWu Fengguang pages = LONG_MAX; 7101a12d8bdSWu Fengguang else { 711a88a341aSTejun Heo pages = min(wb->avg_write_bandwidth / 2, 7121a12d8bdSWu Fengguang global_dirty_limit / DIRTY_SCOPE); 7131a12d8bdSWu Fengguang pages = min(pages, work->nr_pages); 7141a12d8bdSWu Fengguang pages = round_down(pages + MIN_WRITEBACK_PAGES, 7151a12d8bdSWu Fengguang MIN_WRITEBACK_PAGES); 7161a12d8bdSWu Fengguang } 717d46db3d5SWu Fengguang 718d46db3d5SWu Fengguang return pages; 719d46db3d5SWu Fengguang } 720d46db3d5SWu Fengguang 72103ba3782SJens Axboe /* 722f11c9c5cSEdward Shishkin * Write a portion of b_io inodes which belong to @sb. 723edadfb10SChristoph Hellwig * 724d46db3d5SWu Fengguang * Return the number of pages and/or inodes written. 725f11c9c5cSEdward Shishkin */ 726d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb, 727d46db3d5SWu Fengguang struct bdi_writeback *wb, 728d46db3d5SWu Fengguang struct wb_writeback_work *work) 72903ba3782SJens Axboe { 730d46db3d5SWu Fengguang struct writeback_control wbc = { 731d46db3d5SWu Fengguang .sync_mode = work->sync_mode, 732d46db3d5SWu Fengguang .tagged_writepages = work->tagged_writepages, 733d46db3d5SWu Fengguang .for_kupdate = work->for_kupdate, 734d46db3d5SWu Fengguang .for_background = work->for_background, 7357747bd4bSDave Chinner .for_sync = work->for_sync, 736d46db3d5SWu Fengguang .range_cyclic = work->range_cyclic, 737d46db3d5SWu Fengguang .range_start = 0, 738d46db3d5SWu Fengguang .range_end = LLONG_MAX, 739d46db3d5SWu Fengguang }; 740d46db3d5SWu Fengguang unsigned long start_time = jiffies; 741d46db3d5SWu Fengguang long write_chunk; 742d46db3d5SWu Fengguang long wrote = 0; /* count both pages and inodes */ 743d46db3d5SWu Fengguang 74403ba3782SJens Axboe while (!list_empty(&wb->b_io)) { 7457ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 746edadfb10SChristoph Hellwig 747edadfb10SChristoph Hellwig if (inode->i_sb != sb) { 748d46db3d5SWu Fengguang if (work->sb) { 749edadfb10SChristoph Hellwig /* 750edadfb10SChristoph Hellwig * We only want to write back data for this 751edadfb10SChristoph Hellwig * superblock, move all inodes not belonging 752edadfb10SChristoph Hellwig * to it back onto the dirty list. 753edadfb10SChristoph Hellwig */ 754f758eeabSChristoph Hellwig redirty_tail(inode, wb); 75566f3b8e2SJens Axboe continue; 75666f3b8e2SJens Axboe } 757edadfb10SChristoph Hellwig 758edadfb10SChristoph Hellwig /* 759edadfb10SChristoph Hellwig * The inode belongs to a different superblock. 760edadfb10SChristoph Hellwig * Bounce back to the caller to unpin this and 761edadfb10SChristoph Hellwig * pin the next superblock. 762edadfb10SChristoph Hellwig */ 763d46db3d5SWu Fengguang break; 764edadfb10SChristoph Hellwig } 765edadfb10SChristoph Hellwig 7669843b76aSChristoph Hellwig /* 767331cbdeeSWanpeng Li * Don't bother with new inodes or inodes being freed, first 768331cbdeeSWanpeng Li * kind does not need periodic writeout yet, and for the latter 7699843b76aSChristoph Hellwig * kind writeout is handled by the freer. 7709843b76aSChristoph Hellwig */ 771250df6edSDave Chinner spin_lock(&inode->i_lock); 7729843b76aSChristoph Hellwig if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { 773250df6edSDave Chinner spin_unlock(&inode->i_lock); 774fcc5c222SWu Fengguang redirty_tail(inode, wb); 7757ef0d737SNick Piggin continue; 7767ef0d737SNick Piggin } 777cc1676d9SJan Kara if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) { 778cc1676d9SJan Kara /* 779cc1676d9SJan Kara * If this inode is locked for writeback and we are not 780cc1676d9SJan Kara * doing writeback-for-data-integrity, move it to 781cc1676d9SJan Kara * b_more_io so that writeback can proceed with the 782cc1676d9SJan Kara * other inodes on s_io. 783cc1676d9SJan Kara * 784cc1676d9SJan Kara * We'll have another go at writing back this inode 785cc1676d9SJan Kara * when we completed a full scan of b_io. 786cc1676d9SJan Kara */ 787cc1676d9SJan Kara spin_unlock(&inode->i_lock); 788cc1676d9SJan Kara requeue_io(inode, wb); 789cc1676d9SJan Kara trace_writeback_sb_inodes_requeue(inode); 790cc1676d9SJan Kara continue; 791cc1676d9SJan Kara } 792f0d07b7fSJan Kara spin_unlock(&wb->list_lock); 793f0d07b7fSJan Kara 7944f8ad655SJan Kara /* 7954f8ad655SJan Kara * We already requeued the inode if it had I_SYNC set and we 7964f8ad655SJan Kara * are doing WB_SYNC_NONE writeback. So this catches only the 7974f8ad655SJan Kara * WB_SYNC_ALL case. 7984f8ad655SJan Kara */ 799169ebd90SJan Kara if (inode->i_state & I_SYNC) { 800169ebd90SJan Kara /* Wait for I_SYNC. This function drops i_lock... */ 801169ebd90SJan Kara inode_sleep_on_writeback(inode); 802169ebd90SJan Kara /* Inode may be gone, start again */ 803ead188f9SJan Kara spin_lock(&wb->list_lock); 804169ebd90SJan Kara continue; 805169ebd90SJan Kara } 8064f8ad655SJan Kara inode->i_state |= I_SYNC; 8074f8ad655SJan Kara spin_unlock(&inode->i_lock); 808169ebd90SJan Kara 809a88a341aSTejun Heo write_chunk = writeback_chunk_size(wb, work); 810d46db3d5SWu Fengguang wbc.nr_to_write = write_chunk; 811d46db3d5SWu Fengguang wbc.pages_skipped = 0; 812250df6edSDave Chinner 813169ebd90SJan Kara /* 814169ebd90SJan Kara * We use I_SYNC to pin the inode in memory. While it is set 815169ebd90SJan Kara * evict_inode() will wait so the inode cannot be freed. 816169ebd90SJan Kara */ 817cd8ed2a4SYan Hong __writeback_single_inode(inode, &wbc); 818d46db3d5SWu Fengguang 819d46db3d5SWu Fengguang work->nr_pages -= write_chunk - wbc.nr_to_write; 820d46db3d5SWu Fengguang wrote += write_chunk - wbc.nr_to_write; 8214f8ad655SJan Kara spin_lock(&wb->list_lock); 8224f8ad655SJan Kara spin_lock(&inode->i_lock); 8230ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 824d46db3d5SWu Fengguang wrote++; 8254f8ad655SJan Kara requeue_inode(inode, wb, &wbc); 8264f8ad655SJan Kara inode_sync_complete(inode); 8270f1b1fd8SDave Chinner spin_unlock(&inode->i_lock); 828169ebd90SJan Kara cond_resched_lock(&wb->list_lock); 829d46db3d5SWu Fengguang /* 830d46db3d5SWu Fengguang * bail out to wb_writeback() often enough to check 831d46db3d5SWu Fengguang * background threshold and other termination conditions. 832d46db3d5SWu Fengguang */ 833d46db3d5SWu Fengguang if (wrote) { 834d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 835d46db3d5SWu Fengguang break; 836d46db3d5SWu Fengguang if (work->nr_pages <= 0) 837d46db3d5SWu Fengguang break; 8381da177e4SLinus Torvalds } 8398bc3be27SFengguang Wu } 840d46db3d5SWu Fengguang return wrote; 841f11c9c5cSEdward Shishkin } 84238f21977SNick Piggin 843d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb, 844d46db3d5SWu Fengguang struct wb_writeback_work *work) 845f11c9c5cSEdward Shishkin { 846d46db3d5SWu Fengguang unsigned long start_time = jiffies; 847d46db3d5SWu Fengguang long wrote = 0; 848f11c9c5cSEdward Shishkin 849f11c9c5cSEdward Shishkin while (!list_empty(&wb->b_io)) { 8507ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 851f11c9c5cSEdward Shishkin struct super_block *sb = inode->i_sb; 852f11c9c5cSEdward Shishkin 853eb6ef3dfSKonstantin Khlebnikov if (!trylock_super(sb)) { 8540e995816SWu Fengguang /* 855eb6ef3dfSKonstantin Khlebnikov * trylock_super() may fail consistently due to 8560e995816SWu Fengguang * s_umount being grabbed by someone else. Don't use 8570e995816SWu Fengguang * requeue_io() to avoid busy retrying the inode/sb. 8580e995816SWu Fengguang */ 8590e995816SWu Fengguang redirty_tail(inode, wb); 860d19de7edSChristoph Hellwig continue; 861334132aeSChristoph Hellwig } 862d46db3d5SWu Fengguang wrote += writeback_sb_inodes(sb, wb, work); 863eb6ef3dfSKonstantin Khlebnikov up_read(&sb->s_umount); 864f11c9c5cSEdward Shishkin 865d46db3d5SWu Fengguang /* refer to the same tests at the end of writeback_sb_inodes */ 866d46db3d5SWu Fengguang if (wrote) { 867d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 868d46db3d5SWu Fengguang break; 869d46db3d5SWu Fengguang if (work->nr_pages <= 0) 870f11c9c5cSEdward Shishkin break; 871f11c9c5cSEdward Shishkin } 872d46db3d5SWu Fengguang } 87366f3b8e2SJens Axboe /* Leave any unwritten inodes on b_io */ 874d46db3d5SWu Fengguang return wrote; 87566f3b8e2SJens Axboe } 87666f3b8e2SJens Axboe 8777d9f073bSWanpeng Li static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages, 8780e175a18SCurt Wohlgemuth enum wb_reason reason) 879edadfb10SChristoph Hellwig { 880d46db3d5SWu Fengguang struct wb_writeback_work work = { 881d46db3d5SWu Fengguang .nr_pages = nr_pages, 882d46db3d5SWu Fengguang .sync_mode = WB_SYNC_NONE, 883d46db3d5SWu Fengguang .range_cyclic = 1, 8840e175a18SCurt Wohlgemuth .reason = reason, 885d46db3d5SWu Fengguang }; 886edadfb10SChristoph Hellwig 887f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 888424b351fSWu Fengguang if (list_empty(&wb->b_io)) 889ad4e38ddSCurt Wohlgemuth queue_io(wb, &work); 890d46db3d5SWu Fengguang __writeback_inodes_wb(wb, &work); 891f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 892edadfb10SChristoph Hellwig 893d46db3d5SWu Fengguang return nr_pages - work.nr_pages; 89466f3b8e2SJens Axboe } 89566f3b8e2SJens Axboe 896a88a341aSTejun Heo static bool over_bground_thresh(struct bdi_writeback *wb) 89703ba3782SJens Axboe { 89803ba3782SJens Axboe unsigned long background_thresh, dirty_thresh; 89903ba3782SJens Axboe 90016c4042fSWu Fengguang global_dirty_limits(&background_thresh, &dirty_thresh); 90103ba3782SJens Axboe 902b00949aaSWu Fengguang if (global_page_state(NR_FILE_DIRTY) + 903b00949aaSWu Fengguang global_page_state(NR_UNSTABLE_NFS) > background_thresh) 904b00949aaSWu Fengguang return true; 905b00949aaSWu Fengguang 906a88a341aSTejun Heo if (wb_stat(wb, WB_RECLAIMABLE) > wb_dirty_limit(wb, background_thresh)) 907b00949aaSWu Fengguang return true; 908b00949aaSWu Fengguang 909b00949aaSWu Fengguang return false; 91003ba3782SJens Axboe } 91103ba3782SJens Axboe 91203ba3782SJens Axboe /* 913e98be2d5SWu Fengguang * Called under wb->list_lock. If there are multiple wb per bdi, 914e98be2d5SWu Fengguang * only the flusher working on the first wb should do it. 915e98be2d5SWu Fengguang */ 916e98be2d5SWu Fengguang static void wb_update_bandwidth(struct bdi_writeback *wb, 917e98be2d5SWu Fengguang unsigned long start_time) 918e98be2d5SWu Fengguang { 919a88a341aSTejun Heo __wb_update_bandwidth(wb, 0, 0, 0, 0, 0, start_time); 920e98be2d5SWu Fengguang } 921e98be2d5SWu Fengguang 922e98be2d5SWu Fengguang /* 92303ba3782SJens Axboe * Explicit flushing or periodic writeback of "old" data. 92403ba3782SJens Axboe * 92503ba3782SJens Axboe * Define "old": the first time one of an inode's pages is dirtied, we mark the 92603ba3782SJens Axboe * dirtying-time in the inode's address_space. So this periodic writeback code 92703ba3782SJens Axboe * just walks the superblock inode list, writing back any inodes which are 92803ba3782SJens Axboe * older than a specific point in time. 92903ba3782SJens Axboe * 93003ba3782SJens Axboe * Try to run once per dirty_writeback_interval. But if a writeback event 93103ba3782SJens Axboe * takes longer than a dirty_writeback_interval interval, then leave a 93203ba3782SJens Axboe * one-second gap. 93303ba3782SJens Axboe * 93403ba3782SJens Axboe * older_than_this takes precedence over nr_to_write. So we'll only write back 93503ba3782SJens Axboe * all dirty pages if they are all attached to "old" mappings. 93603ba3782SJens Axboe */ 937c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb, 93883ba7b07SChristoph Hellwig struct wb_writeback_work *work) 93903ba3782SJens Axboe { 940e98be2d5SWu Fengguang unsigned long wb_start = jiffies; 941d46db3d5SWu Fengguang long nr_pages = work->nr_pages; 9420dc83bd3SJan Kara unsigned long oldest_jif; 943a5989bdcSJan Kara struct inode *inode; 944d46db3d5SWu Fengguang long progress; 94503ba3782SJens Axboe 9460dc83bd3SJan Kara oldest_jif = jiffies; 9470dc83bd3SJan Kara work->older_than_this = &oldest_jif; 94803ba3782SJens Axboe 949e8dfc305SWu Fengguang spin_lock(&wb->list_lock); 95003ba3782SJens Axboe for (;;) { 95103ba3782SJens Axboe /* 952d3ddec76SWu Fengguang * Stop writeback when nr_pages has been consumed 95303ba3782SJens Axboe */ 95483ba7b07SChristoph Hellwig if (work->nr_pages <= 0) 95503ba3782SJens Axboe break; 95603ba3782SJens Axboe 95703ba3782SJens Axboe /* 958aa373cf5SJan Kara * Background writeout and kupdate-style writeback may 959aa373cf5SJan Kara * run forever. Stop them if there is other work to do 960aa373cf5SJan Kara * so that e.g. sync can proceed. They'll be restarted 961aa373cf5SJan Kara * after the other works are all done. 962aa373cf5SJan Kara */ 963aa373cf5SJan Kara if ((work->for_background || work->for_kupdate) && 964f0054bb1STejun Heo !list_empty(&wb->work_list)) 965aa373cf5SJan Kara break; 966aa373cf5SJan Kara 967aa373cf5SJan Kara /* 968d3ddec76SWu Fengguang * For background writeout, stop when we are below the 969d3ddec76SWu Fengguang * background dirty threshold 97003ba3782SJens Axboe */ 971a88a341aSTejun Heo if (work->for_background && !over_bground_thresh(wb)) 97203ba3782SJens Axboe break; 97303ba3782SJens Axboe 9741bc36b64SJan Kara /* 9751bc36b64SJan Kara * Kupdate and background works are special and we want to 9761bc36b64SJan Kara * include all inodes that need writing. Livelock avoidance is 9771bc36b64SJan Kara * handled by these works yielding to any other work so we are 9781bc36b64SJan Kara * safe. 9791bc36b64SJan Kara */ 980ba9aa839SWu Fengguang if (work->for_kupdate) { 9810dc83bd3SJan Kara oldest_jif = jiffies - 982ba9aa839SWu Fengguang msecs_to_jiffies(dirty_expire_interval * 10); 9831bc36b64SJan Kara } else if (work->for_background) 9840dc83bd3SJan Kara oldest_jif = jiffies; 985028c2dd1SDave Chinner 986d46db3d5SWu Fengguang trace_writeback_start(wb->bdi, work); 987e8dfc305SWu Fengguang if (list_empty(&wb->b_io)) 988ad4e38ddSCurt Wohlgemuth queue_io(wb, work); 98983ba7b07SChristoph Hellwig if (work->sb) 990d46db3d5SWu Fengguang progress = writeback_sb_inodes(work->sb, wb, work); 991edadfb10SChristoph Hellwig else 992d46db3d5SWu Fengguang progress = __writeback_inodes_wb(wb, work); 993d46db3d5SWu Fengguang trace_writeback_written(wb->bdi, work); 994028c2dd1SDave Chinner 995e98be2d5SWu Fengguang wb_update_bandwidth(wb, wb_start); 99603ba3782SJens Axboe 99703ba3782SJens Axboe /* 99871fd05a8SJens Axboe * Did we write something? Try for more 999e6fb6da2SWu Fengguang * 1000e6fb6da2SWu Fengguang * Dirty inodes are moved to b_io for writeback in batches. 1001e6fb6da2SWu Fengguang * The completion of the current batch does not necessarily 1002e6fb6da2SWu Fengguang * mean the overall work is done. So we keep looping as long 1003e6fb6da2SWu Fengguang * as made some progress on cleaning pages or inodes. 100471fd05a8SJens Axboe */ 1005d46db3d5SWu Fengguang if (progress) 100603ba3782SJens Axboe continue; 1007a5989bdcSJan Kara /* 1008e6fb6da2SWu Fengguang * No more inodes for IO, bail 1009a5989bdcSJan Kara */ 1010b7a2441fSWu Fengguang if (list_empty(&wb->b_more_io)) 101103ba3782SJens Axboe break; 101203ba3782SJens Axboe /* 10138010c3b6SJens Axboe * Nothing written. Wait for some inode to 10148010c3b6SJens Axboe * become available for writeback. Otherwise 10158010c3b6SJens Axboe * we'll just busyloop. 101603ba3782SJens Axboe */ 101703ba3782SJens Axboe if (!list_empty(&wb->b_more_io)) { 1018d46db3d5SWu Fengguang trace_writeback_wait(wb->bdi, work); 101903ba3782SJens Axboe inode = wb_inode(wb->b_more_io.prev); 1020250df6edSDave Chinner spin_lock(&inode->i_lock); 1021f0d07b7fSJan Kara spin_unlock(&wb->list_lock); 1022169ebd90SJan Kara /* This function drops i_lock... */ 1023169ebd90SJan Kara inode_sleep_on_writeback(inode); 1024f0d07b7fSJan Kara spin_lock(&wb->list_lock); 102503ba3782SJens Axboe } 102603ba3782SJens Axboe } 1027e8dfc305SWu Fengguang spin_unlock(&wb->list_lock); 102803ba3782SJens Axboe 1029d46db3d5SWu Fengguang return nr_pages - work->nr_pages; 103003ba3782SJens Axboe } 103103ba3782SJens Axboe 103203ba3782SJens Axboe /* 103383ba7b07SChristoph Hellwig * Return the next wb_writeback_work struct that hasn't been processed yet. 103403ba3782SJens Axboe */ 1035f0054bb1STejun Heo static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb) 103603ba3782SJens Axboe { 103783ba7b07SChristoph Hellwig struct wb_writeback_work *work = NULL; 103803ba3782SJens Axboe 1039f0054bb1STejun Heo spin_lock_bh(&wb->work_lock); 1040f0054bb1STejun Heo if (!list_empty(&wb->work_list)) { 1041f0054bb1STejun Heo work = list_entry(wb->work_list.next, 104283ba7b07SChristoph Hellwig struct wb_writeback_work, list); 104383ba7b07SChristoph Hellwig list_del_init(&work->list); 104403ba3782SJens Axboe } 1045f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 104683ba7b07SChristoph Hellwig return work; 104703ba3782SJens Axboe } 104803ba3782SJens Axboe 1049cdf01dd5SLinus Torvalds /* 1050cdf01dd5SLinus Torvalds * Add in the number of potentially dirty inodes, because each inode 1051cdf01dd5SLinus Torvalds * write can dirty pagecache in the underlying blockdev. 1052cdf01dd5SLinus Torvalds */ 1053cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void) 1054cdf01dd5SLinus Torvalds { 1055cdf01dd5SLinus Torvalds return global_page_state(NR_FILE_DIRTY) + 1056cdf01dd5SLinus Torvalds global_page_state(NR_UNSTABLE_NFS) + 1057cdf01dd5SLinus Torvalds get_nr_dirty_inodes(); 1058cdf01dd5SLinus Torvalds } 1059cdf01dd5SLinus Torvalds 10606585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb) 10616585027aSJan Kara { 1062a88a341aSTejun Heo if (over_bground_thresh(wb)) { 10636585027aSJan Kara 10646585027aSJan Kara struct wb_writeback_work work = { 10656585027aSJan Kara .nr_pages = LONG_MAX, 10666585027aSJan Kara .sync_mode = WB_SYNC_NONE, 10676585027aSJan Kara .for_background = 1, 10686585027aSJan Kara .range_cyclic = 1, 10690e175a18SCurt Wohlgemuth .reason = WB_REASON_BACKGROUND, 10706585027aSJan Kara }; 10716585027aSJan Kara 10726585027aSJan Kara return wb_writeback(wb, &work); 10736585027aSJan Kara } 10746585027aSJan Kara 10756585027aSJan Kara return 0; 10766585027aSJan Kara } 10776585027aSJan Kara 107803ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb) 107903ba3782SJens Axboe { 108003ba3782SJens Axboe unsigned long expired; 108103ba3782SJens Axboe long nr_pages; 108203ba3782SJens Axboe 108369b62d01SJens Axboe /* 108469b62d01SJens Axboe * When set to zero, disable periodic writeback 108569b62d01SJens Axboe */ 108669b62d01SJens Axboe if (!dirty_writeback_interval) 108769b62d01SJens Axboe return 0; 108869b62d01SJens Axboe 108903ba3782SJens Axboe expired = wb->last_old_flush + 109003ba3782SJens Axboe msecs_to_jiffies(dirty_writeback_interval * 10); 109103ba3782SJens Axboe if (time_before(jiffies, expired)) 109203ba3782SJens Axboe return 0; 109303ba3782SJens Axboe 109403ba3782SJens Axboe wb->last_old_flush = jiffies; 1095cdf01dd5SLinus Torvalds nr_pages = get_nr_dirty_pages(); 109603ba3782SJens Axboe 1097c4a77a6cSJens Axboe if (nr_pages) { 109883ba7b07SChristoph Hellwig struct wb_writeback_work work = { 1099c4a77a6cSJens Axboe .nr_pages = nr_pages, 1100c4a77a6cSJens Axboe .sync_mode = WB_SYNC_NONE, 1101c4a77a6cSJens Axboe .for_kupdate = 1, 1102c4a77a6cSJens Axboe .range_cyclic = 1, 11030e175a18SCurt Wohlgemuth .reason = WB_REASON_PERIODIC, 1104c4a77a6cSJens Axboe }; 1105c4a77a6cSJens Axboe 110683ba7b07SChristoph Hellwig return wb_writeback(wb, &work); 1107c4a77a6cSJens Axboe } 110803ba3782SJens Axboe 110903ba3782SJens Axboe return 0; 111003ba3782SJens Axboe } 111103ba3782SJens Axboe 111203ba3782SJens Axboe /* 111303ba3782SJens Axboe * Retrieve work items and do the writeback they describe 111403ba3782SJens Axboe */ 111525d130baSWanpeng Li static long wb_do_writeback(struct bdi_writeback *wb) 111603ba3782SJens Axboe { 111783ba7b07SChristoph Hellwig struct wb_writeback_work *work; 1118c4a77a6cSJens Axboe long wrote = 0; 111903ba3782SJens Axboe 11204452226eSTejun Heo set_bit(WB_writeback_running, &wb->state); 1121f0054bb1STejun Heo while ((work = get_next_work_item(wb)) != NULL) { 112283ba7b07SChristoph Hellwig 1123f0054bb1STejun Heo trace_writeback_exec(wb->bdi, work); 1124455b2864SDave Chinner 112583ba7b07SChristoph Hellwig wrote += wb_writeback(wb, work); 112603ba3782SJens Axboe 112703ba3782SJens Axboe /* 112883ba7b07SChristoph Hellwig * Notify the caller of completion if this is a synchronous 112983ba7b07SChristoph Hellwig * work item, otherwise just free it. 113003ba3782SJens Axboe */ 113183ba7b07SChristoph Hellwig if (work->done) 113283ba7b07SChristoph Hellwig complete(work->done); 113383ba7b07SChristoph Hellwig else 113483ba7b07SChristoph Hellwig kfree(work); 113503ba3782SJens Axboe } 113603ba3782SJens Axboe 113703ba3782SJens Axboe /* 113803ba3782SJens Axboe * Check for periodic writeback, kupdated() style 113903ba3782SJens Axboe */ 114003ba3782SJens Axboe wrote += wb_check_old_data_flush(wb); 11416585027aSJan Kara wrote += wb_check_background_flush(wb); 11424452226eSTejun Heo clear_bit(WB_writeback_running, &wb->state); 114303ba3782SJens Axboe 114403ba3782SJens Axboe return wrote; 114503ba3782SJens Axboe } 114603ba3782SJens Axboe 114703ba3782SJens Axboe /* 114803ba3782SJens Axboe * Handle writeback of dirty data for the device backed by this bdi. Also 1149839a8e86STejun Heo * reschedules periodically and does kupdated style flushing. 115003ba3782SJens Axboe */ 1151f0054bb1STejun Heo void wb_workfn(struct work_struct *work) 115203ba3782SJens Axboe { 1153839a8e86STejun Heo struct bdi_writeback *wb = container_of(to_delayed_work(work), 1154839a8e86STejun Heo struct bdi_writeback, dwork); 115503ba3782SJens Axboe long pages_written; 115603ba3782SJens Axboe 1157f0054bb1STejun Heo set_worker_desc("flush-%s", dev_name(wb->bdi->dev)); 1158766f9164SPeter Zijlstra current->flags |= PF_SWAPWRITE; 115903ba3782SJens Axboe 1160839a8e86STejun Heo if (likely(!current_is_workqueue_rescuer() || 11614452226eSTejun Heo !test_bit(WB_registered, &wb->state))) { 116203ba3782SJens Axboe /* 1163f0054bb1STejun Heo * The normal path. Keep writing back @wb until its 1164839a8e86STejun Heo * work_list is empty. Note that this path is also taken 1165f0054bb1STejun Heo * if @wb is shutting down even when we're running off the 1166839a8e86STejun Heo * rescuer as work_list needs to be drained. 116703ba3782SJens Axboe */ 1168839a8e86STejun Heo do { 116925d130baSWanpeng Li pages_written = wb_do_writeback(wb); 1170455b2864SDave Chinner trace_writeback_pages_written(pages_written); 1171f0054bb1STejun Heo } while (!list_empty(&wb->work_list)); 1172839a8e86STejun Heo } else { 1173253c34e9SArtem Bityutskiy /* 1174839a8e86STejun Heo * bdi_wq can't get enough workers and we're running off 1175839a8e86STejun Heo * the emergency worker. Don't hog it. Hopefully, 1024 is 1176839a8e86STejun Heo * enough for efficient IO. 1177253c34e9SArtem Bityutskiy */ 1178f0054bb1STejun Heo pages_written = writeback_inodes_wb(wb, 1024, 1179839a8e86STejun Heo WB_REASON_FORKER_THREAD); 1180839a8e86STejun Heo trace_writeback_pages_written(pages_written); 118103ba3782SJens Axboe } 118203ba3782SJens Axboe 1183f0054bb1STejun Heo if (!list_empty(&wb->work_list)) 11846ca738d6SDerek Basehore mod_delayed_work(bdi_wq, &wb->dwork, 0); 11856ca738d6SDerek Basehore else if (wb_has_dirty_io(wb) && dirty_writeback_interval) 1186f0054bb1STejun Heo wb_wakeup_delayed(wb); 1187455b2864SDave Chinner 1188839a8e86STejun Heo current->flags &= ~PF_SWAPWRITE; 118903ba3782SJens Axboe } 119003ba3782SJens Axboe 119103ba3782SJens Axboe /* 119203ba3782SJens Axboe * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back 119303ba3782SJens Axboe * the whole world. 119403ba3782SJens Axboe */ 11950e175a18SCurt Wohlgemuth void wakeup_flusher_threads(long nr_pages, enum wb_reason reason) 119603ba3782SJens Axboe { 1197b8c2f347SChristoph Hellwig struct backing_dev_info *bdi; 1198b8c2f347SChristoph Hellwig 119947df3ddeSJan Kara if (!nr_pages) 120047df3ddeSJan Kara nr_pages = get_nr_dirty_pages(); 1201b8c2f347SChristoph Hellwig 1202b8c2f347SChristoph Hellwig rcu_read_lock(); 1203e7972912STejun Heo list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) 1204c00ddad3STejun Heo wb_start_writeback(&bdi->wb, nr_pages, false, reason); 1205b8c2f347SChristoph Hellwig rcu_read_unlock(); 120603ba3782SJens Axboe } 120703ba3782SJens Axboe 1208a2f48706STheodore Ts'o /* 1209a2f48706STheodore Ts'o * Wake up bdi's periodically to make sure dirtytime inodes gets 1210a2f48706STheodore Ts'o * written back periodically. We deliberately do *not* check the 1211a2f48706STheodore Ts'o * b_dirtytime list in wb_has_dirty_io(), since this would cause the 1212a2f48706STheodore Ts'o * kernel to be constantly waking up once there are any dirtytime 1213a2f48706STheodore Ts'o * inodes on the system. So instead we define a separate delayed work 1214a2f48706STheodore Ts'o * function which gets called much more rarely. (By default, only 1215a2f48706STheodore Ts'o * once every 12 hours.) 1216a2f48706STheodore Ts'o * 1217a2f48706STheodore Ts'o * If there is any other write activity going on in the file system, 1218a2f48706STheodore Ts'o * this function won't be necessary. But if the only thing that has 1219a2f48706STheodore Ts'o * happened on the file system is a dirtytime inode caused by an atime 1220a2f48706STheodore Ts'o * update, we need this infrastructure below to make sure that inode 1221a2f48706STheodore Ts'o * eventually gets pushed out to disk. 1222a2f48706STheodore Ts'o */ 1223a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w); 1224a2f48706STheodore Ts'o static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback); 1225a2f48706STheodore Ts'o 1226a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w) 1227a2f48706STheodore Ts'o { 1228a2f48706STheodore Ts'o struct backing_dev_info *bdi; 1229a2f48706STheodore Ts'o 1230a2f48706STheodore Ts'o rcu_read_lock(); 1231a2f48706STheodore Ts'o list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 1232a2f48706STheodore Ts'o if (list_empty(&bdi->wb.b_dirty_time)) 1233a2f48706STheodore Ts'o continue; 1234f0054bb1STejun Heo wb_wakeup(&bdi->wb); 1235a2f48706STheodore Ts'o } 1236a2f48706STheodore Ts'o rcu_read_unlock(); 1237a2f48706STheodore Ts'o schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ); 1238a2f48706STheodore Ts'o } 1239a2f48706STheodore Ts'o 1240a2f48706STheodore Ts'o static int __init start_dirtytime_writeback(void) 1241a2f48706STheodore Ts'o { 1242a2f48706STheodore Ts'o schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ); 1243a2f48706STheodore Ts'o return 0; 1244a2f48706STheodore Ts'o } 1245a2f48706STheodore Ts'o __initcall(start_dirtytime_writeback); 1246a2f48706STheodore Ts'o 12471efff914STheodore Ts'o int dirtytime_interval_handler(struct ctl_table *table, int write, 12481efff914STheodore Ts'o void __user *buffer, size_t *lenp, loff_t *ppos) 12491efff914STheodore Ts'o { 12501efff914STheodore Ts'o int ret; 12511efff914STheodore Ts'o 12521efff914STheodore Ts'o ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 12531efff914STheodore Ts'o if (ret == 0 && write) 12541efff914STheodore Ts'o mod_delayed_work(system_wq, &dirtytime_work, 0); 12551efff914STheodore Ts'o return ret; 12561efff914STheodore Ts'o } 12571efff914STheodore Ts'o 125803ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode) 125903ba3782SJens Axboe { 126003ba3782SJens Axboe if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { 126103ba3782SJens Axboe struct dentry *dentry; 126203ba3782SJens Axboe const char *name = "?"; 126303ba3782SJens Axboe 126403ba3782SJens Axboe dentry = d_find_alias(inode); 126503ba3782SJens Axboe if (dentry) { 126603ba3782SJens Axboe spin_lock(&dentry->d_lock); 126703ba3782SJens Axboe name = (const char *) dentry->d_name.name; 126803ba3782SJens Axboe } 126903ba3782SJens Axboe printk(KERN_DEBUG 127003ba3782SJens Axboe "%s(%d): dirtied inode %lu (%s) on %s\n", 127103ba3782SJens Axboe current->comm, task_pid_nr(current), inode->i_ino, 127203ba3782SJens Axboe name, inode->i_sb->s_id); 127303ba3782SJens Axboe if (dentry) { 127403ba3782SJens Axboe spin_unlock(&dentry->d_lock); 127503ba3782SJens Axboe dput(dentry); 127603ba3782SJens Axboe } 127703ba3782SJens Axboe } 127803ba3782SJens Axboe } 127903ba3782SJens Axboe 128003ba3782SJens Axboe /** 128103ba3782SJens Axboe * __mark_inode_dirty - internal function 128203ba3782SJens Axboe * @inode: inode to mark 128303ba3782SJens Axboe * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) 128403ba3782SJens Axboe * Mark an inode as dirty. Callers should use mark_inode_dirty or 128503ba3782SJens Axboe * mark_inode_dirty_sync. 128603ba3782SJens Axboe * 128703ba3782SJens Axboe * Put the inode on the super block's dirty list. 128803ba3782SJens Axboe * 128903ba3782SJens Axboe * CAREFUL! We mark it dirty unconditionally, but move it onto the 129003ba3782SJens Axboe * dirty list only if it is hashed or if it refers to a blockdev. 129103ba3782SJens Axboe * If it was not hashed, it will never be added to the dirty list 129203ba3782SJens Axboe * even if it is later hashed, as it will have been marked dirty already. 129303ba3782SJens Axboe * 129403ba3782SJens Axboe * In short, make sure you hash any inodes _before_ you start marking 129503ba3782SJens Axboe * them dirty. 129603ba3782SJens Axboe * 129703ba3782SJens Axboe * Note that for blockdevs, inode->dirtied_when represents the dirtying time of 129803ba3782SJens Axboe * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of 129903ba3782SJens Axboe * the kernel-internal blockdev inode represents the dirtying time of the 130003ba3782SJens Axboe * blockdev's pages. This is why for I_DIRTY_PAGES we always use 130103ba3782SJens Axboe * page->mapping->host, so the page-dirtying time is recorded in the internal 130203ba3782SJens Axboe * blockdev inode. 130303ba3782SJens Axboe */ 13040ae45f63STheodore Ts'o #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC) 130503ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags) 130603ba3782SJens Axboe { 130703ba3782SJens Axboe struct super_block *sb = inode->i_sb; 1308253c34e9SArtem Bityutskiy struct backing_dev_info *bdi = NULL; 13090ae45f63STheodore Ts'o int dirtytime; 13100ae45f63STheodore Ts'o 13110ae45f63STheodore Ts'o trace_writeback_mark_inode_dirty(inode, flags); 131203ba3782SJens Axboe 131303ba3782SJens Axboe /* 131403ba3782SJens Axboe * Don't do this for I_DIRTY_PAGES - that doesn't actually 131503ba3782SJens Axboe * dirty the inode itself 131603ba3782SJens Axboe */ 13170ae45f63STheodore Ts'o if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_TIME)) { 13189fb0a7daSTejun Heo trace_writeback_dirty_inode_start(inode, flags); 13199fb0a7daSTejun Heo 132003ba3782SJens Axboe if (sb->s_op->dirty_inode) 1321aa385729SChristoph Hellwig sb->s_op->dirty_inode(inode, flags); 13229fb0a7daSTejun Heo 13239fb0a7daSTejun Heo trace_writeback_dirty_inode(inode, flags); 132403ba3782SJens Axboe } 13250ae45f63STheodore Ts'o if (flags & I_DIRTY_INODE) 13260ae45f63STheodore Ts'o flags &= ~I_DIRTY_TIME; 13270ae45f63STheodore Ts'o dirtytime = flags & I_DIRTY_TIME; 132803ba3782SJens Axboe 132903ba3782SJens Axboe /* 13309c6ac78eSTejun Heo * Paired with smp_mb() in __writeback_single_inode() for the 13319c6ac78eSTejun Heo * following lockless i_state test. See there for details. 133203ba3782SJens Axboe */ 133303ba3782SJens Axboe smp_mb(); 133403ba3782SJens Axboe 13350ae45f63STheodore Ts'o if (((inode->i_state & flags) == flags) || 13360ae45f63STheodore Ts'o (dirtytime && (inode->i_state & I_DIRTY_INODE))) 133703ba3782SJens Axboe return; 133803ba3782SJens Axboe 133903ba3782SJens Axboe if (unlikely(block_dump)) 134003ba3782SJens Axboe block_dump___mark_inode_dirty(inode); 134103ba3782SJens Axboe 1342250df6edSDave Chinner spin_lock(&inode->i_lock); 13430ae45f63STheodore Ts'o if (dirtytime && (inode->i_state & I_DIRTY_INODE)) 13440ae45f63STheodore Ts'o goto out_unlock_inode; 134503ba3782SJens Axboe if ((inode->i_state & flags) != flags) { 134603ba3782SJens Axboe const int was_dirty = inode->i_state & I_DIRTY; 134703ba3782SJens Axboe 134852ebea74STejun Heo inode_attach_wb(inode, NULL); 134952ebea74STejun Heo 13500ae45f63STheodore Ts'o if (flags & I_DIRTY_INODE) 13510ae45f63STheodore Ts'o inode->i_state &= ~I_DIRTY_TIME; 135203ba3782SJens Axboe inode->i_state |= flags; 135303ba3782SJens Axboe 135403ba3782SJens Axboe /* 135503ba3782SJens Axboe * If the inode is being synced, just update its dirty state. 135603ba3782SJens Axboe * The unlocker will place the inode on the appropriate 135703ba3782SJens Axboe * superblock list, based upon its state. 135803ba3782SJens Axboe */ 135903ba3782SJens Axboe if (inode->i_state & I_SYNC) 1360250df6edSDave Chinner goto out_unlock_inode; 136103ba3782SJens Axboe 136203ba3782SJens Axboe /* 136303ba3782SJens Axboe * Only add valid (hashed) inodes to the superblock's 136403ba3782SJens Axboe * dirty list. Add blockdev inodes as well. 136503ba3782SJens Axboe */ 136603ba3782SJens Axboe if (!S_ISBLK(inode->i_mode)) { 13671d3382cbSAl Viro if (inode_unhashed(inode)) 1368250df6edSDave Chinner goto out_unlock_inode; 136903ba3782SJens Axboe } 1370a4ffdde6SAl Viro if (inode->i_state & I_FREEING) 1371250df6edSDave Chinner goto out_unlock_inode; 137203ba3782SJens Axboe 137303ba3782SJens Axboe /* 137403ba3782SJens Axboe * If the inode was already on b_dirty/b_io/b_more_io, don't 137503ba3782SJens Axboe * reposition it (that would break b_dirty time-ordering). 137603ba3782SJens Axboe */ 137703ba3782SJens Axboe if (!was_dirty) { 1378d6c10f1fSTejun Heo struct list_head *dirty_list; 1379a66979abSDave Chinner bool wakeup_bdi = false; 1380253c34e9SArtem Bityutskiy bdi = inode_to_bdi(inode); 1381500b067cSJens Axboe 1382146d7009SJunxiao Bi spin_unlock(&inode->i_lock); 1383146d7009SJunxiao Bi spin_lock(&bdi->wb.list_lock); 1384253c34e9SArtem Bityutskiy 1385d6c10f1fSTejun Heo WARN(bdi_cap_writeback_dirty(bdi) && 1386d6c10f1fSTejun Heo !test_bit(WB_registered, &bdi->wb.state), 1387d6c10f1fSTejun Heo "bdi-%s not registered\n", bdi->name); 138803ba3782SJens Axboe 138903ba3782SJens Axboe inode->dirtied_when = jiffies; 1390a2f48706STheodore Ts'o if (dirtytime) 1391a2f48706STheodore Ts'o inode->dirtied_time_when = jiffies; 1392d6c10f1fSTejun Heo 1393a2f48706STheodore Ts'o if (inode->i_state & (I_DIRTY_INODE | I_DIRTY_PAGES)) 1394d6c10f1fSTejun Heo dirty_list = &bdi->wb.b_dirty; 1395a2f48706STheodore Ts'o else 1396d6c10f1fSTejun Heo dirty_list = &bdi->wb.b_dirty_time; 1397d6c10f1fSTejun Heo 1398d6c10f1fSTejun Heo wakeup_bdi = inode_wb_list_move_locked(inode, &bdi->wb, 1399d6c10f1fSTejun Heo dirty_list); 1400d6c10f1fSTejun Heo 1401f758eeabSChristoph Hellwig spin_unlock(&bdi->wb.list_lock); 14020ae45f63STheodore Ts'o trace_writeback_dirty_inode_enqueue(inode); 1403253c34e9SArtem Bityutskiy 1404d6c10f1fSTejun Heo /* 1405d6c10f1fSTejun Heo * If this is the first dirty inode for this bdi, 1406d6c10f1fSTejun Heo * we have to wake-up the corresponding bdi thread 1407d6c10f1fSTejun Heo * to make sure background write-back happens 1408d6c10f1fSTejun Heo * later. 1409d6c10f1fSTejun Heo */ 1410d6c10f1fSTejun Heo if (bdi_cap_writeback_dirty(bdi) && wakeup_bdi) 1411f0054bb1STejun Heo wb_wakeup_delayed(&bdi->wb); 1412a66979abSDave Chinner return; 1413a66979abSDave Chinner } 1414a66979abSDave Chinner } 1415a66979abSDave Chinner out_unlock_inode: 1416a66979abSDave Chinner spin_unlock(&inode->i_lock); 1417a66979abSDave Chinner 141803ba3782SJens Axboe } 141903ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty); 142003ba3782SJens Axboe 1421b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb) 142266f3b8e2SJens Axboe { 142338f21977SNick Piggin struct inode *inode, *old_inode = NULL; 142438f21977SNick Piggin 142503ba3782SJens Axboe /* 142603ba3782SJens Axboe * We need to be protected against the filesystem going from 142703ba3782SJens Axboe * r/o to r/w or vice versa. 142803ba3782SJens Axboe */ 1429b6e51316SJens Axboe WARN_ON(!rwsem_is_locked(&sb->s_umount)); 143003ba3782SJens Axboe 143155fa6091SDave Chinner spin_lock(&inode_sb_list_lock); 143266f3b8e2SJens Axboe 143338f21977SNick Piggin /* 143438f21977SNick Piggin * Data integrity sync. Must wait for all pages under writeback, 143538f21977SNick Piggin * because there may have been pages dirtied before our sync 143638f21977SNick Piggin * call, but which had writeout started before we write it out. 143738f21977SNick Piggin * In which case, the inode may not be on the dirty list, but 143838f21977SNick Piggin * we still have to wait for that writeout. 143938f21977SNick Piggin */ 1440b6e51316SJens Axboe list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 1441250df6edSDave Chinner struct address_space *mapping = inode->i_mapping; 144238f21977SNick Piggin 1443250df6edSDave Chinner spin_lock(&inode->i_lock); 1444250df6edSDave Chinner if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) || 1445250df6edSDave Chinner (mapping->nrpages == 0)) { 1446250df6edSDave Chinner spin_unlock(&inode->i_lock); 144738f21977SNick Piggin continue; 1448250df6edSDave Chinner } 144938f21977SNick Piggin __iget(inode); 1450250df6edSDave Chinner spin_unlock(&inode->i_lock); 145155fa6091SDave Chinner spin_unlock(&inode_sb_list_lock); 145255fa6091SDave Chinner 145338f21977SNick Piggin /* 145455fa6091SDave Chinner * We hold a reference to 'inode' so it couldn't have been 145555fa6091SDave Chinner * removed from s_inodes list while we dropped the 145655fa6091SDave Chinner * inode_sb_list_lock. We cannot iput the inode now as we can 145755fa6091SDave Chinner * be holding the last reference and we cannot iput it under 145855fa6091SDave Chinner * inode_sb_list_lock. So we keep the reference and iput it 145955fa6091SDave Chinner * later. 146038f21977SNick Piggin */ 146138f21977SNick Piggin iput(old_inode); 146238f21977SNick Piggin old_inode = inode; 146338f21977SNick Piggin 146438f21977SNick Piggin filemap_fdatawait(mapping); 146538f21977SNick Piggin 146638f21977SNick Piggin cond_resched(); 146738f21977SNick Piggin 146855fa6091SDave Chinner spin_lock(&inode_sb_list_lock); 146938f21977SNick Piggin } 147055fa6091SDave Chinner spin_unlock(&inode_sb_list_lock); 147138f21977SNick Piggin iput(old_inode); 147266f3b8e2SJens Axboe } 14731da177e4SLinus Torvalds 1474d8a8559cSJens Axboe /** 14753259f8beSChris Mason * writeback_inodes_sb_nr - writeback dirty inodes from given super_block 1476d8a8559cSJens Axboe * @sb: the superblock 14773259f8beSChris Mason * @nr: the number of pages to write 1478786228abSMarcos Paulo de Souza * @reason: reason why some writeback work initiated 14791da177e4SLinus Torvalds * 1480d8a8559cSJens Axboe * Start writeback on some inodes on this super_block. No guarantees are made 1481d8a8559cSJens Axboe * on how many (if any) will be written, and this function does not wait 14823259f8beSChris Mason * for IO completion of submitted IO. 14831da177e4SLinus Torvalds */ 14840e175a18SCurt Wohlgemuth void writeback_inodes_sb_nr(struct super_block *sb, 14850e175a18SCurt Wohlgemuth unsigned long nr, 14860e175a18SCurt Wohlgemuth enum wb_reason reason) 14871da177e4SLinus Torvalds { 148883ba7b07SChristoph Hellwig DECLARE_COMPLETION_ONSTACK(done); 148983ba7b07SChristoph Hellwig struct wb_writeback_work work = { 14903c4d7165SChristoph Hellwig .sb = sb, 14913c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_NONE, 14926e6938b6SWu Fengguang .tagged_writepages = 1, 149383ba7b07SChristoph Hellwig .done = &done, 14943259f8beSChris Mason .nr_pages = nr, 14950e175a18SCurt Wohlgemuth .reason = reason, 14963c4d7165SChristoph Hellwig }; 1497e7972912STejun Heo struct backing_dev_info *bdi = sb->s_bdi; 14980e3c9a22SJens Axboe 1499e7972912STejun Heo if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info) 15006eedc701SJan Kara return; 1501cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 1502e7972912STejun Heo wb_queue_work(&bdi->wb, &work); 150383ba7b07SChristoph Hellwig wait_for_completion(&done); 15041da177e4SLinus Torvalds } 15053259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr); 15063259f8beSChris Mason 15073259f8beSChris Mason /** 15083259f8beSChris Mason * writeback_inodes_sb - writeback dirty inodes from given super_block 15093259f8beSChris Mason * @sb: the superblock 1510786228abSMarcos Paulo de Souza * @reason: reason why some writeback work was initiated 15113259f8beSChris Mason * 15123259f8beSChris Mason * Start writeback on some inodes on this super_block. No guarantees are made 15133259f8beSChris Mason * on how many (if any) will be written, and this function does not wait 15143259f8beSChris Mason * for IO completion of submitted IO. 15153259f8beSChris Mason */ 15160e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 15173259f8beSChris Mason { 15180e175a18SCurt Wohlgemuth return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); 15193259f8beSChris Mason } 1520d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb); 1521d8a8559cSJens Axboe 1522d8a8559cSJens Axboe /** 152310ee27a0SMiao Xie * try_to_writeback_inodes_sb_nr - try to start writeback if none underway 15243259f8beSChris Mason * @sb: the superblock 15253259f8beSChris Mason * @nr: the number of pages to write 152610ee27a0SMiao Xie * @reason: the reason of writeback 15273259f8beSChris Mason * 152810ee27a0SMiao Xie * Invoke writeback_inodes_sb_nr if no writeback is currently underway. 15293259f8beSChris Mason * Returns 1 if writeback was started, 0 if not. 15303259f8beSChris Mason */ 153110ee27a0SMiao Xie int try_to_writeback_inodes_sb_nr(struct super_block *sb, 15320e175a18SCurt Wohlgemuth unsigned long nr, 15330e175a18SCurt Wohlgemuth enum wb_reason reason) 15343259f8beSChris Mason { 153510ee27a0SMiao Xie if (writeback_in_progress(sb->s_bdi)) 153610ee27a0SMiao Xie return 1; 153710ee27a0SMiao Xie 153810ee27a0SMiao Xie if (!down_read_trylock(&sb->s_umount)) 153910ee27a0SMiao Xie return 0; 154010ee27a0SMiao Xie 15410e175a18SCurt Wohlgemuth writeback_inodes_sb_nr(sb, nr, reason); 15423259f8beSChris Mason up_read(&sb->s_umount); 15433259f8beSChris Mason return 1; 15443259f8beSChris Mason } 154510ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb_nr); 154610ee27a0SMiao Xie 154710ee27a0SMiao Xie /** 154810ee27a0SMiao Xie * try_to_writeback_inodes_sb - try to start writeback if none underway 154910ee27a0SMiao Xie * @sb: the superblock 155010ee27a0SMiao Xie * @reason: reason why some writeback work was initiated 155110ee27a0SMiao Xie * 155210ee27a0SMiao Xie * Implement by try_to_writeback_inodes_sb_nr() 155310ee27a0SMiao Xie * Returns 1 if writeback was started, 0 if not. 155410ee27a0SMiao Xie */ 155510ee27a0SMiao Xie int try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 155610ee27a0SMiao Xie { 155710ee27a0SMiao Xie return try_to_writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); 155810ee27a0SMiao Xie } 155910ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb); 15603259f8beSChris Mason 15613259f8beSChris Mason /** 1562d8a8559cSJens Axboe * sync_inodes_sb - sync sb inode pages 1563d8a8559cSJens Axboe * @sb: the superblock 1564d8a8559cSJens Axboe * 1565d8a8559cSJens Axboe * This function writes and waits on any dirty inode belonging to this 15660dc83bd3SJan Kara * super_block. 1567d8a8559cSJens Axboe */ 15680dc83bd3SJan Kara void sync_inodes_sb(struct super_block *sb) 1569d8a8559cSJens Axboe { 157083ba7b07SChristoph Hellwig DECLARE_COMPLETION_ONSTACK(done); 157183ba7b07SChristoph Hellwig struct wb_writeback_work work = { 15723c4d7165SChristoph Hellwig .sb = sb, 15733c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_ALL, 15743c4d7165SChristoph Hellwig .nr_pages = LONG_MAX, 15753c4d7165SChristoph Hellwig .range_cyclic = 0, 157683ba7b07SChristoph Hellwig .done = &done, 15770e175a18SCurt Wohlgemuth .reason = WB_REASON_SYNC, 15787747bd4bSDave Chinner .for_sync = 1, 15793c4d7165SChristoph Hellwig }; 1580e7972912STejun Heo struct backing_dev_info *bdi = sb->s_bdi; 15813c4d7165SChristoph Hellwig 15826eedc701SJan Kara /* Nothing to do? */ 1583e7972912STejun Heo if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info) 15846eedc701SJan Kara return; 1585cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 1586cf37e972SChristoph Hellwig 1587e7972912STejun Heo wb_queue_work(&bdi->wb, &work); 158883ba7b07SChristoph Hellwig wait_for_completion(&done); 158983ba7b07SChristoph Hellwig 1590b6e51316SJens Axboe wait_sb_inodes(sb); 1591d8a8559cSJens Axboe } 1592d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb); 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds /** 15951da177e4SLinus Torvalds * write_inode_now - write an inode to disk 15961da177e4SLinus Torvalds * @inode: inode to write to disk 15971da177e4SLinus Torvalds * @sync: whether the write should be synchronous or not 15981da177e4SLinus Torvalds * 15997f04c26dSAndrea Arcangeli * This function commits an inode to disk immediately if it is dirty. This is 16007f04c26dSAndrea Arcangeli * primarily needed by knfsd. 16017f04c26dSAndrea Arcangeli * 16027f04c26dSAndrea Arcangeli * The caller must either have a ref on the inode or must have set I_WILL_FREE. 16031da177e4SLinus Torvalds */ 16041da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync) 16051da177e4SLinus Torvalds { 1606f758eeabSChristoph Hellwig struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 16071da177e4SLinus Torvalds struct writeback_control wbc = { 16081da177e4SLinus Torvalds .nr_to_write = LONG_MAX, 160918914b18SMike Galbraith .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 1610111ebb6eSOGAWA Hirofumi .range_start = 0, 1611111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 16121da177e4SLinus Torvalds }; 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds if (!mapping_cap_writeback_dirty(inode->i_mapping)) 161549364ce2SAndrew Morton wbc.nr_to_write = 0; 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds might_sleep(); 16184f8ad655SJan Kara return writeback_single_inode(inode, wb, &wbc); 16191da177e4SLinus Torvalds } 16201da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now); 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds /** 16231da177e4SLinus Torvalds * sync_inode - write an inode and its pages to disk. 16241da177e4SLinus Torvalds * @inode: the inode to sync 16251da177e4SLinus Torvalds * @wbc: controls the writeback mode 16261da177e4SLinus Torvalds * 16271da177e4SLinus Torvalds * sync_inode() will write an inode and its pages to disk. It will also 16281da177e4SLinus Torvalds * correctly update the inode on its superblock's dirty inode lists and will 16291da177e4SLinus Torvalds * update inode->i_state. 16301da177e4SLinus Torvalds * 16311da177e4SLinus Torvalds * The caller must have a ref on the inode. 16321da177e4SLinus Torvalds */ 16331da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc) 16341da177e4SLinus Torvalds { 16354f8ad655SJan Kara return writeback_single_inode(inode, &inode_to_bdi(inode)->wb, wbc); 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode); 1638c3765016SChristoph Hellwig 1639c3765016SChristoph Hellwig /** 1640c691b9d9SAndrew Morton * sync_inode_metadata - write an inode to disk 1641c3765016SChristoph Hellwig * @inode: the inode to sync 1642c3765016SChristoph Hellwig * @wait: wait for I/O to complete. 1643c3765016SChristoph Hellwig * 1644c691b9d9SAndrew Morton * Write an inode to disk and adjust its dirty state after completion. 1645c3765016SChristoph Hellwig * 1646c3765016SChristoph Hellwig * Note: only writes the actual inode, no associated data or other metadata. 1647c3765016SChristoph Hellwig */ 1648c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait) 1649c3765016SChristoph Hellwig { 1650c3765016SChristoph Hellwig struct writeback_control wbc = { 1651c3765016SChristoph Hellwig .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, 1652c3765016SChristoph Hellwig .nr_to_write = 0, /* metadata-only */ 1653c3765016SChristoph Hellwig }; 1654c3765016SChristoph Hellwig 1655c3765016SChristoph Hellwig return sync_inode(inode, &wbc); 1656c3765016SChristoph Hellwig } 1657c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata); 1658