11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * fs/fs-writeback.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2002, Linus Torvalds. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Contains all the functions related to writing back and waiting 71da177e4SLinus Torvalds * upon dirty inodes against superblocks, and writing back dirty 81da177e4SLinus Torvalds * pages against inodes. ie: data writeback. Writeout of the 91da177e4SLinus Torvalds * inode itself is not handled here. 101da177e4SLinus Torvalds * 11e1f8e874SFrancois Cami * 10Apr2002 Andrew Morton 121da177e4SLinus Torvalds * Split out of fs/inode.c 131da177e4SLinus Torvalds * Additions for address_space-based writeback 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/kernel.h> 17f5ff8422SJens Axboe #include <linux/module.h> 181da177e4SLinus Torvalds #include <linux/spinlock.h> 195a0e3ad6STejun Heo #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/sched.h> 211da177e4SLinus Torvalds #include <linux/fs.h> 221da177e4SLinus Torvalds #include <linux/mm.h> 2303ba3782SJens Axboe #include <linux/kthread.h> 2403ba3782SJens Axboe #include <linux/freezer.h> 251da177e4SLinus Torvalds #include <linux/writeback.h> 261da177e4SLinus Torvalds #include <linux/blkdev.h> 271da177e4SLinus Torvalds #include <linux/backing-dev.h> 281da177e4SLinus Torvalds #include <linux/buffer_head.h> 29455b2864SDave Chinner #include <linux/tracepoint.h> 3007f3f05cSDavid Howells #include "internal.h" 311da177e4SLinus Torvalds 32d0bceac7SJens Axboe /* 33d46db3d5SWu Fengguang * The maximum number of pages to writeout in a single bdi flush/kupdate 34d46db3d5SWu Fengguang * operation. We do this so we don't hold I_SYNC against an inode for 35d46db3d5SWu Fengguang * enormous amounts of time, which would block a userspace task which has 36d46db3d5SWu Fengguang * been forced to throttle against that inode. Also, the code reevaluates 37d46db3d5SWu Fengguang * the dirty each time it has written this many pages. 38d46db3d5SWu Fengguang */ 39d46db3d5SWu Fengguang #define MAX_WRITEBACK_PAGES 1024L 40d46db3d5SWu Fengguang 41d46db3d5SWu Fengguang /* 42c4a77a6cSJens Axboe * Passed into wb_writeback(), essentially a subset of writeback_control 43c4a77a6cSJens Axboe */ 4483ba7b07SChristoph Hellwig struct wb_writeback_work { 45c4a77a6cSJens Axboe long nr_pages; 46c4a77a6cSJens Axboe struct super_block *sb; 47d46db3d5SWu Fengguang unsigned long *older_than_this; 48c4a77a6cSJens Axboe enum writeback_sync_modes sync_mode; 496e6938b6SWu Fengguang unsigned int tagged_writepages:1; 5052957fe1SH Hartley Sweeten unsigned int for_kupdate:1; 5152957fe1SH Hartley Sweeten unsigned int range_cyclic:1; 5252957fe1SH Hartley Sweeten unsigned int for_background:1; 53c4a77a6cSJens Axboe 548010c3b6SJens Axboe struct list_head list; /* pending work list */ 5583ba7b07SChristoph Hellwig struct completion *done; /* set if the caller waits */ 5603ba3782SJens Axboe }; 5703ba3782SJens Axboe 58455b2864SDave Chinner /* 59455b2864SDave Chinner * Include the creation of the trace points after defining the 60455b2864SDave Chinner * wb_writeback_work structure so that the definition remains local to this 61455b2864SDave Chinner * file. 62455b2864SDave Chinner */ 63455b2864SDave Chinner #define CREATE_TRACE_POINTS 64455b2864SDave Chinner #include <trace/events/writeback.h> 65455b2864SDave Chinner 66455b2864SDave Chinner /* 67455b2864SDave Chinner * We don't actually have pdflush, but this one is exported though /proc... 68455b2864SDave Chinner */ 69455b2864SDave Chinner int nr_pdflush_threads; 70455b2864SDave Chinner 71f11b00f3SAdrian Bunk /** 72f11b00f3SAdrian Bunk * writeback_in_progress - determine whether there is writeback in progress 73f11b00f3SAdrian Bunk * @bdi: the device's backing_dev_info structure. 74f11b00f3SAdrian Bunk * 7503ba3782SJens Axboe * Determine whether there is writeback waiting to be handled against a 7603ba3782SJens Axboe * backing device. 77f11b00f3SAdrian Bunk */ 78f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi) 79f11b00f3SAdrian Bunk { 8081d73a32SJan Kara return test_bit(BDI_writeback_running, &bdi->state); 81f11b00f3SAdrian Bunk } 82f11b00f3SAdrian Bunk 83692ebd17SJan Kara static inline struct backing_dev_info *inode_to_bdi(struct inode *inode) 84692ebd17SJan Kara { 85692ebd17SJan Kara struct super_block *sb = inode->i_sb; 86692ebd17SJan Kara 87aaead25bSChristoph Hellwig if (strcmp(sb->s_type->name, "bdev") == 0) 88aaead25bSChristoph Hellwig return inode->i_mapping->backing_dev_info; 89aaead25bSChristoph Hellwig 90692ebd17SJan Kara return sb->s_bdi; 91692ebd17SJan Kara } 92692ebd17SJan Kara 937ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head) 947ccf19a8SNick Piggin { 957ccf19a8SNick Piggin return list_entry(head, struct inode, i_wb_list); 967ccf19a8SNick Piggin } 977ccf19a8SNick Piggin 986585027aSJan Kara /* Wakeup flusher thread or forker thread to fork it. Requires bdi->wb_lock. */ 996585027aSJan Kara static void bdi_wakeup_flusher(struct backing_dev_info *bdi) 1004195f73dSNick Piggin { 101fff5b85aSArtem Bityutskiy if (bdi->wb.task) { 102fff5b85aSArtem Bityutskiy wake_up_process(bdi->wb.task); 103fff5b85aSArtem Bityutskiy } else { 1041da177e4SLinus Torvalds /* 105fff5b85aSArtem Bityutskiy * The bdi thread isn't there, wake up the forker thread which 106fff5b85aSArtem Bityutskiy * will create and run it. 1071da177e4SLinus Torvalds */ 10803ba3782SJens Axboe wake_up_process(default_backing_dev_info.wb.task); 1091da177e4SLinus Torvalds } 1106585027aSJan Kara } 1116585027aSJan Kara 1126585027aSJan Kara static void bdi_queue_work(struct backing_dev_info *bdi, 1136585027aSJan Kara struct wb_writeback_work *work) 1146585027aSJan Kara { 1156585027aSJan Kara trace_writeback_queue(bdi, work); 1166585027aSJan Kara 1176585027aSJan Kara spin_lock_bh(&bdi->wb_lock); 1186585027aSJan Kara list_add_tail(&work->list, &bdi->work_list); 1196585027aSJan Kara if (!bdi->wb.task) 1206585027aSJan Kara trace_writeback_nothread(bdi, work); 1216585027aSJan Kara bdi_wakeup_flusher(bdi); 1226467716aSArtem Bityutskiy spin_unlock_bh(&bdi->wb_lock); 12303ba3782SJens Axboe } 1241da177e4SLinus Torvalds 12583ba7b07SChristoph Hellwig static void 12683ba7b07SChristoph Hellwig __bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages, 1276585027aSJan Kara bool range_cyclic) 1281da177e4SLinus Torvalds { 12983ba7b07SChristoph Hellwig struct wb_writeback_work *work; 13003ba3782SJens Axboe 131bcddc3f0SJens Axboe /* 132bcddc3f0SJens Axboe * This is WB_SYNC_NONE writeback, so if allocation fails just 133bcddc3f0SJens Axboe * wakeup the thread for old dirty data writeback 134bcddc3f0SJens Axboe */ 13583ba7b07SChristoph Hellwig work = kzalloc(sizeof(*work), GFP_ATOMIC); 13683ba7b07SChristoph Hellwig if (!work) { 137455b2864SDave Chinner if (bdi->wb.task) { 138455b2864SDave Chinner trace_writeback_nowork(bdi); 13983ba7b07SChristoph Hellwig wake_up_process(bdi->wb.task); 140455b2864SDave Chinner } 14183ba7b07SChristoph Hellwig return; 14283ba7b07SChristoph Hellwig } 14383ba7b07SChristoph Hellwig 14483ba7b07SChristoph Hellwig work->sync_mode = WB_SYNC_NONE; 14583ba7b07SChristoph Hellwig work->nr_pages = nr_pages; 14683ba7b07SChristoph Hellwig work->range_cyclic = range_cyclic; 14783ba7b07SChristoph Hellwig 148f11fcae8SJens Axboe bdi_queue_work(bdi, work); 14903ba3782SJens Axboe } 150b6e51316SJens Axboe 151b6e51316SJens Axboe /** 152b6e51316SJens Axboe * bdi_start_writeback - start writeback 153b6e51316SJens Axboe * @bdi: the backing device to write from 154b6e51316SJens Axboe * @nr_pages: the number of pages to write 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 */ 162c5444198SChristoph Hellwig void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages) 163b6e51316SJens Axboe { 1646585027aSJan Kara __bdi_start_writeback(bdi, nr_pages, true); 165d3ddec76SWu Fengguang } 166d3ddec76SWu Fengguang 167c5444198SChristoph Hellwig /** 168c5444198SChristoph Hellwig * bdi_start_background_writeback - start background writeback 169c5444198SChristoph Hellwig * @bdi: the backing device to write from 170c5444198SChristoph Hellwig * 171c5444198SChristoph Hellwig * Description: 1726585027aSJan Kara * This makes sure WB_SYNC_NONE background writeback happens. When 1736585027aSJan Kara * this function returns, it is only guaranteed that for given BDI 1746585027aSJan Kara * some IO is happening if we are over background dirty threshold. 1756585027aSJan Kara * Caller need not hold sb s_umount semaphore. 176c5444198SChristoph Hellwig */ 177c5444198SChristoph Hellwig void bdi_start_background_writeback(struct backing_dev_info *bdi) 178c5444198SChristoph Hellwig { 1796585027aSJan Kara /* 1806585027aSJan Kara * We just wake up the flusher thread. It will perform background 1816585027aSJan Kara * writeback as soon as there is no other work to do. 1826585027aSJan Kara */ 18371927e84SWu Fengguang trace_writeback_wake_background(bdi); 1846585027aSJan Kara spin_lock_bh(&bdi->wb_lock); 1856585027aSJan Kara bdi_wakeup_flusher(bdi); 1866585027aSJan Kara spin_unlock_bh(&bdi->wb_lock); 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /* 190a66979abSDave Chinner * Remove the inode from the writeback list it is on. 191a66979abSDave Chinner */ 192a66979abSDave Chinner void inode_wb_list_del(struct inode *inode) 193a66979abSDave Chinner { 194f758eeabSChristoph Hellwig struct backing_dev_info *bdi = inode_to_bdi(inode); 195a66979abSDave Chinner 196f758eeabSChristoph Hellwig spin_lock(&bdi->wb.list_lock); 197f758eeabSChristoph Hellwig list_del_init(&inode->i_wb_list); 198f758eeabSChristoph Hellwig spin_unlock(&bdi->wb.list_lock); 199f758eeabSChristoph Hellwig } 200a66979abSDave Chinner 201a66979abSDave Chinner /* 2026610a0bcSAndrew Morton * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 2036610a0bcSAndrew Morton * furthest end of its superblock's dirty-inode list. 2046610a0bcSAndrew Morton * 2056610a0bcSAndrew Morton * Before stamping the inode's ->dirtied_when, we check to see whether it is 20666f3b8e2SJens Axboe * already the most-recently-dirtied inode on the b_dirty list. If that is 2076610a0bcSAndrew Morton * the case then the inode must have been redirtied while it was being written 2086610a0bcSAndrew Morton * out and we don't reset its dirtied_when. 2096610a0bcSAndrew Morton */ 210f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb) 2116610a0bcSAndrew Morton { 212f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 21303ba3782SJens Axboe if (!list_empty(&wb->b_dirty)) { 21466f3b8e2SJens Axboe struct inode *tail; 2156610a0bcSAndrew Morton 2167ccf19a8SNick Piggin tail = wb_inode(wb->b_dirty.next); 21766f3b8e2SJens Axboe if (time_before(inode->dirtied_when, tail->dirtied_when)) 2186610a0bcSAndrew Morton inode->dirtied_when = jiffies; 2196610a0bcSAndrew Morton } 2207ccf19a8SNick Piggin list_move(&inode->i_wb_list, &wb->b_dirty); 2216610a0bcSAndrew Morton } 2226610a0bcSAndrew Morton 2236610a0bcSAndrew Morton /* 22466f3b8e2SJens Axboe * requeue inode for re-scanning after bdi->b_io list is exhausted. 225c986d1e2SAndrew Morton */ 226f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb) 227c986d1e2SAndrew Morton { 228f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 2297ccf19a8SNick Piggin list_move(&inode->i_wb_list, &wb->b_more_io); 230c986d1e2SAndrew Morton } 231c986d1e2SAndrew Morton 2321c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode) 2331c0eeaf5SJoern Engel { 2341c0eeaf5SJoern Engel /* 235a66979abSDave Chinner * Prevent speculative execution through 236f758eeabSChristoph Hellwig * spin_unlock(&wb->list_lock); 2371c0eeaf5SJoern Engel */ 238a66979abSDave Chinner 2391c0eeaf5SJoern Engel smp_mb(); 2401c0eeaf5SJoern Engel wake_up_bit(&inode->i_state, __I_SYNC); 2411c0eeaf5SJoern Engel } 2421c0eeaf5SJoern Engel 243d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t) 244d2caa3c5SJeff Layton { 245d2caa3c5SJeff Layton bool ret = time_after(inode->dirtied_when, t); 246d2caa3c5SJeff Layton #ifndef CONFIG_64BIT 247d2caa3c5SJeff Layton /* 248d2caa3c5SJeff Layton * For inodes being constantly redirtied, dirtied_when can get stuck. 249d2caa3c5SJeff Layton * It _appears_ to be in the future, but is actually in distant past. 250d2caa3c5SJeff Layton * This test is necessary to prevent such wrapped-around relative times 2515b0830cbSJens Axboe * from permanently stopping the whole bdi writeback. 252d2caa3c5SJeff Layton */ 253d2caa3c5SJeff Layton ret = ret && time_before_eq(inode->dirtied_when, jiffies); 254d2caa3c5SJeff Layton #endif 255d2caa3c5SJeff Layton return ret; 256d2caa3c5SJeff Layton } 257d2caa3c5SJeff Layton 258c986d1e2SAndrew Morton /* 2592c136579SFengguang Wu * Move expired dirty inodes from @delaying_queue to @dispatch_queue. 2602c136579SFengguang Wu */ 261e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue, 2622c136579SFengguang Wu struct list_head *dispatch_queue, 2632c136579SFengguang Wu unsigned long *older_than_this) 2642c136579SFengguang Wu { 2655c03449dSShaohua Li LIST_HEAD(tmp); 2665c03449dSShaohua Li struct list_head *pos, *node; 267cf137307SJens Axboe struct super_block *sb = NULL; 2685c03449dSShaohua Li struct inode *inode; 269cf137307SJens Axboe int do_sb_sort = 0; 270e84d0a4fSWu Fengguang int moved = 0; 2715c03449dSShaohua Li 2722c136579SFengguang Wu while (!list_empty(delaying_queue)) { 2737ccf19a8SNick Piggin inode = wb_inode(delaying_queue->prev); 2742c136579SFengguang Wu if (older_than_this && 275d2caa3c5SJeff Layton inode_dirtied_after(inode, *older_than_this)) 2762c136579SFengguang Wu break; 277cf137307SJens Axboe if (sb && sb != inode->i_sb) 278cf137307SJens Axboe do_sb_sort = 1; 279cf137307SJens Axboe sb = inode->i_sb; 2807ccf19a8SNick Piggin list_move(&inode->i_wb_list, &tmp); 281e84d0a4fSWu Fengguang moved++; 2825c03449dSShaohua Li } 2835c03449dSShaohua Li 284cf137307SJens Axboe /* just one sb in list, splice to dispatch_queue and we're done */ 285cf137307SJens Axboe if (!do_sb_sort) { 286cf137307SJens Axboe list_splice(&tmp, dispatch_queue); 287e84d0a4fSWu Fengguang goto out; 288cf137307SJens Axboe } 289cf137307SJens Axboe 2905c03449dSShaohua Li /* Move inodes from one superblock together */ 2915c03449dSShaohua Li while (!list_empty(&tmp)) { 2927ccf19a8SNick Piggin sb = wb_inode(tmp.prev)->i_sb; 2935c03449dSShaohua Li list_for_each_prev_safe(pos, node, &tmp) { 2947ccf19a8SNick Piggin inode = wb_inode(pos); 2955c03449dSShaohua Li if (inode->i_sb == sb) 2967ccf19a8SNick Piggin list_move(&inode->i_wb_list, dispatch_queue); 2972c136579SFengguang Wu } 2982c136579SFengguang Wu } 299e84d0a4fSWu Fengguang out: 300e84d0a4fSWu Fengguang return moved; 3015c03449dSShaohua Li } 3022c136579SFengguang Wu 3032c136579SFengguang Wu /* 3042c136579SFengguang Wu * Queue all expired dirty inodes for io, eldest first. 3054ea879b9SWu Fengguang * Before 3064ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 3074ea879b9SWu Fengguang * =============> gf edc BA 3084ea879b9SWu Fengguang * After 3094ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 3104ea879b9SWu Fengguang * =============> g fBAedc 3114ea879b9SWu Fengguang * | 3124ea879b9SWu Fengguang * +--> dequeue for IO 3132c136579SFengguang Wu */ 31403ba3782SJens Axboe static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this) 3152c136579SFengguang Wu { 316e84d0a4fSWu Fengguang int moved; 317f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 3184ea879b9SWu Fengguang list_splice_init(&wb->b_more_io, &wb->b_io); 319e84d0a4fSWu Fengguang moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this); 320e84d0a4fSWu Fengguang trace_writeback_queue_io(wb, older_than_this, moved); 32166f3b8e2SJens Axboe } 32266f3b8e2SJens Axboe 323a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc) 32466f3b8e2SJens Axboe { 32503ba3782SJens Axboe if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) 326a9185b41SChristoph Hellwig return inode->i_sb->s_op->write_inode(inode, wbc); 32703ba3782SJens Axboe return 0; 32866f3b8e2SJens Axboe } 32908d8e974SFengguang Wu 3302c136579SFengguang Wu /* 33101c03194SChristoph Hellwig * Wait for writeback on an inode to complete. 33201c03194SChristoph Hellwig */ 333f758eeabSChristoph Hellwig static void inode_wait_for_writeback(struct inode *inode, 334f758eeabSChristoph Hellwig struct bdi_writeback *wb) 33501c03194SChristoph Hellwig { 33601c03194SChristoph Hellwig DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); 33701c03194SChristoph Hellwig wait_queue_head_t *wqh; 33801c03194SChristoph Hellwig 33901c03194SChristoph Hellwig wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 34058a9d3d8SRichard Kennedy while (inode->i_state & I_SYNC) { 341250df6edSDave Chinner spin_unlock(&inode->i_lock); 342f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 34301c03194SChristoph Hellwig __wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE); 344f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 345250df6edSDave Chinner spin_lock(&inode->i_lock); 34658a9d3d8SRichard Kennedy } 34701c03194SChristoph Hellwig } 34801c03194SChristoph Hellwig 34901c03194SChristoph Hellwig /* 350f758eeabSChristoph Hellwig * Write out an inode's dirty pages. Called under wb->list_lock and 3510f1b1fd8SDave Chinner * inode->i_lock. Either the caller has an active reference on the inode or 3520f1b1fd8SDave Chinner * the inode has I_WILL_FREE set. 35301c03194SChristoph Hellwig * 3541da177e4SLinus Torvalds * If `wait' is set, wait on the writeout. 3551da177e4SLinus Torvalds * 3561da177e4SLinus Torvalds * The whole writeout design is quite complex and fragile. We want to avoid 3571da177e4SLinus Torvalds * starvation of particular inodes when others are being redirtied, prevent 3581da177e4SLinus Torvalds * livelocks, etc. 3591da177e4SLinus Torvalds */ 3601da177e4SLinus Torvalds static int 361f758eeabSChristoph Hellwig writeback_single_inode(struct inode *inode, struct bdi_writeback *wb, 362f758eeabSChristoph Hellwig struct writeback_control *wbc) 3631da177e4SLinus Torvalds { 3641da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 365251d6a47SWu Fengguang long nr_to_write = wbc->nr_to_write; 36601c03194SChristoph Hellwig unsigned dirty; 3671da177e4SLinus Torvalds int ret; 3681da177e4SLinus Torvalds 369f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 3700f1b1fd8SDave Chinner assert_spin_locked(&inode->i_lock); 3710f1b1fd8SDave Chinner 37201c03194SChristoph Hellwig if (!atomic_read(&inode->i_count)) 37301c03194SChristoph Hellwig WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); 37401c03194SChristoph Hellwig else 37501c03194SChristoph Hellwig WARN_ON(inode->i_state & I_WILL_FREE); 37601c03194SChristoph Hellwig 37701c03194SChristoph Hellwig if (inode->i_state & I_SYNC) { 37801c03194SChristoph Hellwig /* 37901c03194SChristoph Hellwig * If this inode is locked for writeback and we are not doing 38066f3b8e2SJens Axboe * writeback-for-data-integrity, move it to b_more_io so that 38101c03194SChristoph Hellwig * writeback can proceed with the other inodes on s_io. 38201c03194SChristoph Hellwig * 38301c03194SChristoph Hellwig * We'll have another go at writing back this inode when we 38466f3b8e2SJens Axboe * completed a full scan of b_io. 38501c03194SChristoph Hellwig */ 386a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) { 387f758eeabSChristoph Hellwig requeue_io(inode, wb); 388251d6a47SWu Fengguang trace_writeback_single_inode_requeue(inode, wbc, 389251d6a47SWu Fengguang nr_to_write); 39001c03194SChristoph Hellwig return 0; 39101c03194SChristoph Hellwig } 39201c03194SChristoph Hellwig 39301c03194SChristoph Hellwig /* 39401c03194SChristoph Hellwig * It's a data-integrity sync. We must wait. 39501c03194SChristoph Hellwig */ 396f758eeabSChristoph Hellwig inode_wait_for_writeback(inode, wb); 39701c03194SChristoph Hellwig } 39801c03194SChristoph Hellwig 3991c0eeaf5SJoern Engel BUG_ON(inode->i_state & I_SYNC); 4001da177e4SLinus Torvalds 4015547e8aaSDmitry Monakhov /* Set I_SYNC, reset I_DIRTY_PAGES */ 4021c0eeaf5SJoern Engel inode->i_state |= I_SYNC; 4035547e8aaSDmitry Monakhov inode->i_state &= ~I_DIRTY_PAGES; 404250df6edSDave Chinner spin_unlock(&inode->i_lock); 405f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds ret = do_writepages(mapping, wbc); 4081da177e4SLinus Torvalds 40926821ed4SChristoph Hellwig /* 41026821ed4SChristoph Hellwig * Make sure to wait on the data before writing out the metadata. 41126821ed4SChristoph Hellwig * This is important for filesystems that modify metadata on data 41226821ed4SChristoph Hellwig * I/O completion. 41326821ed4SChristoph Hellwig */ 414a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) { 41526821ed4SChristoph Hellwig int err = filemap_fdatawait(mapping); 4161da177e4SLinus Torvalds if (ret == 0) 4171da177e4SLinus Torvalds ret = err; 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds 4205547e8aaSDmitry Monakhov /* 4215547e8aaSDmitry Monakhov * Some filesystems may redirty the inode during the writeback 4225547e8aaSDmitry Monakhov * due to delalloc, clear dirty metadata flags right before 4235547e8aaSDmitry Monakhov * write_inode() 4245547e8aaSDmitry Monakhov */ 425250df6edSDave Chinner spin_lock(&inode->i_lock); 4265547e8aaSDmitry Monakhov dirty = inode->i_state & I_DIRTY; 4275547e8aaSDmitry Monakhov inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC); 428250df6edSDave Chinner spin_unlock(&inode->i_lock); 42926821ed4SChristoph Hellwig /* Don't write the inode if only I_DIRTY_PAGES was set */ 43026821ed4SChristoph Hellwig if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { 431a9185b41SChristoph Hellwig int err = write_inode(inode, wbc); 4321da177e4SLinus Torvalds if (ret == 0) 4331da177e4SLinus Torvalds ret = err; 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 436f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 437250df6edSDave Chinner spin_lock(&inode->i_lock); 4381c0eeaf5SJoern Engel inode->i_state &= ~I_SYNC; 439a4ffdde6SAl Viro if (!(inode->i_state & I_FREEING)) { 44094c3dcbbSWu Fengguang /* 44194c3dcbbSWu Fengguang * Sync livelock prevention. Each inode is tagged and synced in 44294c3dcbbSWu Fengguang * one shot. If still dirty, it will be redirty_tail()'ed below. 44394c3dcbbSWu Fengguang * Update the dirty time to prevent enqueue and sync it again. 44494c3dcbbSWu Fengguang */ 44594c3dcbbSWu Fengguang if ((inode->i_state & I_DIRTY) && 44694c3dcbbSWu Fengguang (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)) 44794c3dcbbSWu Fengguang inode->dirtied_when = jiffies; 44894c3dcbbSWu Fengguang 44923539afcSWu Fengguang if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 4501da177e4SLinus Torvalds /* 4511da177e4SLinus Torvalds * We didn't write back all the pages. nfs_writepages() 452a50aeb40SWu Fengguang * sometimes bales out without doing anything. 4531da177e4SLinus Torvalds */ 4541da177e4SLinus Torvalds inode->i_state |= I_DIRTY_PAGES; 4558bc3be27SFengguang Wu if (wbc->nr_to_write <= 0) { 4568bc3be27SFengguang Wu /* 4578bc3be27SFengguang Wu * slice used up: queue for next turn 4588bc3be27SFengguang Wu */ 459f758eeabSChristoph Hellwig requeue_io(inode, wb); 4601da177e4SLinus Torvalds } else { 4611da177e4SLinus Torvalds /* 462a50aeb40SWu Fengguang * Writeback blocked by something other than 463a50aeb40SWu Fengguang * congestion. Delay the inode for some time to 464a50aeb40SWu Fengguang * avoid spinning on the CPU (100% iowait) 465a50aeb40SWu Fengguang * retrying writeback of the dirty page/inode 466a50aeb40SWu Fengguang * that cannot be performed immediately. 4678bc3be27SFengguang Wu */ 468f758eeabSChristoph Hellwig redirty_tail(inode, wb); 4698bc3be27SFengguang Wu } 47023539afcSWu Fengguang } else if (inode->i_state & I_DIRTY) { 47123539afcSWu Fengguang /* 47223539afcSWu Fengguang * Filesystems can dirty the inode during writeback 47323539afcSWu Fengguang * operations, such as delayed allocation during 47423539afcSWu Fengguang * submission or metadata updates after data IO 47523539afcSWu Fengguang * completion. 47623539afcSWu Fengguang */ 477f758eeabSChristoph Hellwig redirty_tail(inode, wb); 4781da177e4SLinus Torvalds } else { 4791da177e4SLinus Torvalds /* 4809e38d86fSNick Piggin * The inode is clean. At this point we either have 4819e38d86fSNick Piggin * a reference to the inode or it's on it's way out. 4829e38d86fSNick Piggin * No need to add it back to the LRU. 4831da177e4SLinus Torvalds */ 4847ccf19a8SNick Piggin list_del_init(&inode->i_wb_list); 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds } 4871c0eeaf5SJoern Engel inode_sync_complete(inode); 488251d6a47SWu Fengguang trace_writeback_single_inode(inode, wbc, nr_to_write); 4891da177e4SLinus Torvalds return ret; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 49203ba3782SJens Axboe /* 493d19de7edSChristoph Hellwig * For background writeback the caller does not have the sb pinned 49403ba3782SJens Axboe * before calling writeback. So make sure that we do pin it, so it doesn't 49503ba3782SJens Axboe * go away while we are writing inodes from it. 49603ba3782SJens Axboe */ 497d19de7edSChristoph Hellwig static bool pin_sb_for_writeback(struct super_block *sb) 4981da177e4SLinus Torvalds { 49903ba3782SJens Axboe spin_lock(&sb_lock); 50029cb4859SChristoph Hellwig if (list_empty(&sb->s_instances)) { 50103ba3782SJens Axboe spin_unlock(&sb_lock); 50229cb4859SChristoph Hellwig return false; 50303ba3782SJens Axboe } 50429cb4859SChristoph Hellwig 50529cb4859SChristoph Hellwig sb->s_count++; 50629cb4859SChristoph Hellwig spin_unlock(&sb_lock); 50729cb4859SChristoph Hellwig 50829cb4859SChristoph Hellwig if (down_read_trylock(&sb->s_umount)) { 50929cb4859SChristoph Hellwig if (sb->s_root) 51029cb4859SChristoph Hellwig return true; 51103ba3782SJens Axboe up_read(&sb->s_umount); 51203ba3782SJens Axboe } 51329cb4859SChristoph Hellwig 51429cb4859SChristoph Hellwig put_super(sb); 515d19de7edSChristoph Hellwig return false; 51603ba3782SJens Axboe } 51703ba3782SJens Axboe 518d46db3d5SWu Fengguang static long writeback_chunk_size(struct wb_writeback_work *work) 519d46db3d5SWu Fengguang { 520d46db3d5SWu Fengguang long pages; 521d46db3d5SWu Fengguang 522d46db3d5SWu Fengguang /* 523d46db3d5SWu Fengguang * WB_SYNC_ALL mode does livelock avoidance by syncing dirty 524d46db3d5SWu Fengguang * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX 525d46db3d5SWu Fengguang * here avoids calling into writeback_inodes_wb() more than once. 526d46db3d5SWu Fengguang * 527d46db3d5SWu Fengguang * The intended call sequence for WB_SYNC_ALL writeback is: 528d46db3d5SWu Fengguang * 529d46db3d5SWu Fengguang * wb_writeback() 530d46db3d5SWu Fengguang * writeback_sb_inodes() <== called only once 531d46db3d5SWu Fengguang * write_cache_pages() <== called once for each inode 532d46db3d5SWu Fengguang * (quickly) tag currently dirty pages 533d46db3d5SWu Fengguang * (maybe slowly) sync all tagged pages 534d46db3d5SWu Fengguang */ 535d46db3d5SWu Fengguang if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages) 536d46db3d5SWu Fengguang pages = LONG_MAX; 537d46db3d5SWu Fengguang else 538d46db3d5SWu Fengguang pages = min(MAX_WRITEBACK_PAGES, work->nr_pages); 539d46db3d5SWu Fengguang 540d46db3d5SWu Fengguang return pages; 541d46db3d5SWu Fengguang } 542d46db3d5SWu Fengguang 543f11c9c5cSEdward Shishkin /* 544f11c9c5cSEdward Shishkin * Write a portion of b_io inodes which belong to @sb. 545edadfb10SChristoph Hellwig * 546edadfb10SChristoph Hellwig * If @only_this_sb is true, then find and write all such 547f11c9c5cSEdward Shishkin * inodes. Otherwise write only ones which go sequentially 548f11c9c5cSEdward Shishkin * in reverse order. 549edadfb10SChristoph Hellwig * 550d46db3d5SWu Fengguang * Return the number of pages and/or inodes written. 551f11c9c5cSEdward Shishkin */ 552d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb, 553d46db3d5SWu Fengguang struct bdi_writeback *wb, 554d46db3d5SWu Fengguang struct wb_writeback_work *work) 55503ba3782SJens Axboe { 556d46db3d5SWu Fengguang struct writeback_control wbc = { 557d46db3d5SWu Fengguang .sync_mode = work->sync_mode, 558d46db3d5SWu Fengguang .tagged_writepages = work->tagged_writepages, 559d46db3d5SWu Fengguang .for_kupdate = work->for_kupdate, 560d46db3d5SWu Fengguang .for_background = work->for_background, 561d46db3d5SWu Fengguang .range_cyclic = work->range_cyclic, 562d46db3d5SWu Fengguang .range_start = 0, 563d46db3d5SWu Fengguang .range_end = LLONG_MAX, 564d46db3d5SWu Fengguang }; 565d46db3d5SWu Fengguang unsigned long start_time = jiffies; 566d46db3d5SWu Fengguang long write_chunk; 567d46db3d5SWu Fengguang long wrote = 0; /* count both pages and inodes */ 568d46db3d5SWu Fengguang 56903ba3782SJens Axboe while (!list_empty(&wb->b_io)) { 5707ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 571edadfb10SChristoph Hellwig 572edadfb10SChristoph Hellwig if (inode->i_sb != sb) { 573d46db3d5SWu Fengguang if (work->sb) { 574edadfb10SChristoph Hellwig /* 575edadfb10SChristoph Hellwig * We only want to write back data for this 576edadfb10SChristoph Hellwig * superblock, move all inodes not belonging 577edadfb10SChristoph Hellwig * to it back onto the dirty list. 578edadfb10SChristoph Hellwig */ 579f758eeabSChristoph Hellwig redirty_tail(inode, wb); 58066f3b8e2SJens Axboe continue; 58166f3b8e2SJens Axboe } 582edadfb10SChristoph Hellwig 583edadfb10SChristoph Hellwig /* 584edadfb10SChristoph Hellwig * The inode belongs to a different superblock. 585edadfb10SChristoph Hellwig * Bounce back to the caller to unpin this and 586edadfb10SChristoph Hellwig * pin the next superblock. 587edadfb10SChristoph Hellwig */ 588d46db3d5SWu Fengguang break; 589edadfb10SChristoph Hellwig } 590edadfb10SChristoph Hellwig 5919843b76aSChristoph Hellwig /* 5929843b76aSChristoph Hellwig * Don't bother with new inodes or inodes beeing freed, first 5939843b76aSChristoph Hellwig * kind does not need peridic writeout yet, and for the latter 5949843b76aSChristoph Hellwig * kind writeout is handled by the freer. 5959843b76aSChristoph Hellwig */ 596250df6edSDave Chinner spin_lock(&inode->i_lock); 5979843b76aSChristoph Hellwig if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { 598250df6edSDave Chinner spin_unlock(&inode->i_lock); 599f758eeabSChristoph Hellwig requeue_io(inode, wb); 6007ef0d737SNick Piggin continue; 6017ef0d737SNick Piggin } 6021da177e4SLinus Torvalds __iget(inode); 603d46db3d5SWu Fengguang write_chunk = writeback_chunk_size(work); 604d46db3d5SWu Fengguang wbc.nr_to_write = write_chunk; 605d46db3d5SWu Fengguang wbc.pages_skipped = 0; 606250df6edSDave Chinner 607d46db3d5SWu Fengguang writeback_single_inode(inode, wb, &wbc); 608d46db3d5SWu Fengguang 609d46db3d5SWu Fengguang work->nr_pages -= write_chunk - wbc.nr_to_write; 610d46db3d5SWu Fengguang wrote += write_chunk - wbc.nr_to_write; 611d46db3d5SWu Fengguang if (!(inode->i_state & I_DIRTY)) 612d46db3d5SWu Fengguang wrote++; 613d46db3d5SWu Fengguang if (wbc.pages_skipped) { 6141da177e4SLinus Torvalds /* 6151da177e4SLinus Torvalds * writeback is not making progress due to locked 6161da177e4SLinus Torvalds * buffers. Skip this inode for now. 6171da177e4SLinus Torvalds */ 618f758eeabSChristoph Hellwig redirty_tail(inode, wb); 6191da177e4SLinus Torvalds } 6200f1b1fd8SDave Chinner spin_unlock(&inode->i_lock); 621f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 6221da177e4SLinus Torvalds iput(inode); 6234ffc8444SOGAWA Hirofumi cond_resched(); 624f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 625d46db3d5SWu Fengguang /* 626d46db3d5SWu Fengguang * bail out to wb_writeback() often enough to check 627d46db3d5SWu Fengguang * background threshold and other termination conditions. 628d46db3d5SWu Fengguang */ 629d46db3d5SWu Fengguang if (wrote) { 630d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 631d46db3d5SWu Fengguang break; 632d46db3d5SWu Fengguang if (work->nr_pages <= 0) 633d46db3d5SWu Fengguang break; 6341da177e4SLinus Torvalds } 635d46db3d5SWu Fengguang } 636d46db3d5SWu Fengguang return wrote; 637f11c9c5cSEdward Shishkin } 63838f21977SNick Piggin 639d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb, 640d46db3d5SWu Fengguang struct wb_writeback_work *work) 641f11c9c5cSEdward Shishkin { 642d46db3d5SWu Fengguang unsigned long start_time = jiffies; 643d46db3d5SWu Fengguang long wrote = 0; 6449ecc2738SJens Axboe 645f11c9c5cSEdward Shishkin while (!list_empty(&wb->b_io)) { 6467ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 647f11c9c5cSEdward Shishkin struct super_block *sb = inode->i_sb; 648f11c9c5cSEdward Shishkin 649334132aeSChristoph Hellwig if (!pin_sb_for_writeback(sb)) { 650f758eeabSChristoph Hellwig requeue_io(inode, wb); 651d19de7edSChristoph Hellwig continue; 652334132aeSChristoph Hellwig } 653d46db3d5SWu Fengguang wrote += writeback_sb_inodes(sb, wb, work); 654d19de7edSChristoph Hellwig drop_super(sb); 655f11c9c5cSEdward Shishkin 656d46db3d5SWu Fengguang /* refer to the same tests at the end of writeback_sb_inodes */ 657d46db3d5SWu Fengguang if (wrote) { 658d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 659d46db3d5SWu Fengguang break; 660d46db3d5SWu Fengguang if (work->nr_pages <= 0) 661f11c9c5cSEdward Shishkin break; 662f11c9c5cSEdward Shishkin } 663d46db3d5SWu Fengguang } 66466f3b8e2SJens Axboe /* Leave any unwritten inodes on b_io */ 665d46db3d5SWu Fengguang return wrote; 66666f3b8e2SJens Axboe } 66766f3b8e2SJens Axboe 668d46db3d5SWu Fengguang long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages) 669edadfb10SChristoph Hellwig { 670d46db3d5SWu Fengguang struct wb_writeback_work work = { 671d46db3d5SWu Fengguang .nr_pages = nr_pages, 672d46db3d5SWu Fengguang .sync_mode = WB_SYNC_NONE, 673d46db3d5SWu Fengguang .range_cyclic = 1, 674d46db3d5SWu Fengguang }; 675d46db3d5SWu Fengguang 676f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 677424b351fSWu Fengguang if (list_empty(&wb->b_io)) 678d46db3d5SWu Fengguang queue_io(wb, NULL); 679d46db3d5SWu Fengguang __writeback_inodes_wb(wb, &work); 680f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 681edadfb10SChristoph Hellwig 682d46db3d5SWu Fengguang return nr_pages - work.nr_pages; 683d46db3d5SWu Fengguang } 68403ba3782SJens Axboe 68503ba3782SJens Axboe static inline bool over_bground_thresh(void) 68603ba3782SJens Axboe { 68703ba3782SJens Axboe unsigned long background_thresh, dirty_thresh; 68803ba3782SJens Axboe 68916c4042fSWu Fengguang global_dirty_limits(&background_thresh, &dirty_thresh); 69003ba3782SJens Axboe 69103ba3782SJens Axboe return (global_page_state(NR_FILE_DIRTY) + 6924cbec4c8SWu Fengguang global_page_state(NR_UNSTABLE_NFS) > background_thresh); 69303ba3782SJens Axboe } 69403ba3782SJens Axboe 69503ba3782SJens Axboe /* 69603ba3782SJens Axboe * Explicit flushing or periodic writeback of "old" data. 69703ba3782SJens Axboe * 69803ba3782SJens Axboe * Define "old": the first time one of an inode's pages is dirtied, we mark the 69903ba3782SJens Axboe * dirtying-time in the inode's address_space. So this periodic writeback code 70003ba3782SJens Axboe * just walks the superblock inode list, writing back any inodes which are 70103ba3782SJens Axboe * older than a specific point in time. 70203ba3782SJens Axboe * 70303ba3782SJens Axboe * Try to run once per dirty_writeback_interval. But if a writeback event 70403ba3782SJens Axboe * takes longer than a dirty_writeback_interval interval, then leave a 70503ba3782SJens Axboe * one-second gap. 70603ba3782SJens Axboe * 70703ba3782SJens Axboe * older_than_this takes precedence over nr_to_write. So we'll only write back 70803ba3782SJens Axboe * all dirty pages if they are all attached to "old" mappings. 70903ba3782SJens Axboe */ 710c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb, 71183ba7b07SChristoph Hellwig struct wb_writeback_work *work) 71203ba3782SJens Axboe { 713d46db3d5SWu Fengguang long nr_pages = work->nr_pages; 71403ba3782SJens Axboe unsigned long oldest_jif; 715a5989bdcSJan Kara struct inode *inode; 716d46db3d5SWu Fengguang long progress; 717b9543dacSJan Kara 718e185dda8SWu Fengguang oldest_jif = jiffies; 719d46db3d5SWu Fengguang work->older_than_this = &oldest_jif; 720e185dda8SWu Fengguang 721e8dfc305SWu Fengguang spin_lock(&wb->list_lock); 72203ba3782SJens Axboe for (;;) { 72303ba3782SJens Axboe /* 724d3ddec76SWu Fengguang * Stop writeback when nr_pages has been consumed 72503ba3782SJens Axboe */ 72683ba7b07SChristoph Hellwig if (work->nr_pages <= 0) 72703ba3782SJens Axboe break; 72803ba3782SJens Axboe 72903ba3782SJens Axboe /* 730aa373cf5SJan Kara * Background writeout and kupdate-style writeback may 731aa373cf5SJan Kara * run forever. Stop them if there is other work to do 732aa373cf5SJan Kara * so that e.g. sync can proceed. They'll be restarted 733aa373cf5SJan Kara * after the other works are all done. 734aa373cf5SJan Kara */ 735aa373cf5SJan Kara if ((work->for_background || work->for_kupdate) && 736aa373cf5SJan Kara !list_empty(&wb->bdi->work_list)) 737aa373cf5SJan Kara break; 738aa373cf5SJan Kara 739aa373cf5SJan Kara /* 740d3ddec76SWu Fengguang * For background writeout, stop when we are below the 741d3ddec76SWu Fengguang * background dirty threshold 74203ba3782SJens Axboe */ 74383ba7b07SChristoph Hellwig if (work->for_background && !over_bground_thresh()) 74403ba3782SJens Axboe break; 74503ba3782SJens Axboe 746ba9aa839SWu Fengguang if (work->for_kupdate) { 747ba9aa839SWu Fengguang oldest_jif = jiffies - 748ba9aa839SWu Fengguang msecs_to_jiffies(dirty_expire_interval * 10); 749d46db3d5SWu Fengguang work->older_than_this = &oldest_jif; 750ba9aa839SWu Fengguang } 751ba9aa839SWu Fengguang 752d46db3d5SWu Fengguang trace_writeback_start(wb->bdi, work); 753e8dfc305SWu Fengguang if (list_empty(&wb->b_io)) 754d46db3d5SWu Fengguang queue_io(wb, work->older_than_this); 75583ba7b07SChristoph Hellwig if (work->sb) 756d46db3d5SWu Fengguang progress = writeback_sb_inodes(work->sb, wb, work); 757edadfb10SChristoph Hellwig else 758d46db3d5SWu Fengguang progress = __writeback_inodes_wb(wb, work); 759d46db3d5SWu Fengguang trace_writeback_written(wb->bdi, work); 76003ba3782SJens Axboe 76103ba3782SJens Axboe /* 762e6fb6da2SWu Fengguang * Did we write something? Try for more 763e6fb6da2SWu Fengguang * 764e6fb6da2SWu Fengguang * Dirty inodes are moved to b_io for writeback in batches. 765e6fb6da2SWu Fengguang * The completion of the current batch does not necessarily 766e6fb6da2SWu Fengguang * mean the overall work is done. So we keep looping as long 767e6fb6da2SWu Fengguang * as made some progress on cleaning pages or inodes. 76803ba3782SJens Axboe */ 769d46db3d5SWu Fengguang if (progress) 770cb9bd115SWu Fengguang continue; 77171fd05a8SJens Axboe /* 772e6fb6da2SWu Fengguang * No more inodes for IO, bail 77371fd05a8SJens Axboe */ 774b7a2441fSWu Fengguang if (list_empty(&wb->b_more_io)) 77571fd05a8SJens Axboe break; 77671fd05a8SJens Axboe /* 777a5989bdcSJan Kara * Nothing written. Wait for some inode to 778a5989bdcSJan Kara * become available for writeback. Otherwise 779a5989bdcSJan Kara * we'll just busyloop. 780a5989bdcSJan Kara */ 781a5989bdcSJan Kara if (!list_empty(&wb->b_more_io)) { 782d46db3d5SWu Fengguang trace_writeback_wait(wb->bdi, work); 7837ccf19a8SNick Piggin inode = wb_inode(wb->b_more_io.prev); 784250df6edSDave Chinner spin_lock(&inode->i_lock); 785f758eeabSChristoph Hellwig inode_wait_for_writeback(inode, wb); 786250df6edSDave Chinner spin_unlock(&inode->i_lock); 787a5989bdcSJan Kara } 78803ba3782SJens Axboe } 789e8dfc305SWu Fengguang spin_unlock(&wb->list_lock); 79003ba3782SJens Axboe 791d46db3d5SWu Fengguang return nr_pages - work->nr_pages; 79203ba3782SJens Axboe } 79303ba3782SJens Axboe 79403ba3782SJens Axboe /* 79583ba7b07SChristoph Hellwig * Return the next wb_writeback_work struct that hasn't been processed yet. 79603ba3782SJens Axboe */ 79783ba7b07SChristoph Hellwig static struct wb_writeback_work * 79808852b6dSMinchan Kim get_next_work_item(struct backing_dev_info *bdi) 79903ba3782SJens Axboe { 80083ba7b07SChristoph Hellwig struct wb_writeback_work *work = NULL; 80103ba3782SJens Axboe 8026467716aSArtem Bityutskiy spin_lock_bh(&bdi->wb_lock); 80383ba7b07SChristoph Hellwig if (!list_empty(&bdi->work_list)) { 80483ba7b07SChristoph Hellwig work = list_entry(bdi->work_list.next, 80583ba7b07SChristoph Hellwig struct wb_writeback_work, list); 80683ba7b07SChristoph Hellwig list_del_init(&work->list); 80703ba3782SJens Axboe } 8086467716aSArtem Bityutskiy spin_unlock_bh(&bdi->wb_lock); 80983ba7b07SChristoph Hellwig return work; 81003ba3782SJens Axboe } 81103ba3782SJens Axboe 812cdf01dd5SLinus Torvalds /* 813cdf01dd5SLinus Torvalds * Add in the number of potentially dirty inodes, because each inode 814cdf01dd5SLinus Torvalds * write can dirty pagecache in the underlying blockdev. 815cdf01dd5SLinus Torvalds */ 816cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void) 817cdf01dd5SLinus Torvalds { 818cdf01dd5SLinus Torvalds return global_page_state(NR_FILE_DIRTY) + 819cdf01dd5SLinus Torvalds global_page_state(NR_UNSTABLE_NFS) + 820cdf01dd5SLinus Torvalds get_nr_dirty_inodes(); 821cdf01dd5SLinus Torvalds } 822cdf01dd5SLinus Torvalds 8236585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb) 8246585027aSJan Kara { 8256585027aSJan Kara if (over_bground_thresh()) { 8266585027aSJan Kara 8276585027aSJan Kara struct wb_writeback_work work = { 8286585027aSJan Kara .nr_pages = LONG_MAX, 8296585027aSJan Kara .sync_mode = WB_SYNC_NONE, 8306585027aSJan Kara .for_background = 1, 8316585027aSJan Kara .range_cyclic = 1, 8326585027aSJan Kara }; 8336585027aSJan Kara 8346585027aSJan Kara return wb_writeback(wb, &work); 8356585027aSJan Kara } 8366585027aSJan Kara 8376585027aSJan Kara return 0; 8386585027aSJan Kara } 8396585027aSJan Kara 84003ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb) 84103ba3782SJens Axboe { 84203ba3782SJens Axboe unsigned long expired; 84303ba3782SJens Axboe long nr_pages; 84403ba3782SJens Axboe 84569b62d01SJens Axboe /* 84669b62d01SJens Axboe * When set to zero, disable periodic writeback 84769b62d01SJens Axboe */ 84869b62d01SJens Axboe if (!dirty_writeback_interval) 84969b62d01SJens Axboe return 0; 85069b62d01SJens Axboe 85103ba3782SJens Axboe expired = wb->last_old_flush + 85203ba3782SJens Axboe msecs_to_jiffies(dirty_writeback_interval * 10); 85303ba3782SJens Axboe if (time_before(jiffies, expired)) 85403ba3782SJens Axboe return 0; 85503ba3782SJens Axboe 85603ba3782SJens Axboe wb->last_old_flush = jiffies; 857cdf01dd5SLinus Torvalds nr_pages = get_nr_dirty_pages(); 85803ba3782SJens Axboe 859c4a77a6cSJens Axboe if (nr_pages) { 86083ba7b07SChristoph Hellwig struct wb_writeback_work work = { 861c4a77a6cSJens Axboe .nr_pages = nr_pages, 862c4a77a6cSJens Axboe .sync_mode = WB_SYNC_NONE, 863c4a77a6cSJens Axboe .for_kupdate = 1, 864c4a77a6cSJens Axboe .range_cyclic = 1, 865c4a77a6cSJens Axboe }; 866c4a77a6cSJens Axboe 86783ba7b07SChristoph Hellwig return wb_writeback(wb, &work); 868c4a77a6cSJens Axboe } 86903ba3782SJens Axboe 87003ba3782SJens Axboe return 0; 87103ba3782SJens Axboe } 87203ba3782SJens Axboe 87303ba3782SJens Axboe /* 87403ba3782SJens Axboe * Retrieve work items and do the writeback they describe 87503ba3782SJens Axboe */ 87603ba3782SJens Axboe long wb_do_writeback(struct bdi_writeback *wb, int force_wait) 87703ba3782SJens Axboe { 87803ba3782SJens Axboe struct backing_dev_info *bdi = wb->bdi; 87983ba7b07SChristoph Hellwig struct wb_writeback_work *work; 880c4a77a6cSJens Axboe long wrote = 0; 88103ba3782SJens Axboe 88281d73a32SJan Kara set_bit(BDI_writeback_running, &wb->bdi->state); 88308852b6dSMinchan Kim while ((work = get_next_work_item(bdi)) != NULL) { 88403ba3782SJens Axboe /* 88503ba3782SJens Axboe * Override sync mode, in case we must wait for completion 88683ba7b07SChristoph Hellwig * because this thread is exiting now. 88703ba3782SJens Axboe */ 88803ba3782SJens Axboe if (force_wait) 88983ba7b07SChristoph Hellwig work->sync_mode = WB_SYNC_ALL; 89083ba7b07SChristoph Hellwig 891455b2864SDave Chinner trace_writeback_exec(bdi, work); 892455b2864SDave Chinner 89383ba7b07SChristoph Hellwig wrote += wb_writeback(wb, work); 89403ba3782SJens Axboe 89503ba3782SJens Axboe /* 89683ba7b07SChristoph Hellwig * Notify the caller of completion if this is a synchronous 89783ba7b07SChristoph Hellwig * work item, otherwise just free it. 89803ba3782SJens Axboe */ 89983ba7b07SChristoph Hellwig if (work->done) 90083ba7b07SChristoph Hellwig complete(work->done); 90183ba7b07SChristoph Hellwig else 90283ba7b07SChristoph Hellwig kfree(work); 90303ba3782SJens Axboe } 90403ba3782SJens Axboe 90503ba3782SJens Axboe /* 90603ba3782SJens Axboe * Check for periodic writeback, kupdated() style 90703ba3782SJens Axboe */ 90803ba3782SJens Axboe wrote += wb_check_old_data_flush(wb); 9096585027aSJan Kara wrote += wb_check_background_flush(wb); 91081d73a32SJan Kara clear_bit(BDI_writeback_running, &wb->bdi->state); 91103ba3782SJens Axboe 91203ba3782SJens Axboe return wrote; 91303ba3782SJens Axboe } 91403ba3782SJens Axboe 91503ba3782SJens Axboe /* 91603ba3782SJens Axboe * Handle writeback of dirty data for the device backed by this bdi. Also 91703ba3782SJens Axboe * wakes up periodically and does kupdated style flushing. 91803ba3782SJens Axboe */ 91908243900SChristoph Hellwig int bdi_writeback_thread(void *data) 92003ba3782SJens Axboe { 92108243900SChristoph Hellwig struct bdi_writeback *wb = data; 92208243900SChristoph Hellwig struct backing_dev_info *bdi = wb->bdi; 92303ba3782SJens Axboe long pages_written; 92403ba3782SJens Axboe 925766f9164SPeter Zijlstra current->flags |= PF_SWAPWRITE; 92608243900SChristoph Hellwig set_freezable(); 927ecd58403SArtem Bityutskiy wb->last_active = jiffies; 92803ba3782SJens Axboe 92903ba3782SJens Axboe /* 93008243900SChristoph Hellwig * Our parent may run at a different priority, just set us to normal 93103ba3782SJens Axboe */ 93208243900SChristoph Hellwig set_user_nice(current, 0); 93308243900SChristoph Hellwig 934455b2864SDave Chinner trace_writeback_thread_start(bdi); 935455b2864SDave Chinner 93603ba3782SJens Axboe while (!kthread_should_stop()) { 9376467716aSArtem Bityutskiy /* 9386467716aSArtem Bityutskiy * Remove own delayed wake-up timer, since we are already awake 9396467716aSArtem Bityutskiy * and we'll take care of the preriodic write-back. 9406467716aSArtem Bityutskiy */ 9416467716aSArtem Bityutskiy del_timer(&wb->wakeup_timer); 9426467716aSArtem Bityutskiy 94303ba3782SJens Axboe pages_written = wb_do_writeback(wb, 0); 94403ba3782SJens Axboe 945455b2864SDave Chinner trace_writeback_pages_written(pages_written); 946455b2864SDave Chinner 94703ba3782SJens Axboe if (pages_written) 948ecd58403SArtem Bityutskiy wb->last_active = jiffies; 94903ba3782SJens Axboe 950297252c8SArtem Bityutskiy set_current_state(TASK_INTERRUPTIBLE); 951b76b4014SJ. Bruce Fields if (!list_empty(&bdi->work_list) || kthread_should_stop()) { 952297252c8SArtem Bityutskiy __set_current_state(TASK_RUNNING); 953297252c8SArtem Bityutskiy continue; 95403ba3782SJens Axboe } 95503ba3782SJens Axboe 956253c34e9SArtem Bityutskiy if (wb_has_dirty_io(wb) && dirty_writeback_interval) 957fff5b85aSArtem Bityutskiy schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10)); 958253c34e9SArtem Bityutskiy else { 959253c34e9SArtem Bityutskiy /* 960253c34e9SArtem Bityutskiy * We have nothing to do, so can go sleep without any 961253c34e9SArtem Bityutskiy * timeout and save power. When a work is queued or 962253c34e9SArtem Bityutskiy * something is made dirty - we will be woken up. 963253c34e9SArtem Bityutskiy */ 96469b62d01SJens Axboe schedule(); 965f9eadbbdSJens Axboe } 96669b62d01SJens Axboe 96703ba3782SJens Axboe try_to_freeze(); 96803ba3782SJens Axboe } 96903ba3782SJens Axboe 970fff5b85aSArtem Bityutskiy /* Flush any work that raced with us exiting */ 97108243900SChristoph Hellwig if (!list_empty(&bdi->work_list)) 97208243900SChristoph Hellwig wb_do_writeback(wb, 1); 973455b2864SDave Chinner 974455b2864SDave Chinner trace_writeback_thread_stop(bdi); 97503ba3782SJens Axboe return 0; 97603ba3782SJens Axboe } 97703ba3782SJens Axboe 97808243900SChristoph Hellwig 97903ba3782SJens Axboe /* 98003ba3782SJens Axboe * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back 98103ba3782SJens Axboe * the whole world. 98203ba3782SJens Axboe */ 98303ba3782SJens Axboe void wakeup_flusher_threads(long nr_pages) 98403ba3782SJens Axboe { 985b8c2f347SChristoph Hellwig struct backing_dev_info *bdi; 986b8c2f347SChristoph Hellwig 98783ba7b07SChristoph Hellwig if (!nr_pages) { 98883ba7b07SChristoph Hellwig nr_pages = global_page_state(NR_FILE_DIRTY) + 98903ba3782SJens Axboe global_page_state(NR_UNSTABLE_NFS); 990b8c2f347SChristoph Hellwig } 991b8c2f347SChristoph Hellwig 992b8c2f347SChristoph Hellwig rcu_read_lock(); 993b8c2f347SChristoph Hellwig list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 994b8c2f347SChristoph Hellwig if (!bdi_has_dirty_io(bdi)) 995b8c2f347SChristoph Hellwig continue; 9966585027aSJan Kara __bdi_start_writeback(bdi, nr_pages, false); 997b8c2f347SChristoph Hellwig } 998b8c2f347SChristoph Hellwig rcu_read_unlock(); 99903ba3782SJens Axboe } 100003ba3782SJens Axboe 100103ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode) 100203ba3782SJens Axboe { 100303ba3782SJens Axboe if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { 100403ba3782SJens Axboe struct dentry *dentry; 100503ba3782SJens Axboe const char *name = "?"; 100603ba3782SJens Axboe 100703ba3782SJens Axboe dentry = d_find_alias(inode); 100803ba3782SJens Axboe if (dentry) { 100903ba3782SJens Axboe spin_lock(&dentry->d_lock); 101003ba3782SJens Axboe name = (const char *) dentry->d_name.name; 101103ba3782SJens Axboe } 101203ba3782SJens Axboe printk(KERN_DEBUG 101303ba3782SJens Axboe "%s(%d): dirtied inode %lu (%s) on %s\n", 101403ba3782SJens Axboe current->comm, task_pid_nr(current), inode->i_ino, 101503ba3782SJens Axboe name, inode->i_sb->s_id); 101603ba3782SJens Axboe if (dentry) { 101703ba3782SJens Axboe spin_unlock(&dentry->d_lock); 101803ba3782SJens Axboe dput(dentry); 101903ba3782SJens Axboe } 102003ba3782SJens Axboe } 102103ba3782SJens Axboe } 102203ba3782SJens Axboe 102303ba3782SJens Axboe /** 102403ba3782SJens Axboe * __mark_inode_dirty - internal function 102503ba3782SJens Axboe * @inode: inode to mark 102603ba3782SJens Axboe * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) 102703ba3782SJens Axboe * Mark an inode as dirty. Callers should use mark_inode_dirty or 102803ba3782SJens Axboe * mark_inode_dirty_sync. 102903ba3782SJens Axboe * 103003ba3782SJens Axboe * Put the inode on the super block's dirty list. 103103ba3782SJens Axboe * 103203ba3782SJens Axboe * CAREFUL! We mark it dirty unconditionally, but move it onto the 103303ba3782SJens Axboe * dirty list only if it is hashed or if it refers to a blockdev. 103403ba3782SJens Axboe * If it was not hashed, it will never be added to the dirty list 103503ba3782SJens Axboe * even if it is later hashed, as it will have been marked dirty already. 103603ba3782SJens Axboe * 103703ba3782SJens Axboe * In short, make sure you hash any inodes _before_ you start marking 103803ba3782SJens Axboe * them dirty. 103903ba3782SJens Axboe * 104003ba3782SJens Axboe * Note that for blockdevs, inode->dirtied_when represents the dirtying time of 104103ba3782SJens Axboe * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of 104203ba3782SJens Axboe * the kernel-internal blockdev inode represents the dirtying time of the 104303ba3782SJens Axboe * blockdev's pages. This is why for I_DIRTY_PAGES we always use 104403ba3782SJens Axboe * page->mapping->host, so the page-dirtying time is recorded in the internal 104503ba3782SJens Axboe * blockdev inode. 104603ba3782SJens Axboe */ 104703ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags) 104803ba3782SJens Axboe { 104903ba3782SJens Axboe struct super_block *sb = inode->i_sb; 1050253c34e9SArtem Bityutskiy struct backing_dev_info *bdi = NULL; 105103ba3782SJens Axboe 105203ba3782SJens Axboe /* 105303ba3782SJens Axboe * Don't do this for I_DIRTY_PAGES - that doesn't actually 105403ba3782SJens Axboe * dirty the inode itself 105503ba3782SJens Axboe */ 105603ba3782SJens Axboe if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { 105703ba3782SJens Axboe if (sb->s_op->dirty_inode) 1058aa385729SChristoph Hellwig sb->s_op->dirty_inode(inode, flags); 105903ba3782SJens Axboe } 106003ba3782SJens Axboe 106103ba3782SJens Axboe /* 106203ba3782SJens Axboe * make sure that changes are seen by all cpus before we test i_state 106303ba3782SJens Axboe * -- mikulas 106403ba3782SJens Axboe */ 106503ba3782SJens Axboe smp_mb(); 106603ba3782SJens Axboe 106703ba3782SJens Axboe /* avoid the locking if we can */ 106803ba3782SJens Axboe if ((inode->i_state & flags) == flags) 106903ba3782SJens Axboe return; 107003ba3782SJens Axboe 107103ba3782SJens Axboe if (unlikely(block_dump)) 107203ba3782SJens Axboe block_dump___mark_inode_dirty(inode); 107303ba3782SJens Axboe 1074250df6edSDave Chinner spin_lock(&inode->i_lock); 107503ba3782SJens Axboe if ((inode->i_state & flags) != flags) { 107603ba3782SJens Axboe const int was_dirty = inode->i_state & I_DIRTY; 107703ba3782SJens Axboe 107803ba3782SJens Axboe inode->i_state |= flags; 107903ba3782SJens Axboe 108003ba3782SJens Axboe /* 108103ba3782SJens Axboe * If the inode is being synced, just update its dirty state. 108203ba3782SJens Axboe * The unlocker will place the inode on the appropriate 108303ba3782SJens Axboe * superblock list, based upon its state. 108403ba3782SJens Axboe */ 108503ba3782SJens Axboe if (inode->i_state & I_SYNC) 1086250df6edSDave Chinner goto out_unlock_inode; 108703ba3782SJens Axboe 108803ba3782SJens Axboe /* 108903ba3782SJens Axboe * Only add valid (hashed) inodes to the superblock's 109003ba3782SJens Axboe * dirty list. Add blockdev inodes as well. 109103ba3782SJens Axboe */ 109203ba3782SJens Axboe if (!S_ISBLK(inode->i_mode)) { 10931d3382cbSAl Viro if (inode_unhashed(inode)) 1094250df6edSDave Chinner goto out_unlock_inode; 109503ba3782SJens Axboe } 1096a4ffdde6SAl Viro if (inode->i_state & I_FREEING) 1097250df6edSDave Chinner goto out_unlock_inode; 109803ba3782SJens Axboe 109903ba3782SJens Axboe /* 110003ba3782SJens Axboe * If the inode was already on b_dirty/b_io/b_more_io, don't 110103ba3782SJens Axboe * reposition it (that would break b_dirty time-ordering). 110203ba3782SJens Axboe */ 110303ba3782SJens Axboe if (!was_dirty) { 1104a66979abSDave Chinner bool wakeup_bdi = false; 1105253c34e9SArtem Bityutskiy bdi = inode_to_bdi(inode); 1106500b067cSJens Axboe 1107253c34e9SArtem Bityutskiy if (bdi_cap_writeback_dirty(bdi)) { 1108253c34e9SArtem Bityutskiy WARN(!test_bit(BDI_registered, &bdi->state), 1109253c34e9SArtem Bityutskiy "bdi-%s not registered\n", bdi->name); 1110253c34e9SArtem Bityutskiy 1111253c34e9SArtem Bityutskiy /* 1112253c34e9SArtem Bityutskiy * If this is the first dirty inode for this 1113253c34e9SArtem Bityutskiy * bdi, we have to wake-up the corresponding 1114253c34e9SArtem Bityutskiy * bdi thread to make sure background 1115253c34e9SArtem Bityutskiy * write-back happens later. 1116253c34e9SArtem Bityutskiy */ 1117253c34e9SArtem Bityutskiy if (!wb_has_dirty_io(&bdi->wb)) 1118253c34e9SArtem Bityutskiy wakeup_bdi = true; 1119500b067cSJens Axboe } 112003ba3782SJens Axboe 1121a66979abSDave Chinner spin_unlock(&inode->i_lock); 1122f758eeabSChristoph Hellwig spin_lock(&bdi->wb.list_lock); 112303ba3782SJens Axboe inode->dirtied_when = jiffies; 11247ccf19a8SNick Piggin list_move(&inode->i_wb_list, &bdi->wb.b_dirty); 1125f758eeabSChristoph Hellwig spin_unlock(&bdi->wb.list_lock); 1126253c34e9SArtem Bityutskiy 1127253c34e9SArtem Bityutskiy if (wakeup_bdi) 11286467716aSArtem Bityutskiy bdi_wakeup_thread_delayed(bdi); 1129a66979abSDave Chinner return; 1130a66979abSDave Chinner } 1131a66979abSDave Chinner } 1132a66979abSDave Chinner out_unlock_inode: 1133a66979abSDave Chinner spin_unlock(&inode->i_lock); 1134a66979abSDave Chinner 113503ba3782SJens Axboe } 113603ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty); 113703ba3782SJens Axboe 113866f3b8e2SJens Axboe /* 113966f3b8e2SJens Axboe * Write out a superblock's list of dirty inodes. A wait will be performed 114066f3b8e2SJens Axboe * upon no inodes, all inodes or the final one, depending upon sync_mode. 114166f3b8e2SJens Axboe * 114266f3b8e2SJens Axboe * If older_than_this is non-NULL, then only write out inodes which 114366f3b8e2SJens Axboe * had their first dirtying at a time earlier than *older_than_this. 114466f3b8e2SJens Axboe * 114566f3b8e2SJens Axboe * If `bdi' is non-zero then we're being asked to writeback a specific queue. 114666f3b8e2SJens Axboe * This function assumes that the blockdev superblock's inodes are backed by 114766f3b8e2SJens Axboe * a variety of queues, so all inodes are searched. For other superblocks, 114866f3b8e2SJens Axboe * assume that all inodes are backed by the same queue. 114966f3b8e2SJens Axboe * 115066f3b8e2SJens Axboe * The inodes to be written are parked on bdi->b_io. They are moved back onto 115166f3b8e2SJens Axboe * bdi->b_dirty as they are selected for writing. This way, none can be missed 115266f3b8e2SJens Axboe * on the writer throttling path, and we get decent balancing between many 115366f3b8e2SJens Axboe * throttled threads: we don't want them all piling up on inode_sync_wait. 115466f3b8e2SJens Axboe */ 1155b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb) 115666f3b8e2SJens Axboe { 115738f21977SNick Piggin struct inode *inode, *old_inode = NULL; 115838f21977SNick Piggin 115903ba3782SJens Axboe /* 116003ba3782SJens Axboe * We need to be protected against the filesystem going from 116103ba3782SJens Axboe * r/o to r/w or vice versa. 116203ba3782SJens Axboe */ 1163b6e51316SJens Axboe WARN_ON(!rwsem_is_locked(&sb->s_umount)); 116403ba3782SJens Axboe 116555fa6091SDave Chinner spin_lock(&inode_sb_list_lock); 116666f3b8e2SJens Axboe 116738f21977SNick Piggin /* 116838f21977SNick Piggin * Data integrity sync. Must wait for all pages under writeback, 116938f21977SNick Piggin * because there may have been pages dirtied before our sync 117038f21977SNick Piggin * call, but which had writeout started before we write it out. 117138f21977SNick Piggin * In which case, the inode may not be on the dirty list, but 117238f21977SNick Piggin * we still have to wait for that writeout. 117338f21977SNick Piggin */ 1174b6e51316SJens Axboe list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 1175250df6edSDave Chinner struct address_space *mapping = inode->i_mapping; 117638f21977SNick Piggin 1177250df6edSDave Chinner spin_lock(&inode->i_lock); 1178250df6edSDave Chinner if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) || 1179250df6edSDave Chinner (mapping->nrpages == 0)) { 1180250df6edSDave Chinner spin_unlock(&inode->i_lock); 118138f21977SNick Piggin continue; 1182250df6edSDave Chinner } 118338f21977SNick Piggin __iget(inode); 1184250df6edSDave Chinner spin_unlock(&inode->i_lock); 118555fa6091SDave Chinner spin_unlock(&inode_sb_list_lock); 118655fa6091SDave Chinner 118738f21977SNick Piggin /* 118855fa6091SDave Chinner * We hold a reference to 'inode' so it couldn't have been 118955fa6091SDave Chinner * removed from s_inodes list while we dropped the 119055fa6091SDave Chinner * inode_sb_list_lock. We cannot iput the inode now as we can 119155fa6091SDave Chinner * be holding the last reference and we cannot iput it under 119255fa6091SDave Chinner * inode_sb_list_lock. So we keep the reference and iput it 119355fa6091SDave Chinner * later. 119438f21977SNick Piggin */ 119538f21977SNick Piggin iput(old_inode); 119638f21977SNick Piggin old_inode = inode; 119738f21977SNick Piggin 119838f21977SNick Piggin filemap_fdatawait(mapping); 119938f21977SNick Piggin 120038f21977SNick Piggin cond_resched(); 120138f21977SNick Piggin 120255fa6091SDave Chinner spin_lock(&inode_sb_list_lock); 120338f21977SNick Piggin } 120455fa6091SDave Chinner spin_unlock(&inode_sb_list_lock); 120538f21977SNick Piggin iput(old_inode); 120666f3b8e2SJens Axboe } 12071da177e4SLinus Torvalds 1208d8a8559cSJens Axboe /** 12093259f8beSChris Mason * writeback_inodes_sb_nr - writeback dirty inodes from given super_block 1210d8a8559cSJens Axboe * @sb: the superblock 12113259f8beSChris Mason * @nr: the number of pages to write 12121da177e4SLinus Torvalds * 1213d8a8559cSJens Axboe * Start writeback on some inodes on this super_block. No guarantees are made 1214d8a8559cSJens Axboe * on how many (if any) will be written, and this function does not wait 12153259f8beSChris Mason * for IO completion of submitted IO. 12161da177e4SLinus Torvalds */ 12173259f8beSChris Mason void writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr) 12181da177e4SLinus Torvalds { 121983ba7b07SChristoph Hellwig DECLARE_COMPLETION_ONSTACK(done); 122083ba7b07SChristoph Hellwig struct wb_writeback_work work = { 12213c4d7165SChristoph Hellwig .sb = sb, 12223c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_NONE, 12236e6938b6SWu Fengguang .tagged_writepages = 1, 122483ba7b07SChristoph Hellwig .done = &done, 12253259f8beSChris Mason .nr_pages = nr, 12263c4d7165SChristoph Hellwig }; 12270e3c9a22SJens Axboe 1228cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 122983ba7b07SChristoph Hellwig bdi_queue_work(sb->s_bdi, &work); 123083ba7b07SChristoph Hellwig wait_for_completion(&done); 12311da177e4SLinus Torvalds } 12323259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr); 12333259f8beSChris Mason 12343259f8beSChris Mason /** 12353259f8beSChris Mason * writeback_inodes_sb - writeback dirty inodes from given super_block 12363259f8beSChris Mason * @sb: the superblock 12373259f8beSChris Mason * 12383259f8beSChris Mason * Start writeback on some inodes on this super_block. No guarantees are made 12393259f8beSChris Mason * on how many (if any) will be written, and this function does not wait 12403259f8beSChris Mason * for IO completion of submitted IO. 12413259f8beSChris Mason */ 12423259f8beSChris Mason void writeback_inodes_sb(struct super_block *sb) 12433259f8beSChris Mason { 1244925d169fSLinus Torvalds return writeback_inodes_sb_nr(sb, get_nr_dirty_pages()); 12453259f8beSChris Mason } 1246d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb); 1247d8a8559cSJens Axboe 1248d8a8559cSJens Axboe /** 124917bd55d0SEric Sandeen * writeback_inodes_sb_if_idle - start writeback if none underway 125017bd55d0SEric Sandeen * @sb: the superblock 125117bd55d0SEric Sandeen * 125217bd55d0SEric Sandeen * Invoke writeback_inodes_sb if no writeback is currently underway. 125317bd55d0SEric Sandeen * Returns 1 if writeback was started, 0 if not. 125417bd55d0SEric Sandeen */ 125517bd55d0SEric Sandeen int writeback_inodes_sb_if_idle(struct super_block *sb) 125617bd55d0SEric Sandeen { 125717bd55d0SEric Sandeen if (!writeback_in_progress(sb->s_bdi)) { 1258cf37e972SChristoph Hellwig down_read(&sb->s_umount); 125917bd55d0SEric Sandeen writeback_inodes_sb(sb); 1260cf37e972SChristoph Hellwig up_read(&sb->s_umount); 126117bd55d0SEric Sandeen return 1; 126217bd55d0SEric Sandeen } else 126317bd55d0SEric Sandeen return 0; 126417bd55d0SEric Sandeen } 126517bd55d0SEric Sandeen EXPORT_SYMBOL(writeback_inodes_sb_if_idle); 126617bd55d0SEric Sandeen 126717bd55d0SEric Sandeen /** 12683259f8beSChris Mason * writeback_inodes_sb_if_idle - start writeback if none underway 12693259f8beSChris Mason * @sb: the superblock 12703259f8beSChris Mason * @nr: the number of pages to write 12713259f8beSChris Mason * 12723259f8beSChris Mason * Invoke writeback_inodes_sb if no writeback is currently underway. 12733259f8beSChris Mason * Returns 1 if writeback was started, 0 if not. 12743259f8beSChris Mason */ 12753259f8beSChris Mason int writeback_inodes_sb_nr_if_idle(struct super_block *sb, 12763259f8beSChris Mason unsigned long nr) 12773259f8beSChris Mason { 12783259f8beSChris Mason if (!writeback_in_progress(sb->s_bdi)) { 12793259f8beSChris Mason down_read(&sb->s_umount); 12803259f8beSChris Mason writeback_inodes_sb_nr(sb, nr); 12813259f8beSChris Mason up_read(&sb->s_umount); 12823259f8beSChris Mason return 1; 12833259f8beSChris Mason } else 12843259f8beSChris Mason return 0; 12853259f8beSChris Mason } 12863259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr_if_idle); 12873259f8beSChris Mason 12883259f8beSChris Mason /** 1289d8a8559cSJens Axboe * sync_inodes_sb - sync sb inode pages 1290d8a8559cSJens Axboe * @sb: the superblock 1291d8a8559cSJens Axboe * 1292d8a8559cSJens Axboe * This function writes and waits on any dirty inode belonging to this 1293cb9ef8d5SStefan Hajnoczi * super_block. 1294d8a8559cSJens Axboe */ 1295b6e51316SJens Axboe void sync_inodes_sb(struct super_block *sb) 1296d8a8559cSJens Axboe { 129783ba7b07SChristoph Hellwig DECLARE_COMPLETION_ONSTACK(done); 129883ba7b07SChristoph Hellwig struct wb_writeback_work work = { 12993c4d7165SChristoph Hellwig .sb = sb, 13003c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_ALL, 13013c4d7165SChristoph Hellwig .nr_pages = LONG_MAX, 13023c4d7165SChristoph Hellwig .range_cyclic = 0, 130383ba7b07SChristoph Hellwig .done = &done, 13043c4d7165SChristoph Hellwig }; 13053c4d7165SChristoph Hellwig 1306cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 1307cf37e972SChristoph Hellwig 130883ba7b07SChristoph Hellwig bdi_queue_work(sb->s_bdi, &work); 130983ba7b07SChristoph Hellwig wait_for_completion(&done); 131083ba7b07SChristoph Hellwig 1311b6e51316SJens Axboe wait_sb_inodes(sb); 1312d8a8559cSJens Axboe } 1313d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb); 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds /** 13161da177e4SLinus Torvalds * write_inode_now - write an inode to disk 13171da177e4SLinus Torvalds * @inode: inode to write to disk 13181da177e4SLinus Torvalds * @sync: whether the write should be synchronous or not 13191da177e4SLinus Torvalds * 13207f04c26dSAndrea Arcangeli * This function commits an inode to disk immediately if it is dirty. This is 13217f04c26dSAndrea Arcangeli * primarily needed by knfsd. 13227f04c26dSAndrea Arcangeli * 13237f04c26dSAndrea Arcangeli * The caller must either have a ref on the inode or must have set I_WILL_FREE. 13241da177e4SLinus Torvalds */ 13251da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync) 13261da177e4SLinus Torvalds { 1327f758eeabSChristoph Hellwig struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 13281da177e4SLinus Torvalds int ret; 13291da177e4SLinus Torvalds struct writeback_control wbc = { 13301da177e4SLinus Torvalds .nr_to_write = LONG_MAX, 133118914b18SMike Galbraith .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 1332111ebb6eSOGAWA Hirofumi .range_start = 0, 1333111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 13341da177e4SLinus Torvalds }; 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds if (!mapping_cap_writeback_dirty(inode->i_mapping)) 133749364ce2SAndrew Morton wbc.nr_to_write = 0; 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds might_sleep(); 1340f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 13410f1b1fd8SDave Chinner spin_lock(&inode->i_lock); 1342f758eeabSChristoph Hellwig ret = writeback_single_inode(inode, wb, &wbc); 13430f1b1fd8SDave Chinner spin_unlock(&inode->i_lock); 1344f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 13451da177e4SLinus Torvalds if (sync) 13461c0eeaf5SJoern Engel inode_sync_wait(inode); 13471da177e4SLinus Torvalds return ret; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now); 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds /** 13521da177e4SLinus Torvalds * sync_inode - write an inode and its pages to disk. 13531da177e4SLinus Torvalds * @inode: the inode to sync 13541da177e4SLinus Torvalds * @wbc: controls the writeback mode 13551da177e4SLinus Torvalds * 13561da177e4SLinus Torvalds * sync_inode() will write an inode and its pages to disk. It will also 13571da177e4SLinus Torvalds * correctly update the inode on its superblock's dirty inode lists and will 13581da177e4SLinus Torvalds * update inode->i_state. 13591da177e4SLinus Torvalds * 13601da177e4SLinus Torvalds * The caller must have a ref on the inode. 13611da177e4SLinus Torvalds */ 13621da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc) 13631da177e4SLinus Torvalds { 1364f758eeabSChristoph Hellwig struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 13651da177e4SLinus Torvalds int ret; 13661da177e4SLinus Torvalds 1367f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 13680f1b1fd8SDave Chinner spin_lock(&inode->i_lock); 1369f758eeabSChristoph Hellwig ret = writeback_single_inode(inode, wb, wbc); 13700f1b1fd8SDave Chinner spin_unlock(&inode->i_lock); 1371f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 13721da177e4SLinus Torvalds return ret; 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode); 1375c3765016SChristoph Hellwig 1376c3765016SChristoph Hellwig /** 1377c691b9d9SAndrew Morton * sync_inode_metadata - write an inode to disk 1378c3765016SChristoph Hellwig * @inode: the inode to sync 1379c3765016SChristoph Hellwig * @wait: wait for I/O to complete. 1380c3765016SChristoph Hellwig * 1381c691b9d9SAndrew Morton * Write an inode to disk and adjust its dirty state after completion. 1382c3765016SChristoph Hellwig * 1383c3765016SChristoph Hellwig * Note: only writes the actual inode, no associated data or other metadata. 1384c3765016SChristoph Hellwig */ 1385c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait) 1386c3765016SChristoph Hellwig { 1387c3765016SChristoph Hellwig struct writeback_control wbc = { 1388c3765016SChristoph Hellwig .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, 1389c3765016SChristoph Hellwig .nr_to_write = 0, /* metadata-only */ 1390c3765016SChristoph Hellwig }; 1391c3765016SChristoph Hellwig 1392c3765016SChristoph Hellwig return sync_inode(inode, &wbc); 1393c3765016SChristoph Hellwig } 1394c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata); 1395