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> 2907f3f05cSDavid Howells #include "internal.h" 301da177e4SLinus Torvalds 3166f3b8e2SJens Axboe #define inode_to_bdi(inode) ((inode)->i_mapping->backing_dev_info) 32f11b00f3SAdrian Bunk 3303ba3782SJens Axboe /* 34d0bceac7SJens Axboe * We don't actually have pdflush, but this one is exported though /proc... 35d0bceac7SJens Axboe */ 36d0bceac7SJens Axboe int nr_pdflush_threads; 37d0bceac7SJens Axboe 38d0bceac7SJens Axboe /* 39c4a77a6cSJens Axboe * Passed into wb_writeback(), essentially a subset of writeback_control 40c4a77a6cSJens Axboe */ 41c4a77a6cSJens Axboe struct wb_writeback_args { 42c4a77a6cSJens Axboe long nr_pages; 43c4a77a6cSJens Axboe struct super_block *sb; 44c4a77a6cSJens Axboe enum writeback_sync_modes sync_mode; 45d3ddec76SWu Fengguang int for_kupdate:1; 46d3ddec76SWu Fengguang int range_cyclic:1; 47d3ddec76SWu Fengguang int for_background:1; 48e913fc82SJens Axboe int sb_pinned:1; 49c4a77a6cSJens Axboe }; 50c4a77a6cSJens Axboe 51c4a77a6cSJens Axboe /* 5203ba3782SJens Axboe * Work items for the bdi_writeback threads 53f11b00f3SAdrian Bunk */ 5403ba3782SJens Axboe struct bdi_work { 558010c3b6SJens Axboe struct list_head list; /* pending work list */ 568010c3b6SJens Axboe struct rcu_head rcu_head; /* for RCU free/clear of work */ 5703ba3782SJens Axboe 588010c3b6SJens Axboe unsigned long seen; /* threads that have seen this work */ 598010c3b6SJens Axboe atomic_t pending; /* number of threads still to do work */ 6003ba3782SJens Axboe 618010c3b6SJens Axboe struct wb_writeback_args args; /* writeback arguments */ 6203ba3782SJens Axboe 638010c3b6SJens Axboe unsigned long state; /* flag bits, see WS_* */ 6403ba3782SJens Axboe }; 6503ba3782SJens Axboe 6603ba3782SJens Axboe enum { 6703ba3782SJens Axboe WS_USED_B = 0, 6803ba3782SJens Axboe WS_ONSTACK_B, 6903ba3782SJens Axboe }; 7003ba3782SJens Axboe 7103ba3782SJens Axboe #define WS_USED (1 << WS_USED_B) 7203ba3782SJens Axboe #define WS_ONSTACK (1 << WS_ONSTACK_B) 7303ba3782SJens Axboe 7403ba3782SJens Axboe static inline bool bdi_work_on_stack(struct bdi_work *work) 75f11b00f3SAdrian Bunk { 7603ba3782SJens Axboe return test_bit(WS_ONSTACK_B, &work->state); 7703ba3782SJens Axboe } 7803ba3782SJens Axboe 7903ba3782SJens Axboe static inline void bdi_work_init(struct bdi_work *work, 80b6e51316SJens Axboe struct wb_writeback_args *args) 8103ba3782SJens Axboe { 8203ba3782SJens Axboe INIT_RCU_HEAD(&work->rcu_head); 83b6e51316SJens Axboe work->args = *args; 8403ba3782SJens Axboe work->state = WS_USED; 8503ba3782SJens Axboe } 8603ba3782SJens Axboe 87f11b00f3SAdrian Bunk /** 88f11b00f3SAdrian Bunk * writeback_in_progress - determine whether there is writeback in progress 89f11b00f3SAdrian Bunk * @bdi: the device's backing_dev_info structure. 90f11b00f3SAdrian Bunk * 9103ba3782SJens Axboe * Determine whether there is writeback waiting to be handled against a 9203ba3782SJens Axboe * backing device. 93f11b00f3SAdrian Bunk */ 94f11b00f3SAdrian Bunk int writeback_in_progress(struct backing_dev_info *bdi) 95f11b00f3SAdrian Bunk { 9603ba3782SJens Axboe return !list_empty(&bdi->work_list); 97f11b00f3SAdrian Bunk } 98f11b00f3SAdrian Bunk 9903ba3782SJens Axboe static void bdi_work_clear(struct bdi_work *work) 100f11b00f3SAdrian Bunk { 10103ba3782SJens Axboe clear_bit(WS_USED_B, &work->state); 10203ba3782SJens Axboe smp_mb__after_clear_bit(); 1031ef7d9aaSNick Piggin /* 1041ef7d9aaSNick Piggin * work can have disappeared at this point. bit waitq functions 1051ef7d9aaSNick Piggin * should be able to tolerate this, provided bdi_sched_wait does 1061ef7d9aaSNick Piggin * not dereference it's pointer argument. 1071ef7d9aaSNick Piggin */ 10803ba3782SJens Axboe wake_up_bit(&work->state, WS_USED_B); 109f11b00f3SAdrian Bunk } 110f11b00f3SAdrian Bunk 11103ba3782SJens Axboe static void bdi_work_free(struct rcu_head *head) 1124195f73dSNick Piggin { 11303ba3782SJens Axboe struct bdi_work *work = container_of(head, struct bdi_work, rcu_head); 1144195f73dSNick Piggin 11503ba3782SJens Axboe if (!bdi_work_on_stack(work)) 11603ba3782SJens Axboe kfree(work); 11703ba3782SJens Axboe else 11803ba3782SJens Axboe bdi_work_clear(work); 1194195f73dSNick Piggin } 1204195f73dSNick Piggin 12103ba3782SJens Axboe static void wb_work_complete(struct bdi_work *work) 1221da177e4SLinus Torvalds { 123c4a77a6cSJens Axboe const enum writeback_sync_modes sync_mode = work->args.sync_mode; 12477b9d059SNick Piggin int onstack = bdi_work_on_stack(work); 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* 12703ba3782SJens Axboe * For allocated work, we can clear the done/seen bit right here. 12803ba3782SJens Axboe * For on-stack work, we need to postpone both the clear and free 12903ba3782SJens Axboe * to after the RCU grace period, since the stack could be invalidated 13003ba3782SJens Axboe * as soon as bdi_work_clear() has done the wakeup. 1311da177e4SLinus Torvalds */ 13277b9d059SNick Piggin if (!onstack) 13303ba3782SJens Axboe bdi_work_clear(work); 13477b9d059SNick Piggin if (sync_mode == WB_SYNC_NONE || onstack) 13503ba3782SJens Axboe call_rcu(&work->rcu_head, bdi_work_free); 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds 13803ba3782SJens Axboe static void wb_clear_pending(struct bdi_writeback *wb, struct bdi_work *work) 13903ba3782SJens Axboe { 1401da177e4SLinus Torvalds /* 14103ba3782SJens Axboe * The caller has retrieved the work arguments from this work, 14203ba3782SJens Axboe * drop our reference. If this is the last ref, delete and free it 14303ba3782SJens Axboe */ 14403ba3782SJens Axboe if (atomic_dec_and_test(&work->pending)) { 14503ba3782SJens Axboe struct backing_dev_info *bdi = wb->bdi; 14603ba3782SJens Axboe 14703ba3782SJens Axboe spin_lock(&bdi->wb_lock); 14803ba3782SJens Axboe list_del_rcu(&work->list); 14903ba3782SJens Axboe spin_unlock(&bdi->wb_lock); 15003ba3782SJens Axboe 15103ba3782SJens Axboe wb_work_complete(work); 15203ba3782SJens Axboe } 15303ba3782SJens Axboe } 15403ba3782SJens Axboe 15503ba3782SJens Axboe static void bdi_queue_work(struct backing_dev_info *bdi, struct bdi_work *work) 15603ba3782SJens Axboe { 15703ba3782SJens Axboe work->seen = bdi->wb_mask; 15803ba3782SJens Axboe BUG_ON(!work->seen); 15903ba3782SJens Axboe atomic_set(&work->pending, bdi->wb_cnt); 16003ba3782SJens Axboe BUG_ON(!bdi->wb_cnt); 16103ba3782SJens Axboe 16203ba3782SJens Axboe /* 163deed62edSNick Piggin * list_add_tail_rcu() contains the necessary barriers to 164deed62edSNick Piggin * make sure the above stores are seen before the item is 165deed62edSNick Piggin * noticed on the list 1661da177e4SLinus Torvalds */ 16703ba3782SJens Axboe spin_lock(&bdi->wb_lock); 16803ba3782SJens Axboe list_add_tail_rcu(&work->list, &bdi->work_list); 16903ba3782SJens Axboe spin_unlock(&bdi->wb_lock); 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds /* 17203ba3782SJens Axboe * If the default thread isn't there, make sure we add it. When 17303ba3782SJens Axboe * it gets created and wakes up, we'll run this work. 1741da177e4SLinus Torvalds */ 17503ba3782SJens Axboe if (unlikely(list_empty_careful(&bdi->wb_list))) 17603ba3782SJens Axboe wake_up_process(default_backing_dev_info.wb.task); 17703ba3782SJens Axboe else { 17803ba3782SJens Axboe struct bdi_writeback *wb = &bdi->wb; 1791da177e4SLinus Torvalds 1801ef7d9aaSNick Piggin if (wb->task) 18103ba3782SJens Axboe wake_up_process(wb->task); 1821da177e4SLinus Torvalds } 18303ba3782SJens Axboe } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds /* 18603ba3782SJens Axboe * Used for on-stack allocated work items. The caller needs to wait until 18703ba3782SJens Axboe * the wb threads have acked the work before it's safe to continue. 1881da177e4SLinus Torvalds */ 18903ba3782SJens Axboe static void bdi_wait_on_work_clear(struct bdi_work *work) 1901da177e4SLinus Torvalds { 19103ba3782SJens Axboe wait_on_bit(&work->state, WS_USED_B, bdi_sched_wait, 19203ba3782SJens Axboe TASK_UNINTERRUPTIBLE); 19303ba3782SJens Axboe } 19403ba3782SJens Axboe 195f11fcae8SJens Axboe static void bdi_alloc_queue_work(struct backing_dev_info *bdi, 196b6e51316SJens Axboe struct wb_writeback_args *args) 19703ba3782SJens Axboe { 19803ba3782SJens Axboe struct bdi_work *work; 19903ba3782SJens Axboe 200bcddc3f0SJens Axboe /* 201bcddc3f0SJens Axboe * This is WB_SYNC_NONE writeback, so if allocation fails just 202bcddc3f0SJens Axboe * wakeup the thread for old dirty data writeback 203bcddc3f0SJens Axboe */ 20403ba3782SJens Axboe work = kmalloc(sizeof(*work), GFP_ATOMIC); 205bcddc3f0SJens Axboe if (work) { 206b6e51316SJens Axboe bdi_work_init(work, args); 207f11fcae8SJens Axboe bdi_queue_work(bdi, work); 208bcddc3f0SJens Axboe } else { 209bcddc3f0SJens Axboe struct bdi_writeback *wb = &bdi->wb; 210bcddc3f0SJens Axboe 211bcddc3f0SJens Axboe if (wb->task) 212bcddc3f0SJens Axboe wake_up_process(wb->task); 213bcddc3f0SJens Axboe } 21403ba3782SJens Axboe } 21503ba3782SJens Axboe 216b6e51316SJens Axboe /** 217b6e51316SJens Axboe * bdi_sync_writeback - start and wait for writeback 218b6e51316SJens Axboe * @bdi: the backing device to write from 219b6e51316SJens Axboe * @sb: write inodes from this super_block 220b6e51316SJens Axboe * 221b6e51316SJens Axboe * Description: 222b6e51316SJens Axboe * This does WB_SYNC_ALL data integrity writeback and waits for the 223b6e51316SJens Axboe * IO to complete. Callers must hold the sb s_umount semaphore for 224b6e51316SJens Axboe * reading, to avoid having the super disappear before we are done. 22503ba3782SJens Axboe */ 226b6e51316SJens Axboe static void bdi_sync_writeback(struct backing_dev_info *bdi, 227b6e51316SJens Axboe struct super_block *sb) 228b6e51316SJens Axboe { 229b6e51316SJens Axboe struct wb_writeback_args args = { 230b6e51316SJens Axboe .sb = sb, 231b6e51316SJens Axboe .sync_mode = WB_SYNC_ALL, 232b6e51316SJens Axboe .nr_pages = LONG_MAX, 233b6e51316SJens Axboe .range_cyclic = 0, 234e913fc82SJens Axboe /* 235e913fc82SJens Axboe * Setting sb_pinned is not necessary for WB_SYNC_ALL, but 236e913fc82SJens Axboe * lets make it explicitly clear. 237e913fc82SJens Axboe */ 238e913fc82SJens Axboe .sb_pinned = 1, 239b6e51316SJens Axboe }; 240f0fad8a5SChristoph Hellwig struct bdi_work work; 241f0fad8a5SChristoph Hellwig 242b6e51316SJens Axboe bdi_work_init(&work, &args); 243f0fad8a5SChristoph Hellwig work.state |= WS_ONSTACK; 244f0fad8a5SChristoph Hellwig 245b6e51316SJens Axboe bdi_queue_work(bdi, &work); 246f0fad8a5SChristoph Hellwig bdi_wait_on_work_clear(&work); 24703ba3782SJens Axboe } 248b6e51316SJens Axboe 249b6e51316SJens Axboe /** 250b6e51316SJens Axboe * bdi_start_writeback - start writeback 251b6e51316SJens Axboe * @bdi: the backing device to write from 2524b6764faSJaswinder Singh Rajput * @sb: write inodes from this super_block 253b6e51316SJens Axboe * @nr_pages: the number of pages to write 254e913fc82SJens Axboe * @sb_locked: caller already holds sb umount sem. 255b6e51316SJens Axboe * 256b6e51316SJens Axboe * Description: 257b6e51316SJens Axboe * This does WB_SYNC_NONE opportunistic writeback. The IO is only 258b6e51316SJens Axboe * started when this function returns, we make no guarentees on 259e913fc82SJens Axboe * completion. Caller specifies whether sb umount sem is held already or not. 260b6e51316SJens Axboe * 261b6e51316SJens Axboe */ 262a72bfd4dSJens Axboe void bdi_start_writeback(struct backing_dev_info *bdi, struct super_block *sb, 263e913fc82SJens Axboe long nr_pages, int sb_locked) 264b6e51316SJens Axboe { 265b6e51316SJens Axboe struct wb_writeback_args args = { 266a72bfd4dSJens Axboe .sb = sb, 267b6e51316SJens Axboe .sync_mode = WB_SYNC_NONE, 268b6e51316SJens Axboe .nr_pages = nr_pages, 269b6e51316SJens Axboe .range_cyclic = 1, 270e913fc82SJens Axboe .sb_pinned = sb_locked, 271b6e51316SJens Axboe }; 272b6e51316SJens Axboe 273d3ddec76SWu Fengguang /* 274d3ddec76SWu Fengguang * We treat @nr_pages=0 as the special case to do background writeback, 275d3ddec76SWu Fengguang * ie. to sync pages until the background dirty threshold is reached. 276d3ddec76SWu Fengguang */ 277d3ddec76SWu Fengguang if (!nr_pages) { 278d3ddec76SWu Fengguang args.nr_pages = LONG_MAX; 279d3ddec76SWu Fengguang args.for_background = 1; 280d3ddec76SWu Fengguang } 281d3ddec76SWu Fengguang 282b6e51316SJens Axboe bdi_alloc_queue_work(bdi, &args); 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds /* 2866610a0bcSAndrew Morton * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 2876610a0bcSAndrew Morton * furthest end of its superblock's dirty-inode list. 2886610a0bcSAndrew Morton * 2896610a0bcSAndrew Morton * Before stamping the inode's ->dirtied_when, we check to see whether it is 29066f3b8e2SJens Axboe * already the most-recently-dirtied inode on the b_dirty list. If that is 2916610a0bcSAndrew Morton * the case then the inode must have been redirtied while it was being written 2926610a0bcSAndrew Morton * out and we don't reset its dirtied_when. 2936610a0bcSAndrew Morton */ 2946610a0bcSAndrew Morton static void redirty_tail(struct inode *inode) 2956610a0bcSAndrew Morton { 29603ba3782SJens Axboe struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 2976610a0bcSAndrew Morton 29803ba3782SJens Axboe if (!list_empty(&wb->b_dirty)) { 29966f3b8e2SJens Axboe struct inode *tail; 3006610a0bcSAndrew Morton 30103ba3782SJens Axboe tail = list_entry(wb->b_dirty.next, struct inode, i_list); 30266f3b8e2SJens Axboe if (time_before(inode->dirtied_when, tail->dirtied_when)) 3036610a0bcSAndrew Morton inode->dirtied_when = jiffies; 3046610a0bcSAndrew Morton } 30503ba3782SJens Axboe list_move(&inode->i_list, &wb->b_dirty); 3066610a0bcSAndrew Morton } 3076610a0bcSAndrew Morton 3086610a0bcSAndrew Morton /* 30966f3b8e2SJens Axboe * requeue inode for re-scanning after bdi->b_io list is exhausted. 310c986d1e2SAndrew Morton */ 3110e0f4fc2SKen Chen static void requeue_io(struct inode *inode) 312c986d1e2SAndrew Morton { 31303ba3782SJens Axboe struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 31403ba3782SJens Axboe 31503ba3782SJens Axboe list_move(&inode->i_list, &wb->b_more_io); 316c986d1e2SAndrew Morton } 317c986d1e2SAndrew Morton 3181c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode) 3191c0eeaf5SJoern Engel { 3201c0eeaf5SJoern Engel /* 3211c0eeaf5SJoern Engel * Prevent speculative execution through spin_unlock(&inode_lock); 3221c0eeaf5SJoern Engel */ 3231c0eeaf5SJoern Engel smp_mb(); 3241c0eeaf5SJoern Engel wake_up_bit(&inode->i_state, __I_SYNC); 3251c0eeaf5SJoern Engel } 3261c0eeaf5SJoern Engel 327d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t) 328d2caa3c5SJeff Layton { 329d2caa3c5SJeff Layton bool ret = time_after(inode->dirtied_when, t); 330d2caa3c5SJeff Layton #ifndef CONFIG_64BIT 331d2caa3c5SJeff Layton /* 332d2caa3c5SJeff Layton * For inodes being constantly redirtied, dirtied_when can get stuck. 333d2caa3c5SJeff Layton * It _appears_ to be in the future, but is actually in distant past. 334d2caa3c5SJeff Layton * This test is necessary to prevent such wrapped-around relative times 3355b0830cbSJens Axboe * from permanently stopping the whole bdi writeback. 336d2caa3c5SJeff Layton */ 337d2caa3c5SJeff Layton ret = ret && time_before_eq(inode->dirtied_when, jiffies); 338d2caa3c5SJeff Layton #endif 339d2caa3c5SJeff Layton return ret; 340d2caa3c5SJeff Layton } 341d2caa3c5SJeff Layton 342c986d1e2SAndrew Morton /* 3432c136579SFengguang Wu * Move expired dirty inodes from @delaying_queue to @dispatch_queue. 3442c136579SFengguang Wu */ 3452c136579SFengguang Wu static void move_expired_inodes(struct list_head *delaying_queue, 3462c136579SFengguang Wu struct list_head *dispatch_queue, 3472c136579SFengguang Wu unsigned long *older_than_this) 3482c136579SFengguang Wu { 3495c03449dSShaohua Li LIST_HEAD(tmp); 3505c03449dSShaohua Li struct list_head *pos, *node; 351cf137307SJens Axboe struct super_block *sb = NULL; 3525c03449dSShaohua Li struct inode *inode; 353cf137307SJens Axboe int do_sb_sort = 0; 3545c03449dSShaohua Li 3552c136579SFengguang Wu while (!list_empty(delaying_queue)) { 3565c03449dSShaohua Li inode = list_entry(delaying_queue->prev, struct inode, i_list); 3572c136579SFengguang Wu if (older_than_this && 358d2caa3c5SJeff Layton inode_dirtied_after(inode, *older_than_this)) 3592c136579SFengguang Wu break; 360cf137307SJens Axboe if (sb && sb != inode->i_sb) 361cf137307SJens Axboe do_sb_sort = 1; 362cf137307SJens Axboe sb = inode->i_sb; 3635c03449dSShaohua Li list_move(&inode->i_list, &tmp); 3645c03449dSShaohua Li } 3655c03449dSShaohua Li 366cf137307SJens Axboe /* just one sb in list, splice to dispatch_queue and we're done */ 367cf137307SJens Axboe if (!do_sb_sort) { 368cf137307SJens Axboe list_splice(&tmp, dispatch_queue); 369cf137307SJens Axboe return; 370cf137307SJens Axboe } 371cf137307SJens Axboe 3725c03449dSShaohua Li /* Move inodes from one superblock together */ 3735c03449dSShaohua Li while (!list_empty(&tmp)) { 3745c03449dSShaohua Li inode = list_entry(tmp.prev, struct inode, i_list); 3755c03449dSShaohua Li sb = inode->i_sb; 3765c03449dSShaohua Li list_for_each_prev_safe(pos, node, &tmp) { 3775c03449dSShaohua Li inode = list_entry(pos, struct inode, i_list); 3785c03449dSShaohua Li if (inode->i_sb == sb) 3792c136579SFengguang Wu list_move(&inode->i_list, dispatch_queue); 3802c136579SFengguang Wu } 3812c136579SFengguang Wu } 3825c03449dSShaohua Li } 3832c136579SFengguang Wu 3842c136579SFengguang Wu /* 3852c136579SFengguang Wu * Queue all expired dirty inodes for io, eldest first. 3862c136579SFengguang Wu */ 38703ba3782SJens Axboe static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this) 3882c136579SFengguang Wu { 38903ba3782SJens Axboe list_splice_init(&wb->b_more_io, wb->b_io.prev); 39003ba3782SJens Axboe move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this); 39166f3b8e2SJens Axboe } 39266f3b8e2SJens Axboe 393a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc) 39466f3b8e2SJens Axboe { 39503ba3782SJens Axboe if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) 396a9185b41SChristoph Hellwig return inode->i_sb->s_op->write_inode(inode, wbc); 39703ba3782SJens Axboe return 0; 39866f3b8e2SJens Axboe } 39908d8e974SFengguang Wu 4002c136579SFengguang Wu /* 40101c03194SChristoph Hellwig * Wait for writeback on an inode to complete. 40201c03194SChristoph Hellwig */ 40301c03194SChristoph Hellwig static void inode_wait_for_writeback(struct inode *inode) 40401c03194SChristoph Hellwig { 40501c03194SChristoph Hellwig DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); 40601c03194SChristoph Hellwig wait_queue_head_t *wqh; 40701c03194SChristoph Hellwig 40801c03194SChristoph Hellwig wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 40901c03194SChristoph Hellwig do { 41001c03194SChristoph Hellwig spin_unlock(&inode_lock); 41101c03194SChristoph Hellwig __wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE); 41201c03194SChristoph Hellwig spin_lock(&inode_lock); 41301c03194SChristoph Hellwig } while (inode->i_state & I_SYNC); 41401c03194SChristoph Hellwig } 41501c03194SChristoph Hellwig 41601c03194SChristoph Hellwig /* 41701c03194SChristoph Hellwig * Write out an inode's dirty pages. Called under inode_lock. Either the 41801c03194SChristoph Hellwig * caller has ref on the inode (either via __iget or via syscall against an fd) 41901c03194SChristoph Hellwig * or the inode has I_WILL_FREE set (via generic_forget_inode) 42001c03194SChristoph Hellwig * 4211da177e4SLinus Torvalds * If `wait' is set, wait on the writeout. 4221da177e4SLinus Torvalds * 4231da177e4SLinus Torvalds * The whole writeout design is quite complex and fragile. We want to avoid 4241da177e4SLinus Torvalds * starvation of particular inodes when others are being redirtied, prevent 4251da177e4SLinus Torvalds * livelocks, etc. 4261da177e4SLinus Torvalds * 4271da177e4SLinus Torvalds * Called under inode_lock. 4281da177e4SLinus Torvalds */ 4291da177e4SLinus Torvalds static int 43001c03194SChristoph Hellwig writeback_single_inode(struct inode *inode, struct writeback_control *wbc) 4311da177e4SLinus Torvalds { 4321da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 43301c03194SChristoph Hellwig unsigned dirty; 4341da177e4SLinus Torvalds int ret; 4351da177e4SLinus Torvalds 43601c03194SChristoph Hellwig if (!atomic_read(&inode->i_count)) 43701c03194SChristoph Hellwig WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); 43801c03194SChristoph Hellwig else 43901c03194SChristoph Hellwig WARN_ON(inode->i_state & I_WILL_FREE); 44001c03194SChristoph Hellwig 44101c03194SChristoph Hellwig if (inode->i_state & I_SYNC) { 44201c03194SChristoph Hellwig /* 44301c03194SChristoph Hellwig * If this inode is locked for writeback and we are not doing 44466f3b8e2SJens Axboe * writeback-for-data-integrity, move it to b_more_io so that 44501c03194SChristoph Hellwig * writeback can proceed with the other inodes on s_io. 44601c03194SChristoph Hellwig * 44701c03194SChristoph Hellwig * We'll have another go at writing back this inode when we 44866f3b8e2SJens Axboe * completed a full scan of b_io. 44901c03194SChristoph Hellwig */ 450a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) { 45101c03194SChristoph Hellwig requeue_io(inode); 45201c03194SChristoph Hellwig return 0; 45301c03194SChristoph Hellwig } 45401c03194SChristoph Hellwig 45501c03194SChristoph Hellwig /* 45601c03194SChristoph Hellwig * It's a data-integrity sync. We must wait. 45701c03194SChristoph Hellwig */ 45801c03194SChristoph Hellwig inode_wait_for_writeback(inode); 45901c03194SChristoph Hellwig } 46001c03194SChristoph Hellwig 4611c0eeaf5SJoern Engel BUG_ON(inode->i_state & I_SYNC); 4621da177e4SLinus Torvalds 4631c0eeaf5SJoern Engel /* Set I_SYNC, reset I_DIRTY */ 4641da177e4SLinus Torvalds dirty = inode->i_state & I_DIRTY; 4651c0eeaf5SJoern Engel inode->i_state |= I_SYNC; 4661da177e4SLinus Torvalds inode->i_state &= ~I_DIRTY; 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds spin_unlock(&inode_lock); 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds ret = do_writepages(mapping, wbc); 4711da177e4SLinus Torvalds 47226821ed4SChristoph Hellwig /* 47326821ed4SChristoph Hellwig * Make sure to wait on the data before writing out the metadata. 47426821ed4SChristoph Hellwig * This is important for filesystems that modify metadata on data 47526821ed4SChristoph Hellwig * I/O completion. 47626821ed4SChristoph Hellwig */ 477a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) { 47826821ed4SChristoph Hellwig int err = filemap_fdatawait(mapping); 4791da177e4SLinus Torvalds if (ret == 0) 4801da177e4SLinus Torvalds ret = err; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 48326821ed4SChristoph Hellwig /* Don't write the inode if only I_DIRTY_PAGES was set */ 48426821ed4SChristoph Hellwig if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { 485a9185b41SChristoph Hellwig int err = write_inode(inode, wbc); 4861da177e4SLinus Torvalds if (ret == 0) 4871da177e4SLinus Torvalds ret = err; 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds spin_lock(&inode_lock); 4911c0eeaf5SJoern Engel inode->i_state &= ~I_SYNC; 49284a89245SWu Fengguang if (!(inode->i_state & (I_FREEING | I_CLEAR))) { 493b3af9468SWu Fengguang if ((inode->i_state & I_DIRTY_PAGES) && wbc->for_kupdate) { 494ae1b7f7dSWu Fengguang /* 495b3af9468SWu Fengguang * More pages get dirtied by a fast dirtier. 496b3af9468SWu Fengguang */ 497b3af9468SWu Fengguang goto select_queue; 498b3af9468SWu Fengguang } else if (inode->i_state & I_DIRTY) { 499b3af9468SWu Fengguang /* 500b3af9468SWu Fengguang * At least XFS will redirty the inode during the 501b3af9468SWu Fengguang * writeback (delalloc) and on io completion (isize). 502ae1b7f7dSWu Fengguang */ 503ae1b7f7dSWu Fengguang redirty_tail(inode); 504ae1b7f7dSWu Fengguang } else if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 5051da177e4SLinus Torvalds /* 5061da177e4SLinus Torvalds * We didn't write back all the pages. nfs_writepages() 5071da177e4SLinus Torvalds * sometimes bales out without doing anything. Redirty 50866f3b8e2SJens Axboe * the inode; Move it from b_io onto b_more_io/b_dirty. 5091b43ef91SAndrew Morton */ 5101b43ef91SAndrew Morton /* 5111b43ef91SAndrew Morton * akpm: if the caller was the kupdate function we put 51266f3b8e2SJens Axboe * this inode at the head of b_dirty so it gets first 5131b43ef91SAndrew Morton * consideration. Otherwise, move it to the tail, for 5141b43ef91SAndrew Morton * the reasons described there. I'm not really sure 5151b43ef91SAndrew Morton * how much sense this makes. Presumably I had a good 5161b43ef91SAndrew Morton * reasons for doing it this way, and I'd rather not 5171b43ef91SAndrew Morton * muck with it at present. 5181da177e4SLinus Torvalds */ 5191da177e4SLinus Torvalds if (wbc->for_kupdate) { 5201da177e4SLinus Torvalds /* 5212c136579SFengguang Wu * For the kupdate function we move the inode 52266f3b8e2SJens Axboe * to b_more_io so it will get more writeout as 5232c136579SFengguang Wu * soon as the queue becomes uncongested. 5241da177e4SLinus Torvalds */ 5251da177e4SLinus Torvalds inode->i_state |= I_DIRTY_PAGES; 526b3af9468SWu Fengguang select_queue: 5278bc3be27SFengguang Wu if (wbc->nr_to_write <= 0) { 5288bc3be27SFengguang Wu /* 5298bc3be27SFengguang Wu * slice used up: queue for next turn 5308bc3be27SFengguang Wu */ 5310e0f4fc2SKen Chen requeue_io(inode); 5321da177e4SLinus Torvalds } else { 5331da177e4SLinus Torvalds /* 5348bc3be27SFengguang Wu * somehow blocked: retry later 5358bc3be27SFengguang Wu */ 5368bc3be27SFengguang Wu redirty_tail(inode); 5378bc3be27SFengguang Wu } 5388bc3be27SFengguang Wu } else { 5398bc3be27SFengguang Wu /* 5401da177e4SLinus Torvalds * Otherwise fully redirty the inode so that 5411da177e4SLinus Torvalds * other inodes on this superblock will get some 5421da177e4SLinus Torvalds * writeout. Otherwise heavy writing to one 5431da177e4SLinus Torvalds * file would indefinitely suspend writeout of 5441da177e4SLinus Torvalds * all the other files. 5451da177e4SLinus Torvalds */ 5461da177e4SLinus Torvalds inode->i_state |= I_DIRTY_PAGES; 5471b43ef91SAndrew Morton redirty_tail(inode); 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds } else if (atomic_read(&inode->i_count)) { 5501da177e4SLinus Torvalds /* 5511da177e4SLinus Torvalds * The inode is clean, inuse 5521da177e4SLinus Torvalds */ 5531da177e4SLinus Torvalds list_move(&inode->i_list, &inode_in_use); 5541da177e4SLinus Torvalds } else { 5551da177e4SLinus Torvalds /* 5561da177e4SLinus Torvalds * The inode is clean, unused 5571da177e4SLinus Torvalds */ 5581da177e4SLinus Torvalds list_move(&inode->i_list, &inode_unused); 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds } 5611c0eeaf5SJoern Engel inode_sync_complete(inode); 5621da177e4SLinus Torvalds return ret; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds 565f11c9c5cSEdward Shishkin static void unpin_sb_for_writeback(struct super_block *sb) 5669ecc2738SJens Axboe { 5679ecc2738SJens Axboe up_read(&sb->s_umount); 5689ecc2738SJens Axboe put_super(sb); 5699ecc2738SJens Axboe } 570f11c9c5cSEdward Shishkin 571f11c9c5cSEdward Shishkin enum sb_pin_state { 572f11c9c5cSEdward Shishkin SB_PINNED, 573f11c9c5cSEdward Shishkin SB_NOT_PINNED, 574f11c9c5cSEdward Shishkin SB_PIN_FAILED 575f11c9c5cSEdward Shishkin }; 5769ecc2738SJens Axboe 57703ba3782SJens Axboe /* 57803ba3782SJens Axboe * For WB_SYNC_NONE writeback, the caller does not have the sb pinned 57903ba3782SJens Axboe * before calling writeback. So make sure that we do pin it, so it doesn't 58003ba3782SJens Axboe * go away while we are writing inodes from it. 58103ba3782SJens Axboe */ 582f11c9c5cSEdward Shishkin static enum sb_pin_state pin_sb_for_writeback(struct writeback_control *wbc, 583f11c9c5cSEdward Shishkin struct super_block *sb) 5841da177e4SLinus Torvalds { 5859ecc2738SJens Axboe /* 58603ba3782SJens Axboe * Caller must already hold the ref for this 58703ba3782SJens Axboe */ 588e913fc82SJens Axboe if (wbc->sync_mode == WB_SYNC_ALL || wbc->sb_pinned) { 58903ba3782SJens Axboe WARN_ON(!rwsem_is_locked(&sb->s_umount)); 590f11c9c5cSEdward Shishkin return SB_NOT_PINNED; 59103ba3782SJens Axboe } 59203ba3782SJens Axboe spin_lock(&sb_lock); 59303ba3782SJens Axboe sb->s_count++; 59403ba3782SJens Axboe if (down_read_trylock(&sb->s_umount)) { 59503ba3782SJens Axboe if (sb->s_root) { 59603ba3782SJens Axboe spin_unlock(&sb_lock); 597f11c9c5cSEdward Shishkin return SB_PINNED; 59803ba3782SJens Axboe } 59903ba3782SJens Axboe /* 60003ba3782SJens Axboe * umounted, drop rwsem again and fall through to failure 60103ba3782SJens Axboe */ 60203ba3782SJens Axboe up_read(&sb->s_umount); 60303ba3782SJens Axboe } 60403ba3782SJens Axboe sb->s_count--; 60503ba3782SJens Axboe spin_unlock(&sb_lock); 606f11c9c5cSEdward Shishkin return SB_PIN_FAILED; 60703ba3782SJens Axboe } 60803ba3782SJens Axboe 609f11c9c5cSEdward Shishkin /* 610f11c9c5cSEdward Shishkin * Write a portion of b_io inodes which belong to @sb. 611f11c9c5cSEdward Shishkin * If @wbc->sb != NULL, then find and write all such 612f11c9c5cSEdward Shishkin * inodes. Otherwise write only ones which go sequentially 613f11c9c5cSEdward Shishkin * in reverse order. 614f11c9c5cSEdward Shishkin * Return 1, if the caller writeback routine should be 615f11c9c5cSEdward Shishkin * interrupted. Otherwise return 0. 616f11c9c5cSEdward Shishkin */ 617f11c9c5cSEdward Shishkin static int writeback_sb_inodes(struct super_block *sb, 618f11c9c5cSEdward Shishkin struct bdi_writeback *wb, 61903ba3782SJens Axboe struct writeback_control *wbc) 62003ba3782SJens Axboe { 62103ba3782SJens Axboe while (!list_empty(&wb->b_io)) { 622f11c9c5cSEdward Shishkin long pages_skipped; 62303ba3782SJens Axboe struct inode *inode = list_entry(wb->b_io.prev, 6241da177e4SLinus Torvalds struct inode, i_list); 625f11c9c5cSEdward Shishkin if (wbc->sb && sb != inode->i_sb) { 626f11c9c5cSEdward Shishkin /* super block given and doesn't 627f11c9c5cSEdward Shishkin match, skip this inode */ 62866f3b8e2SJens Axboe redirty_tail(inode); 62966f3b8e2SJens Axboe continue; 63066f3b8e2SJens Axboe } 631f11c9c5cSEdward Shishkin if (sb != inode->i_sb) 632f11c9c5cSEdward Shishkin /* finish with this superblock */ 633f11c9c5cSEdward Shishkin return 0; 63484a89245SWu Fengguang if (inode->i_state & (I_NEW | I_WILL_FREE)) { 6357ef0d737SNick Piggin requeue_io(inode); 6367ef0d737SNick Piggin continue; 6377ef0d737SNick Piggin } 638d2caa3c5SJeff Layton /* 639d2caa3c5SJeff Layton * Was this inode dirtied after sync_sb_inodes was called? 640d2caa3c5SJeff Layton * This keeps sync from extra jobs and livelock. 641d2caa3c5SJeff Layton */ 642f11c9c5cSEdward Shishkin if (inode_dirtied_after(inode, wbc->wb_start)) 643f11c9c5cSEdward Shishkin return 1; 6441da177e4SLinus Torvalds 64584a89245SWu Fengguang BUG_ON(inode->i_state & (I_FREEING | I_CLEAR)); 6461da177e4SLinus Torvalds __iget(inode); 6471da177e4SLinus Torvalds pages_skipped = wbc->pages_skipped; 64801c03194SChristoph Hellwig writeback_single_inode(inode, wbc); 6491da177e4SLinus Torvalds if (wbc->pages_skipped != pages_skipped) { 6501da177e4SLinus Torvalds /* 6511da177e4SLinus Torvalds * writeback is not making progress due to locked 6521da177e4SLinus Torvalds * buffers. Skip this inode for now. 6531da177e4SLinus Torvalds */ 654f57b9b7bSAndrew Morton redirty_tail(inode); 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds spin_unlock(&inode_lock); 6571da177e4SLinus Torvalds iput(inode); 6584ffc8444SOGAWA Hirofumi cond_resched(); 6591da177e4SLinus Torvalds spin_lock(&inode_lock); 6608bc3be27SFengguang Wu if (wbc->nr_to_write <= 0) { 6618bc3be27SFengguang Wu wbc->more_io = 1; 662f11c9c5cSEdward Shishkin return 1; 6631da177e4SLinus Torvalds } 66403ba3782SJens Axboe if (!list_empty(&wb->b_more_io)) 6658bc3be27SFengguang Wu wbc->more_io = 1; 6668bc3be27SFengguang Wu } 667f11c9c5cSEdward Shishkin /* b_io is empty */ 668f11c9c5cSEdward Shishkin return 1; 669f11c9c5cSEdward Shishkin } 67038f21977SNick Piggin 671f11c9c5cSEdward Shishkin static void writeback_inodes_wb(struct bdi_writeback *wb, 672f11c9c5cSEdward Shishkin struct writeback_control *wbc) 673f11c9c5cSEdward Shishkin { 674f11c9c5cSEdward Shishkin int ret = 0; 6759ecc2738SJens Axboe 676f11c9c5cSEdward Shishkin wbc->wb_start = jiffies; /* livelock avoidance */ 677f11c9c5cSEdward Shishkin spin_lock(&inode_lock); 678f11c9c5cSEdward Shishkin if (!wbc->for_kupdate || list_empty(&wb->b_io)) 679f11c9c5cSEdward Shishkin queue_io(wb, wbc->older_than_this); 680f11c9c5cSEdward Shishkin 681f11c9c5cSEdward Shishkin while (!list_empty(&wb->b_io)) { 682f11c9c5cSEdward Shishkin struct inode *inode = list_entry(wb->b_io.prev, 683f11c9c5cSEdward Shishkin struct inode, i_list); 684f11c9c5cSEdward Shishkin struct super_block *sb = inode->i_sb; 685f11c9c5cSEdward Shishkin enum sb_pin_state state; 686f11c9c5cSEdward Shishkin 687f11c9c5cSEdward Shishkin if (wbc->sb && sb != wbc->sb) { 688f11c9c5cSEdward Shishkin /* super block given and doesn't 689f11c9c5cSEdward Shishkin match, skip this inode */ 690f11c9c5cSEdward Shishkin redirty_tail(inode); 691f11c9c5cSEdward Shishkin continue; 692f11c9c5cSEdward Shishkin } 693f11c9c5cSEdward Shishkin state = pin_sb_for_writeback(wbc, sb); 694f11c9c5cSEdward Shishkin 695f11c9c5cSEdward Shishkin if (state == SB_PIN_FAILED) { 696f11c9c5cSEdward Shishkin requeue_io(inode); 697f11c9c5cSEdward Shishkin continue; 698f11c9c5cSEdward Shishkin } 699f11c9c5cSEdward Shishkin ret = writeback_sb_inodes(sb, wb, wbc); 700f11c9c5cSEdward Shishkin 701f11c9c5cSEdward Shishkin if (state == SB_PINNED) 702f11c9c5cSEdward Shishkin unpin_sb_for_writeback(sb); 703f11c9c5cSEdward Shishkin if (ret) 704f11c9c5cSEdward Shishkin break; 705f11c9c5cSEdward Shishkin } 70666f3b8e2SJens Axboe spin_unlock(&inode_lock); 70766f3b8e2SJens Axboe /* Leave any unwritten inodes on b_io */ 70866f3b8e2SJens Axboe } 70966f3b8e2SJens Axboe 71003ba3782SJens Axboe void writeback_inodes_wbc(struct writeback_control *wbc) 71103ba3782SJens Axboe { 71203ba3782SJens Axboe struct backing_dev_info *bdi = wbc->bdi; 71303ba3782SJens Axboe 71403ba3782SJens Axboe writeback_inodes_wb(&bdi->wb, wbc); 71503ba3782SJens Axboe } 71603ba3782SJens Axboe 71703ba3782SJens Axboe /* 71803ba3782SJens Axboe * The maximum number of pages to writeout in a single bdi flush/kupdate 71903ba3782SJens Axboe * operation. We do this so we don't hold I_SYNC against an inode for 72003ba3782SJens Axboe * enormous amounts of time, which would block a userspace task which has 72103ba3782SJens Axboe * been forced to throttle against that inode. Also, the code reevaluates 72203ba3782SJens Axboe * the dirty each time it has written this many pages. 72303ba3782SJens Axboe */ 72403ba3782SJens Axboe #define MAX_WRITEBACK_PAGES 1024 72503ba3782SJens Axboe 72603ba3782SJens Axboe static inline bool over_bground_thresh(void) 72703ba3782SJens Axboe { 72803ba3782SJens Axboe unsigned long background_thresh, dirty_thresh; 72903ba3782SJens Axboe 73003ba3782SJens Axboe get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL); 73103ba3782SJens Axboe 73203ba3782SJens Axboe return (global_page_state(NR_FILE_DIRTY) + 73303ba3782SJens Axboe global_page_state(NR_UNSTABLE_NFS) >= background_thresh); 73403ba3782SJens Axboe } 73503ba3782SJens Axboe 73603ba3782SJens Axboe /* 73703ba3782SJens Axboe * Explicit flushing or periodic writeback of "old" data. 73803ba3782SJens Axboe * 73903ba3782SJens Axboe * Define "old": the first time one of an inode's pages is dirtied, we mark the 74003ba3782SJens Axboe * dirtying-time in the inode's address_space. So this periodic writeback code 74103ba3782SJens Axboe * just walks the superblock inode list, writing back any inodes which are 74203ba3782SJens Axboe * older than a specific point in time. 74303ba3782SJens Axboe * 74403ba3782SJens Axboe * Try to run once per dirty_writeback_interval. But if a writeback event 74503ba3782SJens Axboe * takes longer than a dirty_writeback_interval interval, then leave a 74603ba3782SJens Axboe * one-second gap. 74703ba3782SJens Axboe * 74803ba3782SJens Axboe * older_than_this takes precedence over nr_to_write. So we'll only write back 74903ba3782SJens Axboe * all dirty pages if they are all attached to "old" mappings. 75003ba3782SJens Axboe */ 751c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb, 752c4a77a6cSJens Axboe struct wb_writeback_args *args) 75303ba3782SJens Axboe { 75403ba3782SJens Axboe struct writeback_control wbc = { 75503ba3782SJens Axboe .bdi = wb->bdi, 756c4a77a6cSJens Axboe .sb = args->sb, 757c4a77a6cSJens Axboe .sync_mode = args->sync_mode, 75803ba3782SJens Axboe .older_than_this = NULL, 759c4a77a6cSJens Axboe .for_kupdate = args->for_kupdate, 760b17621feSWu Fengguang .for_background = args->for_background, 761c4a77a6cSJens Axboe .range_cyclic = args->range_cyclic, 762e913fc82SJens Axboe .sb_pinned = args->sb_pinned, 76303ba3782SJens Axboe }; 76403ba3782SJens Axboe unsigned long oldest_jif; 76503ba3782SJens Axboe long wrote = 0; 766a5989bdcSJan Kara struct inode *inode; 76703ba3782SJens Axboe 76803ba3782SJens Axboe if (wbc.for_kupdate) { 76903ba3782SJens Axboe wbc.older_than_this = &oldest_jif; 77003ba3782SJens Axboe oldest_jif = jiffies - 77103ba3782SJens Axboe msecs_to_jiffies(dirty_expire_interval * 10); 77203ba3782SJens Axboe } 773c4a77a6cSJens Axboe if (!wbc.range_cyclic) { 774c4a77a6cSJens Axboe wbc.range_start = 0; 775c4a77a6cSJens Axboe wbc.range_end = LLONG_MAX; 776c4a77a6cSJens Axboe } 77703ba3782SJens Axboe 77803ba3782SJens Axboe for (;;) { 77903ba3782SJens Axboe /* 780d3ddec76SWu Fengguang * Stop writeback when nr_pages has been consumed 78103ba3782SJens Axboe */ 782d3ddec76SWu Fengguang if (args->nr_pages <= 0) 78303ba3782SJens Axboe break; 78403ba3782SJens Axboe 78503ba3782SJens Axboe /* 786d3ddec76SWu Fengguang * For background writeout, stop when we are below the 787d3ddec76SWu Fengguang * background dirty threshold 78803ba3782SJens Axboe */ 789d3ddec76SWu Fengguang if (args->for_background && !over_bground_thresh()) 79003ba3782SJens Axboe break; 79103ba3782SJens Axboe 79203ba3782SJens Axboe wbc.more_io = 0; 79303ba3782SJens Axboe wbc.nr_to_write = MAX_WRITEBACK_PAGES; 79403ba3782SJens Axboe wbc.pages_skipped = 0; 79503ba3782SJens Axboe writeback_inodes_wb(wb, &wbc); 796c4a77a6cSJens Axboe args->nr_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write; 79703ba3782SJens Axboe wrote += MAX_WRITEBACK_PAGES - wbc.nr_to_write; 79803ba3782SJens Axboe 79903ba3782SJens Axboe /* 80071fd05a8SJens Axboe * If we consumed everything, see if we have more 80103ba3782SJens Axboe */ 80271fd05a8SJens Axboe if (wbc.nr_to_write <= 0) 80371fd05a8SJens Axboe continue; 80471fd05a8SJens Axboe /* 80571fd05a8SJens Axboe * Didn't write everything and we don't have more IO, bail 80671fd05a8SJens Axboe */ 80771fd05a8SJens Axboe if (!wbc.more_io) 80871fd05a8SJens Axboe break; 80971fd05a8SJens Axboe /* 81071fd05a8SJens Axboe * Did we write something? Try for more 81171fd05a8SJens Axboe */ 812a5989bdcSJan Kara if (wbc.nr_to_write < MAX_WRITEBACK_PAGES) 81303ba3782SJens Axboe continue; 814a5989bdcSJan Kara /* 815a5989bdcSJan Kara * Nothing written. Wait for some inode to 816a5989bdcSJan Kara * become available for writeback. Otherwise 817a5989bdcSJan Kara * we'll just busyloop. 818a5989bdcSJan Kara */ 819a5989bdcSJan Kara spin_lock(&inode_lock); 820a5989bdcSJan Kara if (!list_empty(&wb->b_more_io)) { 82171fd05a8SJens Axboe inode = list_entry(wb->b_more_io.prev, 822a5989bdcSJan Kara struct inode, i_list); 823a5989bdcSJan Kara inode_wait_for_writeback(inode); 824a5989bdcSJan Kara } 825a5989bdcSJan Kara spin_unlock(&inode_lock); 82603ba3782SJens Axboe } 82703ba3782SJens Axboe 82803ba3782SJens Axboe return wrote; 82903ba3782SJens Axboe } 83003ba3782SJens Axboe 83103ba3782SJens Axboe /* 83203ba3782SJens Axboe * Return the next bdi_work struct that hasn't been processed by this 8338010c3b6SJens Axboe * wb thread yet. ->seen is initially set for each thread that exists 8348010c3b6SJens Axboe * for this device, when a thread first notices a piece of work it 8358010c3b6SJens Axboe * clears its bit. Depending on writeback type, the thread will notify 8368010c3b6SJens Axboe * completion on either receiving the work (WB_SYNC_NONE) or after 8378010c3b6SJens Axboe * it is done (WB_SYNC_ALL). 83803ba3782SJens Axboe */ 83903ba3782SJens Axboe static struct bdi_work *get_next_work_item(struct backing_dev_info *bdi, 84003ba3782SJens Axboe struct bdi_writeback *wb) 84103ba3782SJens Axboe { 84203ba3782SJens Axboe struct bdi_work *work, *ret = NULL; 84303ba3782SJens Axboe 84403ba3782SJens Axboe rcu_read_lock(); 84503ba3782SJens Axboe 84603ba3782SJens Axboe list_for_each_entry_rcu(work, &bdi->work_list, list) { 84777fad5e6SNick Piggin if (!test_bit(wb->nr, &work->seen)) 84803ba3782SJens Axboe continue; 84977fad5e6SNick Piggin clear_bit(wb->nr, &work->seen); 85003ba3782SJens Axboe 85103ba3782SJens Axboe ret = work; 85203ba3782SJens Axboe break; 85303ba3782SJens Axboe } 85403ba3782SJens Axboe 85503ba3782SJens Axboe rcu_read_unlock(); 85603ba3782SJens Axboe return ret; 85703ba3782SJens Axboe } 85803ba3782SJens Axboe 85903ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb) 86003ba3782SJens Axboe { 86103ba3782SJens Axboe unsigned long expired; 86203ba3782SJens Axboe long nr_pages; 86303ba3782SJens Axboe 86469b62d01SJens Axboe /* 86569b62d01SJens Axboe * When set to zero, disable periodic writeback 86669b62d01SJens Axboe */ 86769b62d01SJens Axboe if (!dirty_writeback_interval) 86869b62d01SJens Axboe return 0; 86969b62d01SJens Axboe 87003ba3782SJens Axboe expired = wb->last_old_flush + 87103ba3782SJens Axboe msecs_to_jiffies(dirty_writeback_interval * 10); 87203ba3782SJens Axboe if (time_before(jiffies, expired)) 87303ba3782SJens Axboe return 0; 87403ba3782SJens Axboe 87503ba3782SJens Axboe wb->last_old_flush = jiffies; 87603ba3782SJens Axboe nr_pages = global_page_state(NR_FILE_DIRTY) + 87703ba3782SJens Axboe global_page_state(NR_UNSTABLE_NFS) + 87803ba3782SJens Axboe (inodes_stat.nr_inodes - inodes_stat.nr_unused); 87903ba3782SJens Axboe 880c4a77a6cSJens Axboe if (nr_pages) { 881c4a77a6cSJens Axboe struct wb_writeback_args args = { 882c4a77a6cSJens Axboe .nr_pages = nr_pages, 883c4a77a6cSJens Axboe .sync_mode = WB_SYNC_NONE, 884c4a77a6cSJens Axboe .for_kupdate = 1, 885c4a77a6cSJens Axboe .range_cyclic = 1, 886c4a77a6cSJens Axboe }; 887c4a77a6cSJens Axboe 888c4a77a6cSJens Axboe return wb_writeback(wb, &args); 889c4a77a6cSJens Axboe } 89003ba3782SJens Axboe 89103ba3782SJens Axboe return 0; 89203ba3782SJens Axboe } 89303ba3782SJens Axboe 89403ba3782SJens Axboe /* 89503ba3782SJens Axboe * Retrieve work items and do the writeback they describe 89603ba3782SJens Axboe */ 89703ba3782SJens Axboe long wb_do_writeback(struct bdi_writeback *wb, int force_wait) 89803ba3782SJens Axboe { 89903ba3782SJens Axboe struct backing_dev_info *bdi = wb->bdi; 90003ba3782SJens Axboe struct bdi_work *work; 901c4a77a6cSJens Axboe long wrote = 0; 90203ba3782SJens Axboe 90303ba3782SJens Axboe while ((work = get_next_work_item(bdi, wb)) != NULL) { 904c4a77a6cSJens Axboe struct wb_writeback_args args = work->args; 90503ba3782SJens Axboe 90603ba3782SJens Axboe /* 90703ba3782SJens Axboe * Override sync mode, in case we must wait for completion 90803ba3782SJens Axboe */ 90903ba3782SJens Axboe if (force_wait) 910c4a77a6cSJens Axboe work->args.sync_mode = args.sync_mode = WB_SYNC_ALL; 91103ba3782SJens Axboe 91203ba3782SJens Axboe /* 91303ba3782SJens Axboe * If this isn't a data integrity operation, just notify 91403ba3782SJens Axboe * that we have seen this work and we are now starting it. 91503ba3782SJens Axboe */ 916c4a77a6cSJens Axboe if (args.sync_mode == WB_SYNC_NONE) 91703ba3782SJens Axboe wb_clear_pending(wb, work); 91803ba3782SJens Axboe 919c4a77a6cSJens Axboe wrote += wb_writeback(wb, &args); 92003ba3782SJens Axboe 92103ba3782SJens Axboe /* 92203ba3782SJens Axboe * This is a data integrity writeback, so only do the 92303ba3782SJens Axboe * notification when we have completed the work. 92403ba3782SJens Axboe */ 925c4a77a6cSJens Axboe if (args.sync_mode == WB_SYNC_ALL) 92603ba3782SJens Axboe wb_clear_pending(wb, work); 92703ba3782SJens Axboe } 92803ba3782SJens Axboe 92903ba3782SJens Axboe /* 93003ba3782SJens Axboe * Check for periodic writeback, kupdated() style 93103ba3782SJens Axboe */ 93203ba3782SJens Axboe wrote += wb_check_old_data_flush(wb); 93303ba3782SJens Axboe 93403ba3782SJens Axboe return wrote; 93503ba3782SJens Axboe } 93603ba3782SJens Axboe 93703ba3782SJens Axboe /* 93803ba3782SJens Axboe * Handle writeback of dirty data for the device backed by this bdi. Also 93903ba3782SJens Axboe * wakes up periodically and does kupdated style flushing. 94003ba3782SJens Axboe */ 94103ba3782SJens Axboe int bdi_writeback_task(struct bdi_writeback *wb) 94203ba3782SJens Axboe { 94303ba3782SJens Axboe unsigned long last_active = jiffies; 94403ba3782SJens Axboe unsigned long wait_jiffies = -1UL; 94503ba3782SJens Axboe long pages_written; 94603ba3782SJens Axboe 94703ba3782SJens Axboe while (!kthread_should_stop()) { 94803ba3782SJens Axboe pages_written = wb_do_writeback(wb, 0); 94903ba3782SJens Axboe 95003ba3782SJens Axboe if (pages_written) 95103ba3782SJens Axboe last_active = jiffies; 95203ba3782SJens Axboe else if (wait_jiffies != -1UL) { 95303ba3782SJens Axboe unsigned long max_idle; 95403ba3782SJens Axboe 95503ba3782SJens Axboe /* 95603ba3782SJens Axboe * Longest period of inactivity that we tolerate. If we 95703ba3782SJens Axboe * see dirty data again later, the task will get 95803ba3782SJens Axboe * recreated automatically. 95903ba3782SJens Axboe */ 96003ba3782SJens Axboe max_idle = max(5UL * 60 * HZ, wait_jiffies); 96103ba3782SJens Axboe if (time_after(jiffies, max_idle + last_active)) 96203ba3782SJens Axboe break; 96303ba3782SJens Axboe } 96403ba3782SJens Axboe 96569b62d01SJens Axboe if (dirty_writeback_interval) { 96603ba3782SJens Axboe wait_jiffies = msecs_to_jiffies(dirty_writeback_interval * 10); 96749db0414SJens Axboe schedule_timeout_interruptible(wait_jiffies); 96869b62d01SJens Axboe } else 96969b62d01SJens Axboe schedule(); 97069b62d01SJens Axboe 97103ba3782SJens Axboe try_to_freeze(); 97203ba3782SJens Axboe } 97303ba3782SJens Axboe 97403ba3782SJens Axboe return 0; 97503ba3782SJens Axboe } 97603ba3782SJens Axboe 97703ba3782SJens Axboe /* 978b6e51316SJens Axboe * Schedule writeback for all backing devices. This does WB_SYNC_NONE 979b6e51316SJens Axboe * writeback, for integrity writeback see bdi_sync_writeback(). 98003ba3782SJens Axboe */ 981b6e51316SJens Axboe static void bdi_writeback_all(struct super_block *sb, long nr_pages) 98203ba3782SJens Axboe { 983b6e51316SJens Axboe struct wb_writeback_args args = { 984b6e51316SJens Axboe .sb = sb, 985b6e51316SJens Axboe .nr_pages = nr_pages, 986b6e51316SJens Axboe .sync_mode = WB_SYNC_NONE, 987b6e51316SJens Axboe }; 98803ba3782SJens Axboe struct backing_dev_info *bdi; 98903ba3782SJens Axboe 990cfc4ba53SJens Axboe rcu_read_lock(); 99103ba3782SJens Axboe 992cfc4ba53SJens Axboe list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 99303ba3782SJens Axboe if (!bdi_has_dirty_io(bdi)) 99403ba3782SJens Axboe continue; 99503ba3782SJens Axboe 996b6e51316SJens Axboe bdi_alloc_queue_work(bdi, &args); 99703ba3782SJens Axboe } 99803ba3782SJens Axboe 999cfc4ba53SJens Axboe rcu_read_unlock(); 100003ba3782SJens Axboe } 100103ba3782SJens Axboe 100203ba3782SJens Axboe /* 100303ba3782SJens Axboe * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back 100403ba3782SJens Axboe * the whole world. 100503ba3782SJens Axboe */ 100603ba3782SJens Axboe void wakeup_flusher_threads(long nr_pages) 100703ba3782SJens Axboe { 100803ba3782SJens Axboe if (nr_pages == 0) 100903ba3782SJens Axboe nr_pages = global_page_state(NR_FILE_DIRTY) + 101003ba3782SJens Axboe global_page_state(NR_UNSTABLE_NFS); 1011b6e51316SJens Axboe bdi_writeback_all(NULL, nr_pages); 101203ba3782SJens Axboe } 101303ba3782SJens Axboe 101403ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode) 101503ba3782SJens Axboe { 101603ba3782SJens Axboe if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { 101703ba3782SJens Axboe struct dentry *dentry; 101803ba3782SJens Axboe const char *name = "?"; 101903ba3782SJens Axboe 102003ba3782SJens Axboe dentry = d_find_alias(inode); 102103ba3782SJens Axboe if (dentry) { 102203ba3782SJens Axboe spin_lock(&dentry->d_lock); 102303ba3782SJens Axboe name = (const char *) dentry->d_name.name; 102403ba3782SJens Axboe } 102503ba3782SJens Axboe printk(KERN_DEBUG 102603ba3782SJens Axboe "%s(%d): dirtied inode %lu (%s) on %s\n", 102703ba3782SJens Axboe current->comm, task_pid_nr(current), inode->i_ino, 102803ba3782SJens Axboe name, inode->i_sb->s_id); 102903ba3782SJens Axboe if (dentry) { 103003ba3782SJens Axboe spin_unlock(&dentry->d_lock); 103103ba3782SJens Axboe dput(dentry); 103203ba3782SJens Axboe } 103303ba3782SJens Axboe } 103403ba3782SJens Axboe } 103503ba3782SJens Axboe 103603ba3782SJens Axboe /** 103703ba3782SJens Axboe * __mark_inode_dirty - internal function 103803ba3782SJens Axboe * @inode: inode to mark 103903ba3782SJens Axboe * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) 104003ba3782SJens Axboe * Mark an inode as dirty. Callers should use mark_inode_dirty or 104103ba3782SJens Axboe * mark_inode_dirty_sync. 104203ba3782SJens Axboe * 104303ba3782SJens Axboe * Put the inode on the super block's dirty list. 104403ba3782SJens Axboe * 104503ba3782SJens Axboe * CAREFUL! We mark it dirty unconditionally, but move it onto the 104603ba3782SJens Axboe * dirty list only if it is hashed or if it refers to a blockdev. 104703ba3782SJens Axboe * If it was not hashed, it will never be added to the dirty list 104803ba3782SJens Axboe * even if it is later hashed, as it will have been marked dirty already. 104903ba3782SJens Axboe * 105003ba3782SJens Axboe * In short, make sure you hash any inodes _before_ you start marking 105103ba3782SJens Axboe * them dirty. 105203ba3782SJens Axboe * 105303ba3782SJens Axboe * This function *must* be atomic for the I_DIRTY_PAGES case - 105403ba3782SJens Axboe * set_page_dirty() is called under spinlock in several places. 105503ba3782SJens Axboe * 105603ba3782SJens Axboe * Note that for blockdevs, inode->dirtied_when represents the dirtying time of 105703ba3782SJens Axboe * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of 105803ba3782SJens Axboe * the kernel-internal blockdev inode represents the dirtying time of the 105903ba3782SJens Axboe * blockdev's pages. This is why for I_DIRTY_PAGES we always use 106003ba3782SJens Axboe * page->mapping->host, so the page-dirtying time is recorded in the internal 106103ba3782SJens Axboe * blockdev inode. 106203ba3782SJens Axboe */ 106303ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags) 106403ba3782SJens Axboe { 106503ba3782SJens Axboe struct super_block *sb = inode->i_sb; 106603ba3782SJens Axboe 106703ba3782SJens Axboe /* 106803ba3782SJens Axboe * Don't do this for I_DIRTY_PAGES - that doesn't actually 106903ba3782SJens Axboe * dirty the inode itself 107003ba3782SJens Axboe */ 107103ba3782SJens Axboe if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { 107203ba3782SJens Axboe if (sb->s_op->dirty_inode) 107303ba3782SJens Axboe sb->s_op->dirty_inode(inode); 107403ba3782SJens Axboe } 107503ba3782SJens Axboe 107603ba3782SJens Axboe /* 107703ba3782SJens Axboe * make sure that changes are seen by all cpus before we test i_state 107803ba3782SJens Axboe * -- mikulas 107903ba3782SJens Axboe */ 108003ba3782SJens Axboe smp_mb(); 108103ba3782SJens Axboe 108203ba3782SJens Axboe /* avoid the locking if we can */ 108303ba3782SJens Axboe if ((inode->i_state & flags) == flags) 108403ba3782SJens Axboe return; 108503ba3782SJens Axboe 108603ba3782SJens Axboe if (unlikely(block_dump)) 108703ba3782SJens Axboe block_dump___mark_inode_dirty(inode); 108803ba3782SJens Axboe 108903ba3782SJens Axboe spin_lock(&inode_lock); 109003ba3782SJens Axboe if ((inode->i_state & flags) != flags) { 109103ba3782SJens Axboe const int was_dirty = inode->i_state & I_DIRTY; 109203ba3782SJens Axboe 109303ba3782SJens Axboe inode->i_state |= flags; 109403ba3782SJens Axboe 109503ba3782SJens Axboe /* 109603ba3782SJens Axboe * If the inode is being synced, just update its dirty state. 109703ba3782SJens Axboe * The unlocker will place the inode on the appropriate 109803ba3782SJens Axboe * superblock list, based upon its state. 109903ba3782SJens Axboe */ 110003ba3782SJens Axboe if (inode->i_state & I_SYNC) 110103ba3782SJens Axboe goto out; 110203ba3782SJens Axboe 110303ba3782SJens Axboe /* 110403ba3782SJens Axboe * Only add valid (hashed) inodes to the superblock's 110503ba3782SJens Axboe * dirty list. Add blockdev inodes as well. 110603ba3782SJens Axboe */ 110703ba3782SJens Axboe if (!S_ISBLK(inode->i_mode)) { 110803ba3782SJens Axboe if (hlist_unhashed(&inode->i_hash)) 110903ba3782SJens Axboe goto out; 111003ba3782SJens Axboe } 111103ba3782SJens Axboe if (inode->i_state & (I_FREEING|I_CLEAR)) 111203ba3782SJens Axboe goto out; 111303ba3782SJens Axboe 111403ba3782SJens Axboe /* 111503ba3782SJens Axboe * If the inode was already on b_dirty/b_io/b_more_io, don't 111603ba3782SJens Axboe * reposition it (that would break b_dirty time-ordering). 111703ba3782SJens Axboe */ 111803ba3782SJens Axboe if (!was_dirty) { 111903ba3782SJens Axboe struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 1120500b067cSJens Axboe struct backing_dev_info *bdi = wb->bdi; 1121500b067cSJens Axboe 1122500b067cSJens Axboe if (bdi_cap_writeback_dirty(bdi) && 1123500b067cSJens Axboe !test_bit(BDI_registered, &bdi->state)) { 1124500b067cSJens Axboe WARN_ON(1); 1125500b067cSJens Axboe printk(KERN_ERR "bdi-%s not registered\n", 1126500b067cSJens Axboe bdi->name); 1127500b067cSJens Axboe } 112803ba3782SJens Axboe 112903ba3782SJens Axboe inode->dirtied_when = jiffies; 113003ba3782SJens Axboe list_move(&inode->i_list, &wb->b_dirty); 113103ba3782SJens Axboe } 113203ba3782SJens Axboe } 113303ba3782SJens Axboe out: 113403ba3782SJens Axboe spin_unlock(&inode_lock); 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 116566f3b8e2SJens Axboe spin_lock(&inode_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) { 117538f21977SNick Piggin struct address_space *mapping; 117638f21977SNick Piggin 117703ba3782SJens Axboe if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW)) 117838f21977SNick Piggin continue; 117938f21977SNick Piggin mapping = inode->i_mapping; 118038f21977SNick Piggin if (mapping->nrpages == 0) 118138f21977SNick Piggin continue; 118238f21977SNick Piggin __iget(inode); 1183ae8547b0SHans Reiser spin_unlock(&inode_lock); 118438f21977SNick Piggin /* 118538f21977SNick Piggin * We hold a reference to 'inode' so it couldn't have 118638f21977SNick Piggin * been removed from s_inodes list while we dropped the 118738f21977SNick Piggin * inode_lock. We cannot iput the inode now as we can 118838f21977SNick Piggin * be holding the last reference and we cannot iput it 118938f21977SNick Piggin * under inode_lock. So we keep the reference and iput 119038f21977SNick Piggin * it later. 119138f21977SNick Piggin */ 119238f21977SNick Piggin iput(old_inode); 119338f21977SNick Piggin old_inode = inode; 119438f21977SNick Piggin 119538f21977SNick Piggin filemap_fdatawait(mapping); 119638f21977SNick Piggin 119738f21977SNick Piggin cond_resched(); 119838f21977SNick Piggin 119938f21977SNick Piggin spin_lock(&inode_lock); 120038f21977SNick Piggin } 120138f21977SNick Piggin spin_unlock(&inode_lock); 120238f21977SNick Piggin iput(old_inode); 120366f3b8e2SJens Axboe } 12041da177e4SLinus Torvalds 1205e913fc82SJens Axboe static void __writeback_inodes_sb(struct super_block *sb, int sb_locked) 1206e913fc82SJens Axboe { 1207e913fc82SJens Axboe unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY); 1208e913fc82SJens Axboe unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS); 1209e913fc82SJens Axboe long nr_to_write; 1210e913fc82SJens Axboe 1211e913fc82SJens Axboe nr_to_write = nr_dirty + nr_unstable + 1212e913fc82SJens Axboe (inodes_stat.nr_inodes - inodes_stat.nr_unused); 1213e913fc82SJens Axboe 1214e913fc82SJens Axboe bdi_start_writeback(sb->s_bdi, sb, nr_to_write, sb_locked); 1215e913fc82SJens Axboe } 1216e913fc82SJens Axboe 1217d8a8559cSJens Axboe /** 1218d8a8559cSJens Axboe * writeback_inodes_sb - writeback dirty inodes from given super_block 1219d8a8559cSJens Axboe * @sb: the superblock 12201da177e4SLinus Torvalds * 1221d8a8559cSJens Axboe * Start writeback on some inodes on this super_block. No guarantees are made 1222d8a8559cSJens Axboe * on how many (if any) will be written, and this function does not wait 1223d8a8559cSJens Axboe * for IO completion of submitted IO. The number of pages submitted is 1224d8a8559cSJens Axboe * returned. 12251da177e4SLinus Torvalds */ 1226b6e51316SJens Axboe void writeback_inodes_sb(struct super_block *sb) 12271da177e4SLinus Torvalds { 1228e913fc82SJens Axboe __writeback_inodes_sb(sb, 0); 12291da177e4SLinus Torvalds } 1230d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb); 1231d8a8559cSJens Axboe 1232d8a8559cSJens Axboe /** 1233e913fc82SJens Axboe * writeback_inodes_sb_locked - writeback dirty inodes from given super_block 1234e913fc82SJens Axboe * @sb: the superblock 1235e913fc82SJens Axboe * 1236e913fc82SJens Axboe * Like writeback_inodes_sb(), except the caller already holds the 1237e913fc82SJens Axboe * sb umount sem. 1238e913fc82SJens Axboe */ 1239e913fc82SJens Axboe void writeback_inodes_sb_locked(struct super_block *sb) 1240e913fc82SJens Axboe { 1241e913fc82SJens Axboe __writeback_inodes_sb(sb, 1); 1242e913fc82SJens Axboe } 1243e913fc82SJens Axboe 1244e913fc82SJens Axboe /** 124517bd55d0SEric Sandeen * writeback_inodes_sb_if_idle - start writeback if none underway 124617bd55d0SEric Sandeen * @sb: the superblock 124717bd55d0SEric Sandeen * 124817bd55d0SEric Sandeen * Invoke writeback_inodes_sb if no writeback is currently underway. 124917bd55d0SEric Sandeen * Returns 1 if writeback was started, 0 if not. 125017bd55d0SEric Sandeen */ 125117bd55d0SEric Sandeen int writeback_inodes_sb_if_idle(struct super_block *sb) 125217bd55d0SEric Sandeen { 125317bd55d0SEric Sandeen if (!writeback_in_progress(sb->s_bdi)) { 125417bd55d0SEric Sandeen writeback_inodes_sb(sb); 125517bd55d0SEric Sandeen return 1; 125617bd55d0SEric Sandeen } else 125717bd55d0SEric Sandeen return 0; 125817bd55d0SEric Sandeen } 125917bd55d0SEric Sandeen EXPORT_SYMBOL(writeback_inodes_sb_if_idle); 126017bd55d0SEric Sandeen 126117bd55d0SEric Sandeen /** 1262d8a8559cSJens Axboe * sync_inodes_sb - sync sb inode pages 1263d8a8559cSJens Axboe * @sb: the superblock 1264d8a8559cSJens Axboe * 1265d8a8559cSJens Axboe * This function writes and waits on any dirty inode belonging to this 1266d8a8559cSJens Axboe * super_block. The number of pages synced is returned. 1267d8a8559cSJens Axboe */ 1268b6e51316SJens Axboe void sync_inodes_sb(struct super_block *sb) 1269d8a8559cSJens Axboe { 1270b6e51316SJens Axboe bdi_sync_writeback(sb->s_bdi, sb); 1271b6e51316SJens Axboe wait_sb_inodes(sb); 1272d8a8559cSJens Axboe } 1273d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb); 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds /** 12761da177e4SLinus Torvalds * write_inode_now - write an inode to disk 12771da177e4SLinus Torvalds * @inode: inode to write to disk 12781da177e4SLinus Torvalds * @sync: whether the write should be synchronous or not 12791da177e4SLinus Torvalds * 12807f04c26dSAndrea Arcangeli * This function commits an inode to disk immediately if it is dirty. This is 12817f04c26dSAndrea Arcangeli * primarily needed by knfsd. 12827f04c26dSAndrea Arcangeli * 12837f04c26dSAndrea Arcangeli * The caller must either have a ref on the inode or must have set I_WILL_FREE. 12841da177e4SLinus Torvalds */ 12851da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync) 12861da177e4SLinus Torvalds { 12871da177e4SLinus Torvalds int ret; 12881da177e4SLinus Torvalds struct writeback_control wbc = { 12891da177e4SLinus Torvalds .nr_to_write = LONG_MAX, 129018914b18SMike Galbraith .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 1291111ebb6eSOGAWA Hirofumi .range_start = 0, 1292111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 12931da177e4SLinus Torvalds }; 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds if (!mapping_cap_writeback_dirty(inode->i_mapping)) 129649364ce2SAndrew Morton wbc.nr_to_write = 0; 12971da177e4SLinus Torvalds 12981da177e4SLinus Torvalds might_sleep(); 12991da177e4SLinus Torvalds spin_lock(&inode_lock); 130001c03194SChristoph Hellwig ret = writeback_single_inode(inode, &wbc); 13011da177e4SLinus Torvalds spin_unlock(&inode_lock); 13021da177e4SLinus Torvalds if (sync) 13031c0eeaf5SJoern Engel inode_sync_wait(inode); 13041da177e4SLinus Torvalds return ret; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now); 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds /** 13091da177e4SLinus Torvalds * sync_inode - write an inode and its pages to disk. 13101da177e4SLinus Torvalds * @inode: the inode to sync 13111da177e4SLinus Torvalds * @wbc: controls the writeback mode 13121da177e4SLinus Torvalds * 13131da177e4SLinus Torvalds * sync_inode() will write an inode and its pages to disk. It will also 13141da177e4SLinus Torvalds * correctly update the inode on its superblock's dirty inode lists and will 13151da177e4SLinus Torvalds * update inode->i_state. 13161da177e4SLinus Torvalds * 13171da177e4SLinus Torvalds * The caller must have a ref on the inode. 13181da177e4SLinus Torvalds */ 13191da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc) 13201da177e4SLinus Torvalds { 13211da177e4SLinus Torvalds int ret; 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds spin_lock(&inode_lock); 132401c03194SChristoph Hellwig ret = writeback_single_inode(inode, wbc); 13251da177e4SLinus Torvalds spin_unlock(&inode_lock); 13261da177e4SLinus Torvalds return ret; 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode); 1329