11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * fs/fs-writeback.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2002, Linus Torvalds. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Contains all the functions related to writing back and waiting 71da177e4SLinus Torvalds * upon dirty inodes against superblocks, and writing back dirty 81da177e4SLinus Torvalds * pages against inodes. ie: data writeback. Writeout of the 91da177e4SLinus Torvalds * inode itself is not handled here. 101da177e4SLinus Torvalds * 11e1f8e874SFrancois Cami * 10Apr2002 Andrew Morton 121da177e4SLinus Torvalds * Split out of fs/inode.c 131da177e4SLinus Torvalds * Additions for address_space-based writeback 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/kernel.h> 17630d9c47SPaul Gortmaker #include <linux/export.h> 181da177e4SLinus Torvalds #include <linux/spinlock.h> 195a0e3ad6STejun Heo #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/sched.h> 211da177e4SLinus Torvalds #include <linux/fs.h> 221da177e4SLinus Torvalds #include <linux/mm.h> 23bc31b86aSWu Fengguang #include <linux/pagemap.h> 2403ba3782SJens Axboe #include <linux/kthread.h> 251da177e4SLinus Torvalds #include <linux/writeback.h> 261da177e4SLinus Torvalds #include <linux/blkdev.h> 271da177e4SLinus Torvalds #include <linux/backing-dev.h> 28455b2864SDave Chinner #include <linux/tracepoint.h> 29719ea2fbSAl Viro #include <linux/device.h> 3021c6321fSTejun Heo #include <linux/memcontrol.h> 3107f3f05cSDavid Howells #include "internal.h" 321da177e4SLinus Torvalds 33d0bceac7SJens Axboe /* 34bc31b86aSWu Fengguang * 4MB minimal write chunk size 35bc31b86aSWu Fengguang */ 3609cbfeafSKirill A. Shutemov #define MIN_WRITEBACK_PAGES (4096UL >> (PAGE_SHIFT - 10)) 37bc31b86aSWu Fengguang 38cc395d7fSTejun Heo struct wb_completion { 39cc395d7fSTejun Heo atomic_t cnt; 40cc395d7fSTejun Heo }; 41cc395d7fSTejun Heo 42bc31b86aSWu Fengguang /* 43c4a77a6cSJens Axboe * Passed into wb_writeback(), essentially a subset of writeback_control 44c4a77a6cSJens Axboe */ 4583ba7b07SChristoph Hellwig struct wb_writeback_work { 46c4a77a6cSJens Axboe long nr_pages; 47c4a77a6cSJens Axboe struct super_block *sb; 480dc83bd3SJan Kara unsigned long *older_than_this; 49c4a77a6cSJens Axboe enum writeback_sync_modes sync_mode; 506e6938b6SWu Fengguang unsigned int tagged_writepages:1; 5152957fe1SH Hartley Sweeten unsigned int for_kupdate:1; 5252957fe1SH Hartley Sweeten unsigned int range_cyclic:1; 5352957fe1SH Hartley Sweeten unsigned int for_background:1; 547747bd4bSDave Chinner unsigned int for_sync:1; /* sync(2) WB_SYNC_ALL writeback */ 55ac7b19a3STejun Heo unsigned int auto_free:1; /* free on completion */ 560e175a18SCurt Wohlgemuth enum wb_reason reason; /* why was writeback initiated? */ 57c4a77a6cSJens Axboe 588010c3b6SJens Axboe struct list_head list; /* pending work list */ 59cc395d7fSTejun Heo struct wb_completion *done; /* set if the caller waits */ 6003ba3782SJens Axboe }; 6103ba3782SJens Axboe 62a2f48706STheodore Ts'o /* 63cc395d7fSTejun Heo * If one wants to wait for one or more wb_writeback_works, each work's 64cc395d7fSTejun Heo * ->done should be set to a wb_completion defined using the following 65cc395d7fSTejun Heo * macro. Once all work items are issued with wb_queue_work(), the caller 66cc395d7fSTejun Heo * can wait for the completion of all using wb_wait_for_completion(). Work 67cc395d7fSTejun Heo * items which are waited upon aren't freed automatically on completion. 68cc395d7fSTejun Heo */ 69cc395d7fSTejun Heo #define DEFINE_WB_COMPLETION_ONSTACK(cmpl) \ 70cc395d7fSTejun Heo struct wb_completion cmpl = { \ 71cc395d7fSTejun Heo .cnt = ATOMIC_INIT(1), \ 72cc395d7fSTejun Heo } 73cc395d7fSTejun Heo 74cc395d7fSTejun Heo 75cc395d7fSTejun Heo /* 76a2f48706STheodore Ts'o * If an inode is constantly having its pages dirtied, but then the 77a2f48706STheodore Ts'o * updates stop dirtytime_expire_interval seconds in the past, it's 78a2f48706STheodore Ts'o * possible for the worst case time between when an inode has its 79a2f48706STheodore Ts'o * timestamps updated and when they finally get written out to be two 80a2f48706STheodore Ts'o * dirtytime_expire_intervals. We set the default to 12 hours (in 81a2f48706STheodore Ts'o * seconds), which means most of the time inodes will have their 82a2f48706STheodore Ts'o * timestamps written to disk after 12 hours, but in the worst case a 83a2f48706STheodore Ts'o * few inodes might not their timestamps updated for 24 hours. 84a2f48706STheodore Ts'o */ 85a2f48706STheodore Ts'o unsigned int dirtytime_expire_interval = 12 * 60 * 60; 86a2f48706STheodore Ts'o 877ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head) 887ccf19a8SNick Piggin { 89c7f54084SDave Chinner return list_entry(head, struct inode, i_io_list); 907ccf19a8SNick Piggin } 917ccf19a8SNick Piggin 9215eb77a0SWu Fengguang /* 9315eb77a0SWu Fengguang * Include the creation of the trace points after defining the 9415eb77a0SWu Fengguang * wb_writeback_work structure and inline functions so that the definition 9515eb77a0SWu Fengguang * remains local to this file. 9615eb77a0SWu Fengguang */ 9715eb77a0SWu Fengguang #define CREATE_TRACE_POINTS 9815eb77a0SWu Fengguang #include <trace/events/writeback.h> 9915eb77a0SWu Fengguang 100774016b2SSteven Whitehouse EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage); 101774016b2SSteven Whitehouse 102d6c10f1fSTejun Heo static bool wb_io_lists_populated(struct bdi_writeback *wb) 103d6c10f1fSTejun Heo { 104d6c10f1fSTejun Heo if (wb_has_dirty_io(wb)) { 105d6c10f1fSTejun Heo return false; 106d6c10f1fSTejun Heo } else { 107d6c10f1fSTejun Heo set_bit(WB_has_dirty_io, &wb->state); 10895a46c65STejun Heo WARN_ON_ONCE(!wb->avg_write_bandwidth); 109766a9d6eSTejun Heo atomic_long_add(wb->avg_write_bandwidth, 110766a9d6eSTejun Heo &wb->bdi->tot_write_bandwidth); 111d6c10f1fSTejun Heo return true; 112d6c10f1fSTejun Heo } 113d6c10f1fSTejun Heo } 114d6c10f1fSTejun Heo 115d6c10f1fSTejun Heo static void wb_io_lists_depopulated(struct bdi_writeback *wb) 116d6c10f1fSTejun Heo { 117d6c10f1fSTejun Heo if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) && 118766a9d6eSTejun Heo list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) { 119d6c10f1fSTejun Heo clear_bit(WB_has_dirty_io, &wb->state); 12095a46c65STejun Heo WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth, 12195a46c65STejun Heo &wb->bdi->tot_write_bandwidth) < 0); 122766a9d6eSTejun Heo } 123d6c10f1fSTejun Heo } 124d6c10f1fSTejun Heo 125d6c10f1fSTejun Heo /** 126c7f54084SDave Chinner * inode_io_list_move_locked - move an inode onto a bdi_writeback IO list 127d6c10f1fSTejun Heo * @inode: inode to be moved 128d6c10f1fSTejun Heo * @wb: target bdi_writeback 129bbbc3c1cSWang Long * @head: one of @wb->b_{dirty|io|more_io|dirty_time} 130d6c10f1fSTejun Heo * 131c7f54084SDave Chinner * Move @inode->i_io_list to @list of @wb and set %WB_has_dirty_io. 132d6c10f1fSTejun Heo * Returns %true if @inode is the first occupant of the !dirty_time IO 133d6c10f1fSTejun Heo * lists; otherwise, %false. 134d6c10f1fSTejun Heo */ 135c7f54084SDave Chinner static bool inode_io_list_move_locked(struct inode *inode, 136d6c10f1fSTejun Heo struct bdi_writeback *wb, 137d6c10f1fSTejun Heo struct list_head *head) 138d6c10f1fSTejun Heo { 139d6c10f1fSTejun Heo assert_spin_locked(&wb->list_lock); 140d6c10f1fSTejun Heo 141c7f54084SDave Chinner list_move(&inode->i_io_list, head); 142d6c10f1fSTejun Heo 143d6c10f1fSTejun Heo /* dirty_time doesn't count as dirty_io until expiration */ 144d6c10f1fSTejun Heo if (head != &wb->b_dirty_time) 145d6c10f1fSTejun Heo return wb_io_lists_populated(wb); 146d6c10f1fSTejun Heo 147d6c10f1fSTejun Heo wb_io_lists_depopulated(wb); 148d6c10f1fSTejun Heo return false; 149d6c10f1fSTejun Heo } 150d6c10f1fSTejun Heo 151d6c10f1fSTejun Heo /** 152c7f54084SDave Chinner * inode_io_list_del_locked - remove an inode from its bdi_writeback IO list 153d6c10f1fSTejun Heo * @inode: inode to be removed 154d6c10f1fSTejun Heo * @wb: bdi_writeback @inode is being removed from 155d6c10f1fSTejun Heo * 156d6c10f1fSTejun Heo * Remove @inode which may be on one of @wb->b_{dirty|io|more_io} lists and 157d6c10f1fSTejun Heo * clear %WB_has_dirty_io if all are empty afterwards. 158d6c10f1fSTejun Heo */ 159c7f54084SDave Chinner static void inode_io_list_del_locked(struct inode *inode, 160d6c10f1fSTejun Heo struct bdi_writeback *wb) 161d6c10f1fSTejun Heo { 162d6c10f1fSTejun Heo assert_spin_locked(&wb->list_lock); 163d6c10f1fSTejun Heo 164c7f54084SDave Chinner list_del_init(&inode->i_io_list); 165d6c10f1fSTejun Heo wb_io_lists_depopulated(wb); 166d6c10f1fSTejun Heo } 167d6c10f1fSTejun Heo 168f0054bb1STejun Heo static void wb_wakeup(struct bdi_writeback *wb) 1695acda9d1SJan Kara { 170f0054bb1STejun Heo spin_lock_bh(&wb->work_lock); 171f0054bb1STejun Heo if (test_bit(WB_registered, &wb->state)) 172f0054bb1STejun Heo mod_delayed_work(bdi_wq, &wb->dwork, 0); 173f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 1745acda9d1SJan Kara } 1755acda9d1SJan Kara 1764a3a485bSTahsin Erdogan static void finish_writeback_work(struct bdi_writeback *wb, 1774a3a485bSTahsin Erdogan struct wb_writeback_work *work) 1784a3a485bSTahsin Erdogan { 1794a3a485bSTahsin Erdogan struct wb_completion *done = work->done; 1804a3a485bSTahsin Erdogan 1814a3a485bSTahsin Erdogan if (work->auto_free) 1824a3a485bSTahsin Erdogan kfree(work); 1834a3a485bSTahsin Erdogan if (done && atomic_dec_and_test(&done->cnt)) 1844a3a485bSTahsin Erdogan wake_up_all(&wb->bdi->wb_waitq); 1854a3a485bSTahsin Erdogan } 1864a3a485bSTahsin Erdogan 187f0054bb1STejun Heo static void wb_queue_work(struct bdi_writeback *wb, 1886585027aSJan Kara struct wb_writeback_work *work) 1896585027aSJan Kara { 1905634cc2aSTejun Heo trace_writeback_queue(wb, work); 1916585027aSJan Kara 192cc395d7fSTejun Heo if (work->done) 193cc395d7fSTejun Heo atomic_inc(&work->done->cnt); 1944a3a485bSTahsin Erdogan 1954a3a485bSTahsin Erdogan spin_lock_bh(&wb->work_lock); 1964a3a485bSTahsin Erdogan 1974a3a485bSTahsin Erdogan if (test_bit(WB_registered, &wb->state)) { 198f0054bb1STejun Heo list_add_tail(&work->list, &wb->work_list); 199f0054bb1STejun Heo mod_delayed_work(bdi_wq, &wb->dwork, 0); 2004a3a485bSTahsin Erdogan } else 2014a3a485bSTahsin Erdogan finish_writeback_work(wb, work); 2024a3a485bSTahsin Erdogan 203f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 20403ba3782SJens Axboe } 2051da177e4SLinus Torvalds 206cc395d7fSTejun Heo /** 207cc395d7fSTejun Heo * wb_wait_for_completion - wait for completion of bdi_writeback_works 208cc395d7fSTejun Heo * @bdi: bdi work items were issued to 209cc395d7fSTejun Heo * @done: target wb_completion 210cc395d7fSTejun Heo * 211cc395d7fSTejun Heo * Wait for one or more work items issued to @bdi with their ->done field 212cc395d7fSTejun Heo * set to @done, which should have been defined with 213cc395d7fSTejun Heo * DEFINE_WB_COMPLETION_ONSTACK(). This function returns after all such 214cc395d7fSTejun Heo * work items are completed. Work items which are waited upon aren't freed 215cc395d7fSTejun Heo * automatically on completion. 216cc395d7fSTejun Heo */ 217cc395d7fSTejun Heo static void wb_wait_for_completion(struct backing_dev_info *bdi, 218cc395d7fSTejun Heo struct wb_completion *done) 219cc395d7fSTejun Heo { 220cc395d7fSTejun Heo atomic_dec(&done->cnt); /* put down the initial count */ 221cc395d7fSTejun Heo wait_event(bdi->wb_waitq, !atomic_read(&done->cnt)); 222cc395d7fSTejun Heo } 223cc395d7fSTejun Heo 224703c2708STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 225703c2708STejun Heo 2262a814908STejun Heo /* parameters for foreign inode detection, see wb_detach_inode() */ 2272a814908STejun Heo #define WB_FRN_TIME_SHIFT 13 /* 1s = 2^13, upto 8 secs w/ 16bit */ 2282a814908STejun Heo #define WB_FRN_TIME_AVG_SHIFT 3 /* avg = avg * 7/8 + new * 1/8 */ 2292a814908STejun Heo #define WB_FRN_TIME_CUT_DIV 2 /* ignore rounds < avg / 2 */ 2302a814908STejun Heo #define WB_FRN_TIME_PERIOD (2 * (1 << WB_FRN_TIME_SHIFT)) /* 2s */ 2312a814908STejun Heo 2322a814908STejun Heo #define WB_FRN_HIST_SLOTS 16 /* inode->i_wb_frn_history is 16bit */ 2332a814908STejun Heo #define WB_FRN_HIST_UNIT (WB_FRN_TIME_PERIOD / WB_FRN_HIST_SLOTS) 2342a814908STejun Heo /* each slot's duration is 2s / 16 */ 2352a814908STejun Heo #define WB_FRN_HIST_THR_SLOTS (WB_FRN_HIST_SLOTS / 2) 2362a814908STejun Heo /* if foreign slots >= 8, switch */ 2372a814908STejun Heo #define WB_FRN_HIST_MAX_SLOTS (WB_FRN_HIST_THR_SLOTS / 2 + 1) 2382a814908STejun Heo /* one round can affect upto 5 slots */ 2392a814908STejun Heo 240a1a0e23eSTejun Heo static atomic_t isw_nr_in_flight = ATOMIC_INIT(0); 241a1a0e23eSTejun Heo static struct workqueue_struct *isw_wq; 242a1a0e23eSTejun Heo 24321c6321fSTejun Heo void __inode_attach_wb(struct inode *inode, struct page *page) 24421c6321fSTejun Heo { 24521c6321fSTejun Heo struct backing_dev_info *bdi = inode_to_bdi(inode); 24621c6321fSTejun Heo struct bdi_writeback *wb = NULL; 24721c6321fSTejun Heo 24821c6321fSTejun Heo if (inode_cgwb_enabled(inode)) { 24921c6321fSTejun Heo struct cgroup_subsys_state *memcg_css; 25021c6321fSTejun Heo 25121c6321fSTejun Heo if (page) { 25221c6321fSTejun Heo memcg_css = mem_cgroup_css_from_page(page); 25321c6321fSTejun Heo wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 25421c6321fSTejun Heo } else { 25521c6321fSTejun Heo /* must pin memcg_css, see wb_get_create() */ 25621c6321fSTejun Heo memcg_css = task_get_css(current, memory_cgrp_id); 25721c6321fSTejun Heo wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 25821c6321fSTejun Heo css_put(memcg_css); 25921c6321fSTejun Heo } 26021c6321fSTejun Heo } 26121c6321fSTejun Heo 26221c6321fSTejun Heo if (!wb) 26321c6321fSTejun Heo wb = &bdi->wb; 26421c6321fSTejun Heo 26521c6321fSTejun Heo /* 26621c6321fSTejun Heo * There may be multiple instances of this function racing to 26721c6321fSTejun Heo * update the same inode. Use cmpxchg() to tell the winner. 26821c6321fSTejun Heo */ 26921c6321fSTejun Heo if (unlikely(cmpxchg(&inode->i_wb, NULL, wb))) 27021c6321fSTejun Heo wb_put(wb); 27121c6321fSTejun Heo } 27221c6321fSTejun Heo 273703c2708STejun Heo /** 27487e1d789STejun Heo * locked_inode_to_wb_and_lock_list - determine a locked inode's wb and lock it 27587e1d789STejun Heo * @inode: inode of interest with i_lock held 27687e1d789STejun Heo * 27787e1d789STejun Heo * Returns @inode's wb with its list_lock held. @inode->i_lock must be 27887e1d789STejun Heo * held on entry and is released on return. The returned wb is guaranteed 27987e1d789STejun Heo * to stay @inode's associated wb until its list_lock is released. 28087e1d789STejun Heo */ 28187e1d789STejun Heo static struct bdi_writeback * 28287e1d789STejun Heo locked_inode_to_wb_and_lock_list(struct inode *inode) 28387e1d789STejun Heo __releases(&inode->i_lock) 28487e1d789STejun Heo __acquires(&wb->list_lock) 28587e1d789STejun Heo { 28687e1d789STejun Heo while (true) { 28787e1d789STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 28887e1d789STejun Heo 28987e1d789STejun Heo /* 29087e1d789STejun Heo * inode_to_wb() association is protected by both 29187e1d789STejun Heo * @inode->i_lock and @wb->list_lock but list_lock nests 29287e1d789STejun Heo * outside i_lock. Drop i_lock and verify that the 29387e1d789STejun Heo * association hasn't changed after acquiring list_lock. 29487e1d789STejun Heo */ 29587e1d789STejun Heo wb_get(wb); 29687e1d789STejun Heo spin_unlock(&inode->i_lock); 29787e1d789STejun Heo spin_lock(&wb->list_lock); 29887e1d789STejun Heo 299aaa2cacfSTejun Heo /* i_wb may have changed inbetween, can't use inode_to_wb() */ 300614a4e37STejun Heo if (likely(wb == inode->i_wb)) { 301614a4e37STejun Heo wb_put(wb); /* @inode already has ref */ 302614a4e37STejun Heo return wb; 303614a4e37STejun Heo } 30487e1d789STejun Heo 30587e1d789STejun Heo spin_unlock(&wb->list_lock); 306614a4e37STejun Heo wb_put(wb); 30787e1d789STejun Heo cpu_relax(); 30887e1d789STejun Heo spin_lock(&inode->i_lock); 30987e1d789STejun Heo } 31087e1d789STejun Heo } 31187e1d789STejun Heo 31287e1d789STejun Heo /** 31387e1d789STejun Heo * inode_to_wb_and_lock_list - determine an inode's wb and lock it 31487e1d789STejun Heo * @inode: inode of interest 31587e1d789STejun Heo * 31687e1d789STejun Heo * Same as locked_inode_to_wb_and_lock_list() but @inode->i_lock isn't held 31787e1d789STejun Heo * on entry. 31887e1d789STejun Heo */ 31987e1d789STejun Heo static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode) 32087e1d789STejun Heo __acquires(&wb->list_lock) 32187e1d789STejun Heo { 32287e1d789STejun Heo spin_lock(&inode->i_lock); 32387e1d789STejun Heo return locked_inode_to_wb_and_lock_list(inode); 32487e1d789STejun Heo } 32587e1d789STejun Heo 326682aa8e1STejun Heo struct inode_switch_wbs_context { 327682aa8e1STejun Heo struct inode *inode; 328682aa8e1STejun Heo struct bdi_writeback *new_wb; 329682aa8e1STejun Heo 330682aa8e1STejun Heo struct rcu_head rcu_head; 331682aa8e1STejun Heo struct work_struct work; 332682aa8e1STejun Heo }; 333682aa8e1STejun Heo 334682aa8e1STejun Heo static void inode_switch_wbs_work_fn(struct work_struct *work) 335682aa8e1STejun Heo { 336682aa8e1STejun Heo struct inode_switch_wbs_context *isw = 337682aa8e1STejun Heo container_of(work, struct inode_switch_wbs_context, work); 338682aa8e1STejun Heo struct inode *inode = isw->inode; 339d10c8095STejun Heo struct address_space *mapping = inode->i_mapping; 340d10c8095STejun Heo struct bdi_writeback *old_wb = inode->i_wb; 341682aa8e1STejun Heo struct bdi_writeback *new_wb = isw->new_wb; 342d10c8095STejun Heo struct radix_tree_iter iter; 343d10c8095STejun Heo bool switched = false; 344d10c8095STejun Heo void **slot; 345682aa8e1STejun Heo 346682aa8e1STejun Heo /* 347682aa8e1STejun Heo * By the time control reaches here, RCU grace period has passed 348682aa8e1STejun Heo * since I_WB_SWITCH assertion and all wb stat update transactions 349682aa8e1STejun Heo * between unlocked_inode_to_wb_begin/end() are guaranteed to be 350682aa8e1STejun Heo * synchronizing against mapping->tree_lock. 351d10c8095STejun Heo * 352d10c8095STejun Heo * Grabbing old_wb->list_lock, inode->i_lock and mapping->tree_lock 353d10c8095STejun Heo * gives us exclusion against all wb related operations on @inode 354d10c8095STejun Heo * including IO list manipulations and stat updates. 355682aa8e1STejun Heo */ 356d10c8095STejun Heo if (old_wb < new_wb) { 357d10c8095STejun Heo spin_lock(&old_wb->list_lock); 358d10c8095STejun Heo spin_lock_nested(&new_wb->list_lock, SINGLE_DEPTH_NESTING); 359d10c8095STejun Heo } else { 360d10c8095STejun Heo spin_lock(&new_wb->list_lock); 361d10c8095STejun Heo spin_lock_nested(&old_wb->list_lock, SINGLE_DEPTH_NESTING); 362d10c8095STejun Heo } 363682aa8e1STejun Heo spin_lock(&inode->i_lock); 364d10c8095STejun Heo spin_lock_irq(&mapping->tree_lock); 365682aa8e1STejun Heo 366d10c8095STejun Heo /* 367d10c8095STejun Heo * Once I_FREEING is visible under i_lock, the eviction path owns 368c7f54084SDave Chinner * the inode and we shouldn't modify ->i_io_list. 369d10c8095STejun Heo */ 370d10c8095STejun Heo if (unlikely(inode->i_state & I_FREEING)) 371d10c8095STejun Heo goto skip_switch; 372d10c8095STejun Heo 373d10c8095STejun Heo /* 374d10c8095STejun Heo * Count and transfer stats. Note that PAGECACHE_TAG_DIRTY points 375d10c8095STejun Heo * to possibly dirty pages while PAGECACHE_TAG_WRITEBACK points to 376d10c8095STejun Heo * pages actually under underwriteback. 377d10c8095STejun Heo */ 378d10c8095STejun Heo radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter, 0, 379d10c8095STejun Heo PAGECACHE_TAG_DIRTY) { 380d10c8095STejun Heo struct page *page = radix_tree_deref_slot_protected(slot, 381d10c8095STejun Heo &mapping->tree_lock); 382d10c8095STejun Heo if (likely(page) && PageDirty(page)) { 3833e8f399dSNikolay Borisov dec_wb_stat(old_wb, WB_RECLAIMABLE); 3843e8f399dSNikolay Borisov inc_wb_stat(new_wb, WB_RECLAIMABLE); 385d10c8095STejun Heo } 386d10c8095STejun Heo } 387d10c8095STejun Heo 388d10c8095STejun Heo radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter, 0, 389d10c8095STejun Heo PAGECACHE_TAG_WRITEBACK) { 390d10c8095STejun Heo struct page *page = radix_tree_deref_slot_protected(slot, 391d10c8095STejun Heo &mapping->tree_lock); 392d10c8095STejun Heo if (likely(page)) { 393d10c8095STejun Heo WARN_ON_ONCE(!PageWriteback(page)); 3943e8f399dSNikolay Borisov dec_wb_stat(old_wb, WB_WRITEBACK); 3953e8f399dSNikolay Borisov inc_wb_stat(new_wb, WB_WRITEBACK); 396d10c8095STejun Heo } 397d10c8095STejun Heo } 398d10c8095STejun Heo 399d10c8095STejun Heo wb_get(new_wb); 400d10c8095STejun Heo 401d10c8095STejun Heo /* 402d10c8095STejun Heo * Transfer to @new_wb's IO list if necessary. The specific list 403d10c8095STejun Heo * @inode was on is ignored and the inode is put on ->b_dirty which 404d10c8095STejun Heo * is always correct including from ->b_dirty_time. The transfer 405d10c8095STejun Heo * preserves @inode->dirtied_when ordering. 406d10c8095STejun Heo */ 407c7f54084SDave Chinner if (!list_empty(&inode->i_io_list)) { 408d10c8095STejun Heo struct inode *pos; 409d10c8095STejun Heo 410c7f54084SDave Chinner inode_io_list_del_locked(inode, old_wb); 411d10c8095STejun Heo inode->i_wb = new_wb; 412c7f54084SDave Chinner list_for_each_entry(pos, &new_wb->b_dirty, i_io_list) 413d10c8095STejun Heo if (time_after_eq(inode->dirtied_when, 414d10c8095STejun Heo pos->dirtied_when)) 415d10c8095STejun Heo break; 416c7f54084SDave Chinner inode_io_list_move_locked(inode, new_wb, pos->i_io_list.prev); 417d10c8095STejun Heo } else { 418d10c8095STejun Heo inode->i_wb = new_wb; 419d10c8095STejun Heo } 420d10c8095STejun Heo 421d10c8095STejun Heo /* ->i_wb_frn updates may race wbc_detach_inode() but doesn't matter */ 422682aa8e1STejun Heo inode->i_wb_frn_winner = 0; 423682aa8e1STejun Heo inode->i_wb_frn_avg_time = 0; 424682aa8e1STejun Heo inode->i_wb_frn_history = 0; 425d10c8095STejun Heo switched = true; 426d10c8095STejun Heo skip_switch: 427682aa8e1STejun Heo /* 428682aa8e1STejun Heo * Paired with load_acquire in unlocked_inode_to_wb_begin() and 429682aa8e1STejun Heo * ensures that the new wb is visible if they see !I_WB_SWITCH. 430682aa8e1STejun Heo */ 431682aa8e1STejun Heo smp_store_release(&inode->i_state, inode->i_state & ~I_WB_SWITCH); 432682aa8e1STejun Heo 433d10c8095STejun Heo spin_unlock_irq(&mapping->tree_lock); 434682aa8e1STejun Heo spin_unlock(&inode->i_lock); 435d10c8095STejun Heo spin_unlock(&new_wb->list_lock); 436d10c8095STejun Heo spin_unlock(&old_wb->list_lock); 437d10c8095STejun Heo 438d10c8095STejun Heo if (switched) { 439d10c8095STejun Heo wb_wakeup(new_wb); 440d10c8095STejun Heo wb_put(old_wb); 441d10c8095STejun Heo } 442d10c8095STejun Heo wb_put(new_wb); 443682aa8e1STejun Heo 444682aa8e1STejun Heo iput(inode); 445682aa8e1STejun Heo kfree(isw); 446a1a0e23eSTejun Heo 447a1a0e23eSTejun Heo atomic_dec(&isw_nr_in_flight); 448682aa8e1STejun Heo } 449682aa8e1STejun Heo 450682aa8e1STejun Heo static void inode_switch_wbs_rcu_fn(struct rcu_head *rcu_head) 451682aa8e1STejun Heo { 452682aa8e1STejun Heo struct inode_switch_wbs_context *isw = container_of(rcu_head, 453682aa8e1STejun Heo struct inode_switch_wbs_context, rcu_head); 454682aa8e1STejun Heo 455682aa8e1STejun Heo /* needs to grab bh-unsafe locks, bounce to work item */ 456682aa8e1STejun Heo INIT_WORK(&isw->work, inode_switch_wbs_work_fn); 457a1a0e23eSTejun Heo queue_work(isw_wq, &isw->work); 458682aa8e1STejun Heo } 459682aa8e1STejun Heo 460682aa8e1STejun Heo /** 461682aa8e1STejun Heo * inode_switch_wbs - change the wb association of an inode 462682aa8e1STejun Heo * @inode: target inode 463682aa8e1STejun Heo * @new_wb_id: ID of the new wb 464682aa8e1STejun Heo * 465682aa8e1STejun Heo * Switch @inode's wb association to the wb identified by @new_wb_id. The 466682aa8e1STejun Heo * switching is performed asynchronously and may fail silently. 467682aa8e1STejun Heo */ 468682aa8e1STejun Heo static void inode_switch_wbs(struct inode *inode, int new_wb_id) 469682aa8e1STejun Heo { 470682aa8e1STejun Heo struct backing_dev_info *bdi = inode_to_bdi(inode); 471682aa8e1STejun Heo struct cgroup_subsys_state *memcg_css; 472682aa8e1STejun Heo struct inode_switch_wbs_context *isw; 473682aa8e1STejun Heo 474682aa8e1STejun Heo /* noop if seems to be already in progress */ 475682aa8e1STejun Heo if (inode->i_state & I_WB_SWITCH) 476682aa8e1STejun Heo return; 477682aa8e1STejun Heo 478682aa8e1STejun Heo isw = kzalloc(sizeof(*isw), GFP_ATOMIC); 479682aa8e1STejun Heo if (!isw) 480682aa8e1STejun Heo return; 481682aa8e1STejun Heo 482682aa8e1STejun Heo /* find and pin the new wb */ 483682aa8e1STejun Heo rcu_read_lock(); 484682aa8e1STejun Heo memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys); 485682aa8e1STejun Heo if (memcg_css) 486682aa8e1STejun Heo isw->new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 487682aa8e1STejun Heo rcu_read_unlock(); 488682aa8e1STejun Heo if (!isw->new_wb) 489682aa8e1STejun Heo goto out_free; 490682aa8e1STejun Heo 491682aa8e1STejun Heo /* while holding I_WB_SWITCH, no one else can update the association */ 492682aa8e1STejun Heo spin_lock(&inode->i_lock); 4931751e8a6SLinus Torvalds if (!(inode->i_sb->s_flags & SB_ACTIVE) || 494a1a0e23eSTejun Heo inode->i_state & (I_WB_SWITCH | I_FREEING) || 495a1a0e23eSTejun Heo inode_to_wb(inode) == isw->new_wb) { 496a1a0e23eSTejun Heo spin_unlock(&inode->i_lock); 497a1a0e23eSTejun Heo goto out_free; 498a1a0e23eSTejun Heo } 499682aa8e1STejun Heo inode->i_state |= I_WB_SWITCH; 50074524955STahsin Erdogan __iget(inode); 501682aa8e1STejun Heo spin_unlock(&inode->i_lock); 502682aa8e1STejun Heo 503682aa8e1STejun Heo isw->inode = inode; 504682aa8e1STejun Heo 505a1a0e23eSTejun Heo atomic_inc(&isw_nr_in_flight); 506a1a0e23eSTejun Heo 507682aa8e1STejun Heo /* 508682aa8e1STejun Heo * In addition to synchronizing among switchers, I_WB_SWITCH tells 509682aa8e1STejun Heo * the RCU protected stat update paths to grab the mapping's 510682aa8e1STejun Heo * tree_lock so that stat transfer can synchronize against them. 511682aa8e1STejun Heo * Let's continue after I_WB_SWITCH is guaranteed to be visible. 512682aa8e1STejun Heo */ 513682aa8e1STejun Heo call_rcu(&isw->rcu_head, inode_switch_wbs_rcu_fn); 514682aa8e1STejun Heo return; 515682aa8e1STejun Heo 516682aa8e1STejun Heo out_free: 517682aa8e1STejun Heo if (isw->new_wb) 518682aa8e1STejun Heo wb_put(isw->new_wb); 519682aa8e1STejun Heo kfree(isw); 520682aa8e1STejun Heo } 521682aa8e1STejun Heo 52287e1d789STejun Heo /** 523b16b1debSTejun Heo * wbc_attach_and_unlock_inode - associate wbc with target inode and unlock it 524b16b1debSTejun Heo * @wbc: writeback_control of interest 525b16b1debSTejun Heo * @inode: target inode 526b16b1debSTejun Heo * 527b16b1debSTejun Heo * @inode is locked and about to be written back under the control of @wbc. 528b16b1debSTejun Heo * Record @inode's writeback context into @wbc and unlock the i_lock. On 529b16b1debSTejun Heo * writeback completion, wbc_detach_inode() should be called. This is used 530b16b1debSTejun Heo * to track the cgroup writeback context. 531b16b1debSTejun Heo */ 532b16b1debSTejun Heo void wbc_attach_and_unlock_inode(struct writeback_control *wbc, 533b16b1debSTejun Heo struct inode *inode) 534b16b1debSTejun Heo { 535dd73e4b7STejun Heo if (!inode_cgwb_enabled(inode)) { 536dd73e4b7STejun Heo spin_unlock(&inode->i_lock); 537dd73e4b7STejun Heo return; 538dd73e4b7STejun Heo } 539dd73e4b7STejun Heo 540b16b1debSTejun Heo wbc->wb = inode_to_wb(inode); 5412a814908STejun Heo wbc->inode = inode; 5422a814908STejun Heo 5432a814908STejun Heo wbc->wb_id = wbc->wb->memcg_css->id; 5442a814908STejun Heo wbc->wb_lcand_id = inode->i_wb_frn_winner; 5452a814908STejun Heo wbc->wb_tcand_id = 0; 5462a814908STejun Heo wbc->wb_bytes = 0; 5472a814908STejun Heo wbc->wb_lcand_bytes = 0; 5482a814908STejun Heo wbc->wb_tcand_bytes = 0; 5492a814908STejun Heo 550b16b1debSTejun Heo wb_get(wbc->wb); 551b16b1debSTejun Heo spin_unlock(&inode->i_lock); 552e8a7abf5STejun Heo 553e8a7abf5STejun Heo /* 554e8a7abf5STejun Heo * A dying wb indicates that the memcg-blkcg mapping has changed 555e8a7abf5STejun Heo * and a new wb is already serving the memcg. Switch immediately. 556e8a7abf5STejun Heo */ 557e8a7abf5STejun Heo if (unlikely(wb_dying(wbc->wb))) 558e8a7abf5STejun Heo inode_switch_wbs(inode, wbc->wb_id); 559b16b1debSTejun Heo } 560b16b1debSTejun Heo 561b16b1debSTejun Heo /** 5622a814908STejun Heo * wbc_detach_inode - disassociate wbc from inode and perform foreign detection 5632a814908STejun Heo * @wbc: writeback_control of the just finished writeback 564b16b1debSTejun Heo * 565b16b1debSTejun Heo * To be called after a writeback attempt of an inode finishes and undoes 566b16b1debSTejun Heo * wbc_attach_and_unlock_inode(). Can be called under any context. 5672a814908STejun Heo * 5682a814908STejun Heo * As concurrent write sharing of an inode is expected to be very rare and 5692a814908STejun Heo * memcg only tracks page ownership on first-use basis severely confining 5702a814908STejun Heo * the usefulness of such sharing, cgroup writeback tracks ownership 5712a814908STejun Heo * per-inode. While the support for concurrent write sharing of an inode 5722a814908STejun Heo * is deemed unnecessary, an inode being written to by different cgroups at 5732a814908STejun Heo * different points in time is a lot more common, and, more importantly, 5742a814908STejun Heo * charging only by first-use can too readily lead to grossly incorrect 5752a814908STejun Heo * behaviors (single foreign page can lead to gigabytes of writeback to be 5762a814908STejun Heo * incorrectly attributed). 5772a814908STejun Heo * 5782a814908STejun Heo * To resolve this issue, cgroup writeback detects the majority dirtier of 5792a814908STejun Heo * an inode and transfers the ownership to it. To avoid unnnecessary 5802a814908STejun Heo * oscillation, the detection mechanism keeps track of history and gives 5812a814908STejun Heo * out the switch verdict only if the foreign usage pattern is stable over 5822a814908STejun Heo * a certain amount of time and/or writeback attempts. 5832a814908STejun Heo * 5842a814908STejun Heo * On each writeback attempt, @wbc tries to detect the majority writer 5852a814908STejun Heo * using Boyer-Moore majority vote algorithm. In addition to the byte 5862a814908STejun Heo * count from the majority voting, it also counts the bytes written for the 5872a814908STejun Heo * current wb and the last round's winner wb (max of last round's current 5882a814908STejun Heo * wb, the winner from two rounds ago, and the last round's majority 5892a814908STejun Heo * candidate). Keeping track of the historical winner helps the algorithm 5902a814908STejun Heo * to semi-reliably detect the most active writer even when it's not the 5912a814908STejun Heo * absolute majority. 5922a814908STejun Heo * 5932a814908STejun Heo * Once the winner of the round is determined, whether the winner is 5942a814908STejun Heo * foreign or not and how much IO time the round consumed is recorded in 5952a814908STejun Heo * inode->i_wb_frn_history. If the amount of recorded foreign IO time is 5962a814908STejun Heo * over a certain threshold, the switch verdict is given. 597b16b1debSTejun Heo */ 598b16b1debSTejun Heo void wbc_detach_inode(struct writeback_control *wbc) 599b16b1debSTejun Heo { 6002a814908STejun Heo struct bdi_writeback *wb = wbc->wb; 6012a814908STejun Heo struct inode *inode = wbc->inode; 602dd73e4b7STejun Heo unsigned long avg_time, max_bytes, max_time; 603dd73e4b7STejun Heo u16 history; 6042a814908STejun Heo int max_id; 6052a814908STejun Heo 606dd73e4b7STejun Heo if (!wb) 607dd73e4b7STejun Heo return; 608dd73e4b7STejun Heo 609dd73e4b7STejun Heo history = inode->i_wb_frn_history; 610dd73e4b7STejun Heo avg_time = inode->i_wb_frn_avg_time; 611dd73e4b7STejun Heo 6122a814908STejun Heo /* pick the winner of this round */ 6132a814908STejun Heo if (wbc->wb_bytes >= wbc->wb_lcand_bytes && 6142a814908STejun Heo wbc->wb_bytes >= wbc->wb_tcand_bytes) { 6152a814908STejun Heo max_id = wbc->wb_id; 6162a814908STejun Heo max_bytes = wbc->wb_bytes; 6172a814908STejun Heo } else if (wbc->wb_lcand_bytes >= wbc->wb_tcand_bytes) { 6182a814908STejun Heo max_id = wbc->wb_lcand_id; 6192a814908STejun Heo max_bytes = wbc->wb_lcand_bytes; 6202a814908STejun Heo } else { 6212a814908STejun Heo max_id = wbc->wb_tcand_id; 6222a814908STejun Heo max_bytes = wbc->wb_tcand_bytes; 6232a814908STejun Heo } 6242a814908STejun Heo 6252a814908STejun Heo /* 6262a814908STejun Heo * Calculate the amount of IO time the winner consumed and fold it 6272a814908STejun Heo * into the running average kept per inode. If the consumed IO 6282a814908STejun Heo * time is lower than avag / WB_FRN_TIME_CUT_DIV, ignore it for 6292a814908STejun Heo * deciding whether to switch or not. This is to prevent one-off 6302a814908STejun Heo * small dirtiers from skewing the verdict. 6312a814908STejun Heo */ 6322a814908STejun Heo max_time = DIV_ROUND_UP((max_bytes >> PAGE_SHIFT) << WB_FRN_TIME_SHIFT, 6332a814908STejun Heo wb->avg_write_bandwidth); 6342a814908STejun Heo if (avg_time) 6352a814908STejun Heo avg_time += (max_time >> WB_FRN_TIME_AVG_SHIFT) - 6362a814908STejun Heo (avg_time >> WB_FRN_TIME_AVG_SHIFT); 6372a814908STejun Heo else 6382a814908STejun Heo avg_time = max_time; /* immediate catch up on first run */ 6392a814908STejun Heo 6402a814908STejun Heo if (max_time >= avg_time / WB_FRN_TIME_CUT_DIV) { 6412a814908STejun Heo int slots; 6422a814908STejun Heo 6432a814908STejun Heo /* 6442a814908STejun Heo * The switch verdict is reached if foreign wb's consume 6452a814908STejun Heo * more than a certain proportion of IO time in a 6462a814908STejun Heo * WB_FRN_TIME_PERIOD. This is loosely tracked by 16 slot 6472a814908STejun Heo * history mask where each bit represents one sixteenth of 6482a814908STejun Heo * the period. Determine the number of slots to shift into 6492a814908STejun Heo * history from @max_time. 6502a814908STejun Heo */ 6512a814908STejun Heo slots = min(DIV_ROUND_UP(max_time, WB_FRN_HIST_UNIT), 6522a814908STejun Heo (unsigned long)WB_FRN_HIST_MAX_SLOTS); 6532a814908STejun Heo history <<= slots; 6542a814908STejun Heo if (wbc->wb_id != max_id) 6552a814908STejun Heo history |= (1U << slots) - 1; 6562a814908STejun Heo 6572a814908STejun Heo /* 6582a814908STejun Heo * Switch if the current wb isn't the consistent winner. 6592a814908STejun Heo * If there are multiple closely competing dirtiers, the 6602a814908STejun Heo * inode may switch across them repeatedly over time, which 6612a814908STejun Heo * is okay. The main goal is avoiding keeping an inode on 6622a814908STejun Heo * the wrong wb for an extended period of time. 6632a814908STejun Heo */ 664682aa8e1STejun Heo if (hweight32(history) > WB_FRN_HIST_THR_SLOTS) 665682aa8e1STejun Heo inode_switch_wbs(inode, max_id); 6662a814908STejun Heo } 6672a814908STejun Heo 6682a814908STejun Heo /* 6692a814908STejun Heo * Multiple instances of this function may race to update the 6702a814908STejun Heo * following fields but we don't mind occassional inaccuracies. 6712a814908STejun Heo */ 6722a814908STejun Heo inode->i_wb_frn_winner = max_id; 6732a814908STejun Heo inode->i_wb_frn_avg_time = min(avg_time, (unsigned long)U16_MAX); 6742a814908STejun Heo inode->i_wb_frn_history = history; 6752a814908STejun Heo 676b16b1debSTejun Heo wb_put(wbc->wb); 677b16b1debSTejun Heo wbc->wb = NULL; 678b16b1debSTejun Heo } 679b16b1debSTejun Heo 680b16b1debSTejun Heo /** 6812a814908STejun Heo * wbc_account_io - account IO issued during writeback 6822a814908STejun Heo * @wbc: writeback_control of the writeback in progress 6832a814908STejun Heo * @page: page being written out 6842a814908STejun Heo * @bytes: number of bytes being written out 6852a814908STejun Heo * 6862a814908STejun Heo * @bytes from @page are about to written out during the writeback 6872a814908STejun Heo * controlled by @wbc. Keep the book for foreign inode detection. See 6882a814908STejun Heo * wbc_detach_inode(). 6892a814908STejun Heo */ 6902a814908STejun Heo void wbc_account_io(struct writeback_control *wbc, struct page *page, 6912a814908STejun Heo size_t bytes) 6922a814908STejun Heo { 6932a814908STejun Heo int id; 6942a814908STejun Heo 6952a814908STejun Heo /* 6962a814908STejun Heo * pageout() path doesn't attach @wbc to the inode being written 6972a814908STejun Heo * out. This is intentional as we don't want the function to block 6982a814908STejun Heo * behind a slow cgroup. Ultimately, we want pageout() to kick off 6992a814908STejun Heo * regular writeback instead of writing things out itself. 7002a814908STejun Heo */ 7012a814908STejun Heo if (!wbc->wb) 7022a814908STejun Heo return; 7032a814908STejun Heo 7042a814908STejun Heo id = mem_cgroup_css_from_page(page)->id; 7052a814908STejun Heo 7062a814908STejun Heo if (id == wbc->wb_id) { 7072a814908STejun Heo wbc->wb_bytes += bytes; 7082a814908STejun Heo return; 7092a814908STejun Heo } 7102a814908STejun Heo 7112a814908STejun Heo if (id == wbc->wb_lcand_id) 7122a814908STejun Heo wbc->wb_lcand_bytes += bytes; 7132a814908STejun Heo 7142a814908STejun Heo /* Boyer-Moore majority vote algorithm */ 7152a814908STejun Heo if (!wbc->wb_tcand_bytes) 7162a814908STejun Heo wbc->wb_tcand_id = id; 7172a814908STejun Heo if (id == wbc->wb_tcand_id) 7182a814908STejun Heo wbc->wb_tcand_bytes += bytes; 7192a814908STejun Heo else 7202a814908STejun Heo wbc->wb_tcand_bytes -= min(bytes, wbc->wb_tcand_bytes); 7212a814908STejun Heo } 7225aa2a96bSTejun Heo EXPORT_SYMBOL_GPL(wbc_account_io); 7232a814908STejun Heo 7242a814908STejun Heo /** 725703c2708STejun Heo * inode_congested - test whether an inode is congested 72660292bccSTejun Heo * @inode: inode to test for congestion (may be NULL) 727703c2708STejun Heo * @cong_bits: mask of WB_[a]sync_congested bits to test 728703c2708STejun Heo * 729703c2708STejun Heo * Tests whether @inode is congested. @cong_bits is the mask of congestion 730703c2708STejun Heo * bits to test and the return value is the mask of set bits. 731703c2708STejun Heo * 732703c2708STejun Heo * If cgroup writeback is enabled for @inode, the congestion state is 733703c2708STejun Heo * determined by whether the cgwb (cgroup bdi_writeback) for the blkcg 734703c2708STejun Heo * associated with @inode is congested; otherwise, the root wb's congestion 735703c2708STejun Heo * state is used. 73660292bccSTejun Heo * 73760292bccSTejun Heo * @inode is allowed to be NULL as this function is often called on 73860292bccSTejun Heo * mapping->host which is NULL for the swapper space. 739703c2708STejun Heo */ 740703c2708STejun Heo int inode_congested(struct inode *inode, int cong_bits) 741703c2708STejun Heo { 7425cb8b824STejun Heo /* 7435cb8b824STejun Heo * Once set, ->i_wb never becomes NULL while the inode is alive. 7445cb8b824STejun Heo * Start transaction iff ->i_wb is visible. 7455cb8b824STejun Heo */ 746aaa2cacfSTejun Heo if (inode && inode_to_wb_is_valid(inode)) { 7475cb8b824STejun Heo struct bdi_writeback *wb; 7485cb8b824STejun Heo bool locked, congested; 7495cb8b824STejun Heo 7505cb8b824STejun Heo wb = unlocked_inode_to_wb_begin(inode, &locked); 7515cb8b824STejun Heo congested = wb_congested(wb, cong_bits); 7525cb8b824STejun Heo unlocked_inode_to_wb_end(inode, locked); 7535cb8b824STejun Heo return congested; 754703c2708STejun Heo } 755703c2708STejun Heo 756703c2708STejun Heo return wb_congested(&inode_to_bdi(inode)->wb, cong_bits); 757703c2708STejun Heo } 758703c2708STejun Heo EXPORT_SYMBOL_GPL(inode_congested); 759703c2708STejun Heo 760f2b65121STejun Heo /** 761f2b65121STejun Heo * wb_split_bdi_pages - split nr_pages to write according to bandwidth 762f2b65121STejun Heo * @wb: target bdi_writeback to split @nr_pages to 763f2b65121STejun Heo * @nr_pages: number of pages to write for the whole bdi 764f2b65121STejun Heo * 765f2b65121STejun Heo * Split @wb's portion of @nr_pages according to @wb's write bandwidth in 766f2b65121STejun Heo * relation to the total write bandwidth of all wb's w/ dirty inodes on 767f2b65121STejun Heo * @wb->bdi. 768f2b65121STejun Heo */ 769f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) 770f2b65121STejun Heo { 771f2b65121STejun Heo unsigned long this_bw = wb->avg_write_bandwidth; 772f2b65121STejun Heo unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth); 773f2b65121STejun Heo 774f2b65121STejun Heo if (nr_pages == LONG_MAX) 775f2b65121STejun Heo return LONG_MAX; 776f2b65121STejun Heo 777f2b65121STejun Heo /* 778f2b65121STejun Heo * This may be called on clean wb's and proportional distribution 779f2b65121STejun Heo * may not make sense, just use the original @nr_pages in those 780f2b65121STejun Heo * cases. In general, we wanna err on the side of writing more. 781f2b65121STejun Heo */ 782f2b65121STejun Heo if (!tot_bw || this_bw >= tot_bw) 783f2b65121STejun Heo return nr_pages; 784f2b65121STejun Heo else 785f2b65121STejun Heo return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw); 786f2b65121STejun Heo } 787f2b65121STejun Heo 788db125360STejun Heo /** 789db125360STejun Heo * bdi_split_work_to_wbs - split a wb_writeback_work to all wb's of a bdi 790db125360STejun Heo * @bdi: target backing_dev_info 791db125360STejun Heo * @base_work: wb_writeback_work to issue 792db125360STejun Heo * @skip_if_busy: skip wb's which already have writeback in progress 793db125360STejun Heo * 794db125360STejun Heo * Split and issue @base_work to all wb's (bdi_writeback's) of @bdi which 795db125360STejun Heo * have dirty inodes. If @base_work->nr_page isn't %LONG_MAX, it's 796db125360STejun Heo * distributed to the busy wbs according to each wb's proportion in the 797db125360STejun Heo * total active write bandwidth of @bdi. 798db125360STejun Heo */ 799db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, 800db125360STejun Heo struct wb_writeback_work *base_work, 801db125360STejun Heo bool skip_if_busy) 802db125360STejun Heo { 803b817525aSTejun Heo struct bdi_writeback *last_wb = NULL; 804b33e18f6STejun Heo struct bdi_writeback *wb = list_entry(&bdi->wb_list, 805b817525aSTejun Heo struct bdi_writeback, bdi_node); 806db125360STejun Heo 807db125360STejun Heo might_sleep(); 808db125360STejun Heo restart: 809db125360STejun Heo rcu_read_lock(); 810b817525aSTejun Heo list_for_each_entry_continue_rcu(wb, &bdi->wb_list, bdi_node) { 8118a1270cdSTejun Heo DEFINE_WB_COMPLETION_ONSTACK(fallback_work_done); 8128a1270cdSTejun Heo struct wb_writeback_work fallback_work; 8138a1270cdSTejun Heo struct wb_writeback_work *work; 8148a1270cdSTejun Heo long nr_pages; 8158a1270cdSTejun Heo 816b817525aSTejun Heo if (last_wb) { 817b817525aSTejun Heo wb_put(last_wb); 818b817525aSTejun Heo last_wb = NULL; 819b817525aSTejun Heo } 820b817525aSTejun Heo 821006a0973STejun Heo /* SYNC_ALL writes out I_DIRTY_TIME too */ 822006a0973STejun Heo if (!wb_has_dirty_io(wb) && 823006a0973STejun Heo (base_work->sync_mode == WB_SYNC_NONE || 824006a0973STejun Heo list_empty(&wb->b_dirty_time))) 825006a0973STejun Heo continue; 826006a0973STejun Heo if (skip_if_busy && writeback_in_progress(wb)) 827db125360STejun Heo continue; 828db125360STejun Heo 8298a1270cdSTejun Heo nr_pages = wb_split_bdi_pages(wb, base_work->nr_pages); 8308a1270cdSTejun Heo 8318a1270cdSTejun Heo work = kmalloc(sizeof(*work), GFP_ATOMIC); 8328a1270cdSTejun Heo if (work) { 8338a1270cdSTejun Heo *work = *base_work; 8348a1270cdSTejun Heo work->nr_pages = nr_pages; 8358a1270cdSTejun Heo work->auto_free = 1; 8368a1270cdSTejun Heo wb_queue_work(wb, work); 8378a1270cdSTejun Heo continue; 838db125360STejun Heo } 8398a1270cdSTejun Heo 8408a1270cdSTejun Heo /* alloc failed, execute synchronously using on-stack fallback */ 8418a1270cdSTejun Heo work = &fallback_work; 8428a1270cdSTejun Heo *work = *base_work; 8438a1270cdSTejun Heo work->nr_pages = nr_pages; 8448a1270cdSTejun Heo work->auto_free = 0; 8458a1270cdSTejun Heo work->done = &fallback_work_done; 8468a1270cdSTejun Heo 8478a1270cdSTejun Heo wb_queue_work(wb, work); 8488a1270cdSTejun Heo 849b817525aSTejun Heo /* 850b817525aSTejun Heo * Pin @wb so that it stays on @bdi->wb_list. This allows 851b817525aSTejun Heo * continuing iteration from @wb after dropping and 852b817525aSTejun Heo * regrabbing rcu read lock. 853b817525aSTejun Heo */ 854b817525aSTejun Heo wb_get(wb); 855b817525aSTejun Heo last_wb = wb; 856b817525aSTejun Heo 857db125360STejun Heo rcu_read_unlock(); 8588a1270cdSTejun Heo wb_wait_for_completion(bdi, &fallback_work_done); 859db125360STejun Heo goto restart; 860db125360STejun Heo } 861db125360STejun Heo rcu_read_unlock(); 862b817525aSTejun Heo 863b817525aSTejun Heo if (last_wb) 864b817525aSTejun Heo wb_put(last_wb); 865db125360STejun Heo } 866db125360STejun Heo 867a1a0e23eSTejun Heo /** 868a1a0e23eSTejun Heo * cgroup_writeback_umount - flush inode wb switches for umount 869a1a0e23eSTejun Heo * 870a1a0e23eSTejun Heo * This function is called when a super_block is about to be destroyed and 871a1a0e23eSTejun Heo * flushes in-flight inode wb switches. An inode wb switch goes through 872a1a0e23eSTejun Heo * RCU and then workqueue, so the two need to be flushed in order to ensure 873a1a0e23eSTejun Heo * that all previously scheduled switches are finished. As wb switches are 874a1a0e23eSTejun Heo * rare occurrences and synchronize_rcu() can take a while, perform 875a1a0e23eSTejun Heo * flushing iff wb switches are in flight. 876a1a0e23eSTejun Heo */ 877a1a0e23eSTejun Heo void cgroup_writeback_umount(void) 878a1a0e23eSTejun Heo { 879a1a0e23eSTejun Heo if (atomic_read(&isw_nr_in_flight)) { 880a1a0e23eSTejun Heo synchronize_rcu(); 881a1a0e23eSTejun Heo flush_workqueue(isw_wq); 882a1a0e23eSTejun Heo } 883a1a0e23eSTejun Heo } 884a1a0e23eSTejun Heo 885a1a0e23eSTejun Heo static int __init cgroup_writeback_init(void) 886a1a0e23eSTejun Heo { 887a1a0e23eSTejun Heo isw_wq = alloc_workqueue("inode_switch_wbs", 0, 0); 888a1a0e23eSTejun Heo if (!isw_wq) 889a1a0e23eSTejun Heo return -ENOMEM; 890a1a0e23eSTejun Heo return 0; 891a1a0e23eSTejun Heo } 892a1a0e23eSTejun Heo fs_initcall(cgroup_writeback_init); 893a1a0e23eSTejun Heo 894f2b65121STejun Heo #else /* CONFIG_CGROUP_WRITEBACK */ 895f2b65121STejun Heo 89687e1d789STejun Heo static struct bdi_writeback * 89787e1d789STejun Heo locked_inode_to_wb_and_lock_list(struct inode *inode) 89887e1d789STejun Heo __releases(&inode->i_lock) 89987e1d789STejun Heo __acquires(&wb->list_lock) 90087e1d789STejun Heo { 90187e1d789STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 90287e1d789STejun Heo 90387e1d789STejun Heo spin_unlock(&inode->i_lock); 90487e1d789STejun Heo spin_lock(&wb->list_lock); 90587e1d789STejun Heo return wb; 90687e1d789STejun Heo } 90787e1d789STejun Heo 90887e1d789STejun Heo static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode) 90987e1d789STejun Heo __acquires(&wb->list_lock) 91087e1d789STejun Heo { 91187e1d789STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 91287e1d789STejun Heo 91387e1d789STejun Heo spin_lock(&wb->list_lock); 91487e1d789STejun Heo return wb; 91587e1d789STejun Heo } 91687e1d789STejun Heo 917f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) 918f2b65121STejun Heo { 919f2b65121STejun Heo return nr_pages; 920f2b65121STejun Heo } 921f2b65121STejun Heo 922db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, 923db125360STejun Heo struct wb_writeback_work *base_work, 924db125360STejun Heo bool skip_if_busy) 925db125360STejun Heo { 926db125360STejun Heo might_sleep(); 927db125360STejun Heo 928006a0973STejun Heo if (!skip_if_busy || !writeback_in_progress(&bdi->wb)) { 929db125360STejun Heo base_work->auto_free = 0; 930db125360STejun Heo wb_queue_work(&bdi->wb, base_work); 931db125360STejun Heo } 932db125360STejun Heo } 933db125360STejun Heo 934703c2708STejun Heo #endif /* CONFIG_CGROUP_WRITEBACK */ 935703c2708STejun Heo 936e8e8a0c6SJens Axboe /* 937e8e8a0c6SJens Axboe * Add in the number of potentially dirty inodes, because each inode 938e8e8a0c6SJens Axboe * write can dirty pagecache in the underlying blockdev. 939e8e8a0c6SJens Axboe */ 940e8e8a0c6SJens Axboe static unsigned long get_nr_dirty_pages(void) 941e8e8a0c6SJens Axboe { 942e8e8a0c6SJens Axboe return global_node_page_state(NR_FILE_DIRTY) + 943e8e8a0c6SJens Axboe global_node_page_state(NR_UNSTABLE_NFS) + 944e8e8a0c6SJens Axboe get_nr_dirty_inodes(); 945e8e8a0c6SJens Axboe } 946e8e8a0c6SJens Axboe 947e8e8a0c6SJens Axboe static void wb_start_writeback(struct bdi_writeback *wb, enum wb_reason reason) 948b6e51316SJens Axboe { 949c00ddad3STejun Heo if (!wb_has_dirty_io(wb)) 950c00ddad3STejun Heo return; 951c00ddad3STejun Heo 952c00ddad3STejun Heo /* 953aac8d41cSJens Axboe * All callers of this function want to start writeback of all 954aac8d41cSJens Axboe * dirty pages. Places like vmscan can call this at a very 955aac8d41cSJens Axboe * high frequency, causing pointless allocations of tons of 956aac8d41cSJens Axboe * work items and keeping the flusher threads busy retrieving 957aac8d41cSJens Axboe * that work. Ensure that we only allow one of them pending and 95885009b4fSJens Axboe * inflight at the time. 959aac8d41cSJens Axboe */ 96085009b4fSJens Axboe if (test_bit(WB_start_all, &wb->state) || 96185009b4fSJens Axboe test_and_set_bit(WB_start_all, &wb->state)) 962aac8d41cSJens Axboe return; 963aac8d41cSJens Axboe 96485009b4fSJens Axboe wb->start_all_reason = reason; 965c00ddad3STejun Heo wb_wakeup(wb); 966d3ddec76SWu Fengguang } 967d3ddec76SWu Fengguang 968c5444198SChristoph Hellwig /** 9699ecf4866STejun Heo * wb_start_background_writeback - start background writeback 9709ecf4866STejun Heo * @wb: bdi_writback to write from 971c5444198SChristoph Hellwig * 972c5444198SChristoph Hellwig * Description: 9736585027aSJan Kara * This makes sure WB_SYNC_NONE background writeback happens. When 9749ecf4866STejun Heo * this function returns, it is only guaranteed that for given wb 9756585027aSJan Kara * some IO is happening if we are over background dirty threshold. 9766585027aSJan Kara * Caller need not hold sb s_umount semaphore. 977c5444198SChristoph Hellwig */ 9789ecf4866STejun Heo void wb_start_background_writeback(struct bdi_writeback *wb) 979c5444198SChristoph Hellwig { 9806585027aSJan Kara /* 9816585027aSJan Kara * We just wake up the flusher thread. It will perform background 9826585027aSJan Kara * writeback as soon as there is no other work to do. 9836585027aSJan Kara */ 9845634cc2aSTejun Heo trace_writeback_wake_background(wb); 9859ecf4866STejun Heo wb_wakeup(wb); 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds /* 989a66979abSDave Chinner * Remove the inode from the writeback list it is on. 990a66979abSDave Chinner */ 991c7f54084SDave Chinner void inode_io_list_del(struct inode *inode) 992a66979abSDave Chinner { 99387e1d789STejun Heo struct bdi_writeback *wb; 994a66979abSDave Chinner 99587e1d789STejun Heo wb = inode_to_wb_and_lock_list(inode); 996c7f54084SDave Chinner inode_io_list_del_locked(inode, wb); 99752ebea74STejun Heo spin_unlock(&wb->list_lock); 998f758eeabSChristoph Hellwig } 999a66979abSDave Chinner 1000a66979abSDave Chinner /* 10016c60d2b5SDave Chinner * mark an inode as under writeback on the sb 10026c60d2b5SDave Chinner */ 10036c60d2b5SDave Chinner void sb_mark_inode_writeback(struct inode *inode) 10046c60d2b5SDave Chinner { 10056c60d2b5SDave Chinner struct super_block *sb = inode->i_sb; 10066c60d2b5SDave Chinner unsigned long flags; 10076c60d2b5SDave Chinner 10086c60d2b5SDave Chinner if (list_empty(&inode->i_wb_list)) { 10096c60d2b5SDave Chinner spin_lock_irqsave(&sb->s_inode_wblist_lock, flags); 10109a46b04fSBrian Foster if (list_empty(&inode->i_wb_list)) { 10116c60d2b5SDave Chinner list_add_tail(&inode->i_wb_list, &sb->s_inodes_wb); 10129a46b04fSBrian Foster trace_sb_mark_inode_writeback(inode); 10139a46b04fSBrian Foster } 10146c60d2b5SDave Chinner spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags); 10156c60d2b5SDave Chinner } 10166c60d2b5SDave Chinner } 10176c60d2b5SDave Chinner 10186c60d2b5SDave Chinner /* 10196c60d2b5SDave Chinner * clear an inode as under writeback on the sb 10206c60d2b5SDave Chinner */ 10216c60d2b5SDave Chinner void sb_clear_inode_writeback(struct inode *inode) 10226c60d2b5SDave Chinner { 10236c60d2b5SDave Chinner struct super_block *sb = inode->i_sb; 10246c60d2b5SDave Chinner unsigned long flags; 10256c60d2b5SDave Chinner 10266c60d2b5SDave Chinner if (!list_empty(&inode->i_wb_list)) { 10276c60d2b5SDave Chinner spin_lock_irqsave(&sb->s_inode_wblist_lock, flags); 10289a46b04fSBrian Foster if (!list_empty(&inode->i_wb_list)) { 10296c60d2b5SDave Chinner list_del_init(&inode->i_wb_list); 10309a46b04fSBrian Foster trace_sb_clear_inode_writeback(inode); 10319a46b04fSBrian Foster } 10326c60d2b5SDave Chinner spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags); 10336c60d2b5SDave Chinner } 10346c60d2b5SDave Chinner } 10356c60d2b5SDave Chinner 10366c60d2b5SDave Chinner /* 10376610a0bcSAndrew Morton * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 10386610a0bcSAndrew Morton * furthest end of its superblock's dirty-inode list. 10396610a0bcSAndrew Morton * 10406610a0bcSAndrew Morton * Before stamping the inode's ->dirtied_when, we check to see whether it is 104166f3b8e2SJens Axboe * already the most-recently-dirtied inode on the b_dirty list. If that is 10426610a0bcSAndrew Morton * the case then the inode must have been redirtied while it was being written 10436610a0bcSAndrew Morton * out and we don't reset its dirtied_when. 10446610a0bcSAndrew Morton */ 1045f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb) 10466610a0bcSAndrew Morton { 104703ba3782SJens Axboe if (!list_empty(&wb->b_dirty)) { 104866f3b8e2SJens Axboe struct inode *tail; 10496610a0bcSAndrew Morton 10507ccf19a8SNick Piggin tail = wb_inode(wb->b_dirty.next); 105166f3b8e2SJens Axboe if (time_before(inode->dirtied_when, tail->dirtied_when)) 10526610a0bcSAndrew Morton inode->dirtied_when = jiffies; 10536610a0bcSAndrew Morton } 1054c7f54084SDave Chinner inode_io_list_move_locked(inode, wb, &wb->b_dirty); 10556610a0bcSAndrew Morton } 10566610a0bcSAndrew Morton 10576610a0bcSAndrew Morton /* 105866f3b8e2SJens Axboe * requeue inode for re-scanning after bdi->b_io list is exhausted. 1059c986d1e2SAndrew Morton */ 1060f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb) 1061c986d1e2SAndrew Morton { 1062c7f54084SDave Chinner inode_io_list_move_locked(inode, wb, &wb->b_more_io); 1063c986d1e2SAndrew Morton } 1064c986d1e2SAndrew Morton 10651c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode) 10661c0eeaf5SJoern Engel { 1067365b94aeSJan Kara inode->i_state &= ~I_SYNC; 10684eff96ddSJan Kara /* If inode is clean an unused, put it into LRU now... */ 10694eff96ddSJan Kara inode_add_lru(inode); 1070365b94aeSJan Kara /* Waiters must see I_SYNC cleared before being woken up */ 10711c0eeaf5SJoern Engel smp_mb(); 10721c0eeaf5SJoern Engel wake_up_bit(&inode->i_state, __I_SYNC); 10731c0eeaf5SJoern Engel } 10741c0eeaf5SJoern Engel 1075d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t) 1076d2caa3c5SJeff Layton { 1077d2caa3c5SJeff Layton bool ret = time_after(inode->dirtied_when, t); 1078d2caa3c5SJeff Layton #ifndef CONFIG_64BIT 1079d2caa3c5SJeff Layton /* 1080d2caa3c5SJeff Layton * For inodes being constantly redirtied, dirtied_when can get stuck. 1081d2caa3c5SJeff Layton * It _appears_ to be in the future, but is actually in distant past. 1082d2caa3c5SJeff Layton * This test is necessary to prevent such wrapped-around relative times 10835b0830cbSJens Axboe * from permanently stopping the whole bdi writeback. 1084d2caa3c5SJeff Layton */ 1085d2caa3c5SJeff Layton ret = ret && time_before_eq(inode->dirtied_when, jiffies); 1086d2caa3c5SJeff Layton #endif 1087d2caa3c5SJeff Layton return ret; 1088d2caa3c5SJeff Layton } 1089d2caa3c5SJeff Layton 10900ae45f63STheodore Ts'o #define EXPIRE_DIRTY_ATIME 0x0001 10910ae45f63STheodore Ts'o 1092c986d1e2SAndrew Morton /* 10930e2f2b23SWang Sheng-Hui * Move expired (dirtied before work->older_than_this) dirty inodes from 1094697e6fedSJan Kara * @delaying_queue to @dispatch_queue. 10952c136579SFengguang Wu */ 1096e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue, 10972c136579SFengguang Wu struct list_head *dispatch_queue, 10980ae45f63STheodore Ts'o int flags, 1099ad4e38ddSCurt Wohlgemuth struct wb_writeback_work *work) 11002c136579SFengguang Wu { 11010ae45f63STheodore Ts'o unsigned long *older_than_this = NULL; 11020ae45f63STheodore Ts'o unsigned long expire_time; 11035c03449dSShaohua Li LIST_HEAD(tmp); 11045c03449dSShaohua Li struct list_head *pos, *node; 1105cf137307SJens Axboe struct super_block *sb = NULL; 11065c03449dSShaohua Li struct inode *inode; 1107cf137307SJens Axboe int do_sb_sort = 0; 1108e84d0a4fSWu Fengguang int moved = 0; 11095c03449dSShaohua Li 11100ae45f63STheodore Ts'o if ((flags & EXPIRE_DIRTY_ATIME) == 0) 11110ae45f63STheodore Ts'o older_than_this = work->older_than_this; 1112a2f48706STheodore Ts'o else if (!work->for_sync) { 1113a2f48706STheodore Ts'o expire_time = jiffies - (dirtytime_expire_interval * HZ); 11140ae45f63STheodore Ts'o older_than_this = &expire_time; 11150ae45f63STheodore Ts'o } 11162c136579SFengguang Wu while (!list_empty(delaying_queue)) { 11177ccf19a8SNick Piggin inode = wb_inode(delaying_queue->prev); 11180ae45f63STheodore Ts'o if (older_than_this && 11190ae45f63STheodore Ts'o inode_dirtied_after(inode, *older_than_this)) 11202c136579SFengguang Wu break; 1121c7f54084SDave Chinner list_move(&inode->i_io_list, &tmp); 1122a8855990SJan Kara moved++; 11230ae45f63STheodore Ts'o if (flags & EXPIRE_DIRTY_ATIME) 11240ae45f63STheodore Ts'o set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state); 1125a8855990SJan Kara if (sb_is_blkdev_sb(inode->i_sb)) 1126a8855990SJan Kara continue; 1127cf137307SJens Axboe if (sb && sb != inode->i_sb) 1128cf137307SJens Axboe do_sb_sort = 1; 1129cf137307SJens Axboe sb = inode->i_sb; 11305c03449dSShaohua Li } 11315c03449dSShaohua Li 1132cf137307SJens Axboe /* just one sb in list, splice to dispatch_queue and we're done */ 1133cf137307SJens Axboe if (!do_sb_sort) { 1134cf137307SJens Axboe list_splice(&tmp, dispatch_queue); 1135e84d0a4fSWu Fengguang goto out; 1136cf137307SJens Axboe } 1137cf137307SJens Axboe 11385c03449dSShaohua Li /* Move inodes from one superblock together */ 11395c03449dSShaohua Li while (!list_empty(&tmp)) { 11407ccf19a8SNick Piggin sb = wb_inode(tmp.prev)->i_sb; 11415c03449dSShaohua Li list_for_each_prev_safe(pos, node, &tmp) { 11427ccf19a8SNick Piggin inode = wb_inode(pos); 11435c03449dSShaohua Li if (inode->i_sb == sb) 1144c7f54084SDave Chinner list_move(&inode->i_io_list, dispatch_queue); 11452c136579SFengguang Wu } 11462c136579SFengguang Wu } 1147e84d0a4fSWu Fengguang out: 1148e84d0a4fSWu Fengguang return moved; 11495c03449dSShaohua Li } 11502c136579SFengguang Wu 11512c136579SFengguang Wu /* 11522c136579SFengguang Wu * Queue all expired dirty inodes for io, eldest first. 11534ea879b9SWu Fengguang * Before 11544ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 11554ea879b9SWu Fengguang * =============> gf edc BA 11564ea879b9SWu Fengguang * After 11574ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 11584ea879b9SWu Fengguang * =============> g fBAedc 11594ea879b9SWu Fengguang * | 11604ea879b9SWu Fengguang * +--> dequeue for IO 11612c136579SFengguang Wu */ 1162ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work) 11632c136579SFengguang Wu { 1164e84d0a4fSWu Fengguang int moved; 11650ae45f63STheodore Ts'o 1166f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 11674ea879b9SWu Fengguang list_splice_init(&wb->b_more_io, &wb->b_io); 11680ae45f63STheodore Ts'o moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work); 11690ae45f63STheodore Ts'o moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io, 11700ae45f63STheodore Ts'o EXPIRE_DIRTY_ATIME, work); 1171d6c10f1fSTejun Heo if (moved) 1172d6c10f1fSTejun Heo wb_io_lists_populated(wb); 1173ad4e38ddSCurt Wohlgemuth trace_writeback_queue_io(wb, work, moved); 117466f3b8e2SJens Axboe } 117566f3b8e2SJens Axboe 1176a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc) 117766f3b8e2SJens Axboe { 11789fb0a7daSTejun Heo int ret; 11799fb0a7daSTejun Heo 11809fb0a7daSTejun Heo if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) { 11819fb0a7daSTejun Heo trace_writeback_write_inode_start(inode, wbc); 11829fb0a7daSTejun Heo ret = inode->i_sb->s_op->write_inode(inode, wbc); 11839fb0a7daSTejun Heo trace_writeback_write_inode(inode, wbc); 11849fb0a7daSTejun Heo return ret; 11859fb0a7daSTejun Heo } 118603ba3782SJens Axboe return 0; 118766f3b8e2SJens Axboe } 118808d8e974SFengguang Wu 11892c136579SFengguang Wu /* 1190169ebd90SJan Kara * Wait for writeback on an inode to complete. Called with i_lock held. 1191169ebd90SJan Kara * Caller must make sure inode cannot go away when we drop i_lock. 119201c03194SChristoph Hellwig */ 1193169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode) 1194169ebd90SJan Kara __releases(inode->i_lock) 1195169ebd90SJan Kara __acquires(inode->i_lock) 119601c03194SChristoph Hellwig { 119701c03194SChristoph Hellwig DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); 119801c03194SChristoph Hellwig wait_queue_head_t *wqh; 119901c03194SChristoph Hellwig 120001c03194SChristoph Hellwig wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 120158a9d3d8SRichard Kennedy while (inode->i_state & I_SYNC) { 1202250df6edSDave Chinner spin_unlock(&inode->i_lock); 120374316201SNeilBrown __wait_on_bit(wqh, &wq, bit_wait, 120474316201SNeilBrown TASK_UNINTERRUPTIBLE); 1205250df6edSDave Chinner spin_lock(&inode->i_lock); 120658a9d3d8SRichard Kennedy } 120701c03194SChristoph Hellwig } 120801c03194SChristoph Hellwig 120901c03194SChristoph Hellwig /* 1210169ebd90SJan Kara * Wait for writeback on an inode to complete. Caller must have inode pinned. 1211169ebd90SJan Kara */ 1212169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode) 1213169ebd90SJan Kara { 1214169ebd90SJan Kara spin_lock(&inode->i_lock); 1215169ebd90SJan Kara __inode_wait_for_writeback(inode); 1216169ebd90SJan Kara spin_unlock(&inode->i_lock); 1217169ebd90SJan Kara } 1218169ebd90SJan Kara 1219169ebd90SJan Kara /* 1220169ebd90SJan Kara * Sleep until I_SYNC is cleared. This function must be called with i_lock 1221169ebd90SJan Kara * held and drops it. It is aimed for callers not holding any inode reference 1222169ebd90SJan Kara * so once i_lock is dropped, inode can go away. 1223169ebd90SJan Kara */ 1224169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode) 1225169ebd90SJan Kara __releases(inode->i_lock) 1226169ebd90SJan Kara { 1227169ebd90SJan Kara DEFINE_WAIT(wait); 1228169ebd90SJan Kara wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 1229169ebd90SJan Kara int sleep; 1230169ebd90SJan Kara 1231169ebd90SJan Kara prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); 1232169ebd90SJan Kara sleep = inode->i_state & I_SYNC; 1233169ebd90SJan Kara spin_unlock(&inode->i_lock); 1234169ebd90SJan Kara if (sleep) 1235169ebd90SJan Kara schedule(); 1236169ebd90SJan Kara finish_wait(wqh, &wait); 1237169ebd90SJan Kara } 1238169ebd90SJan Kara 1239169ebd90SJan Kara /* 1240ccb26b5aSJan Kara * Find proper writeback list for the inode depending on its current state and 1241ccb26b5aSJan Kara * possibly also change of its state while we were doing writeback. Here we 1242ccb26b5aSJan Kara * handle things such as livelock prevention or fairness of writeback among 1243ccb26b5aSJan Kara * inodes. This function can be called only by flusher thread - noone else 1244ccb26b5aSJan Kara * processes all inodes in writeback lists and requeueing inodes behind flusher 1245ccb26b5aSJan Kara * thread's back can have unexpected consequences. 1246ccb26b5aSJan Kara */ 1247ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb, 1248ccb26b5aSJan Kara struct writeback_control *wbc) 1249ccb26b5aSJan Kara { 1250ccb26b5aSJan Kara if (inode->i_state & I_FREEING) 1251ccb26b5aSJan Kara return; 1252ccb26b5aSJan Kara 1253ccb26b5aSJan Kara /* 1254ccb26b5aSJan Kara * Sync livelock prevention. Each inode is tagged and synced in one 1255ccb26b5aSJan Kara * shot. If still dirty, it will be redirty_tail()'ed below. Update 1256ccb26b5aSJan Kara * the dirty time to prevent enqueue and sync it again. 1257ccb26b5aSJan Kara */ 1258ccb26b5aSJan Kara if ((inode->i_state & I_DIRTY) && 1259ccb26b5aSJan Kara (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)) 1260ccb26b5aSJan Kara inode->dirtied_when = jiffies; 1261ccb26b5aSJan Kara 12624f8ad655SJan Kara if (wbc->pages_skipped) { 12634f8ad655SJan Kara /* 12644f8ad655SJan Kara * writeback is not making progress due to locked 12654f8ad655SJan Kara * buffers. Skip this inode for now. 12664f8ad655SJan Kara */ 12674f8ad655SJan Kara redirty_tail(inode, wb); 12684f8ad655SJan Kara return; 12694f8ad655SJan Kara } 12704f8ad655SJan Kara 1271ccb26b5aSJan Kara if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) { 1272ccb26b5aSJan Kara /* 1273ccb26b5aSJan Kara * We didn't write back all the pages. nfs_writepages() 1274ccb26b5aSJan Kara * sometimes bales out without doing anything. 1275ccb26b5aSJan Kara */ 1276ccb26b5aSJan Kara if (wbc->nr_to_write <= 0) { 1277ccb26b5aSJan Kara /* Slice used up. Queue for next turn. */ 1278ccb26b5aSJan Kara requeue_io(inode, wb); 1279ccb26b5aSJan Kara } else { 1280ccb26b5aSJan Kara /* 1281ccb26b5aSJan Kara * Writeback blocked by something other than 1282ccb26b5aSJan Kara * congestion. Delay the inode for some time to 1283ccb26b5aSJan Kara * avoid spinning on the CPU (100% iowait) 1284ccb26b5aSJan Kara * retrying writeback of the dirty page/inode 1285ccb26b5aSJan Kara * that cannot be performed immediately. 1286ccb26b5aSJan Kara */ 1287ccb26b5aSJan Kara redirty_tail(inode, wb); 1288ccb26b5aSJan Kara } 1289ccb26b5aSJan Kara } else if (inode->i_state & I_DIRTY) { 1290ccb26b5aSJan Kara /* 1291ccb26b5aSJan Kara * Filesystems can dirty the inode during writeback operations, 1292ccb26b5aSJan Kara * such as delayed allocation during submission or metadata 1293ccb26b5aSJan Kara * updates after data IO completion. 1294ccb26b5aSJan Kara */ 1295ccb26b5aSJan Kara redirty_tail(inode, wb); 12960ae45f63STheodore Ts'o } else if (inode->i_state & I_DIRTY_TIME) { 1297a2f48706STheodore Ts'o inode->dirtied_when = jiffies; 1298c7f54084SDave Chinner inode_io_list_move_locked(inode, wb, &wb->b_dirty_time); 1299ccb26b5aSJan Kara } else { 1300ccb26b5aSJan Kara /* The inode is clean. Remove from writeback lists. */ 1301c7f54084SDave Chinner inode_io_list_del_locked(inode, wb); 1302ccb26b5aSJan Kara } 1303ccb26b5aSJan Kara } 1304ccb26b5aSJan Kara 1305ccb26b5aSJan Kara /* 13064f8ad655SJan Kara * Write out an inode and its dirty pages. Do not update the writeback list 13074f8ad655SJan Kara * linkage. That is left to the caller. The caller is also responsible for 13084f8ad655SJan Kara * setting I_SYNC flag and calling inode_sync_complete() to clear it. 13091da177e4SLinus Torvalds */ 13101da177e4SLinus Torvalds static int 1311cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) 13121da177e4SLinus Torvalds { 13131da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 1314251d6a47SWu Fengguang long nr_to_write = wbc->nr_to_write; 131501c03194SChristoph Hellwig unsigned dirty; 13161da177e4SLinus Torvalds int ret; 13171da177e4SLinus Torvalds 13184f8ad655SJan Kara WARN_ON(!(inode->i_state & I_SYNC)); 13191da177e4SLinus Torvalds 13209fb0a7daSTejun Heo trace_writeback_single_inode_start(inode, wbc, nr_to_write); 13219fb0a7daSTejun Heo 13221da177e4SLinus Torvalds ret = do_writepages(mapping, wbc); 13231da177e4SLinus Torvalds 132426821ed4SChristoph Hellwig /* 132526821ed4SChristoph Hellwig * Make sure to wait on the data before writing out the metadata. 132626821ed4SChristoph Hellwig * This is important for filesystems that modify metadata on data 13277747bd4bSDave Chinner * I/O completion. We don't do it for sync(2) writeback because it has a 13287747bd4bSDave Chinner * separate, external IO completion path and ->sync_fs for guaranteeing 13297747bd4bSDave Chinner * inode metadata is written back correctly. 133026821ed4SChristoph Hellwig */ 13317747bd4bSDave Chinner if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) { 133226821ed4SChristoph Hellwig int err = filemap_fdatawait(mapping); 13331da177e4SLinus Torvalds if (ret == 0) 13341da177e4SLinus Torvalds ret = err; 13351da177e4SLinus Torvalds } 13361da177e4SLinus Torvalds 13375547e8aaSDmitry Monakhov /* 13385547e8aaSDmitry Monakhov * Some filesystems may redirty the inode during the writeback 13395547e8aaSDmitry Monakhov * due to delalloc, clear dirty metadata flags right before 13405547e8aaSDmitry Monakhov * write_inode() 13415547e8aaSDmitry Monakhov */ 1342250df6edSDave Chinner spin_lock(&inode->i_lock); 13439c6ac78eSTejun Heo 13445547e8aaSDmitry Monakhov dirty = inode->i_state & I_DIRTY; 1345a2f48706STheodore Ts'o if (inode->i_state & I_DIRTY_TIME) { 13460e11f644SChristoph Hellwig if ((dirty & I_DIRTY_INODE) || 1347dc5ff2b1SJan Kara wbc->sync_mode == WB_SYNC_ALL || 1348a2f48706STheodore Ts'o unlikely(inode->i_state & I_DIRTY_TIME_EXPIRED) || 1349a2f48706STheodore Ts'o unlikely(time_after(jiffies, 1350a2f48706STheodore Ts'o (inode->dirtied_time_when + 1351a2f48706STheodore Ts'o dirtytime_expire_interval * HZ)))) { 13520ae45f63STheodore Ts'o dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED; 13530ae45f63STheodore Ts'o trace_writeback_lazytime(inode); 13540ae45f63STheodore Ts'o } 1355a2f48706STheodore Ts'o } else 1356a2f48706STheodore Ts'o inode->i_state &= ~I_DIRTY_TIME_EXPIRED; 13570ae45f63STheodore Ts'o inode->i_state &= ~dirty; 13589c6ac78eSTejun Heo 13599c6ac78eSTejun Heo /* 13609c6ac78eSTejun Heo * Paired with smp_mb() in __mark_inode_dirty(). This allows 13619c6ac78eSTejun Heo * __mark_inode_dirty() to test i_state without grabbing i_lock - 13629c6ac78eSTejun Heo * either they see the I_DIRTY bits cleared or we see the dirtied 13639c6ac78eSTejun Heo * inode. 13649c6ac78eSTejun Heo * 13659c6ac78eSTejun Heo * I_DIRTY_PAGES is always cleared together above even if @mapping 13669c6ac78eSTejun Heo * still has dirty pages. The flag is reinstated after smp_mb() if 13679c6ac78eSTejun Heo * necessary. This guarantees that either __mark_inode_dirty() 13689c6ac78eSTejun Heo * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY. 13699c6ac78eSTejun Heo */ 13709c6ac78eSTejun Heo smp_mb(); 13719c6ac78eSTejun Heo 13729c6ac78eSTejun Heo if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 13739c6ac78eSTejun Heo inode->i_state |= I_DIRTY_PAGES; 13749c6ac78eSTejun Heo 1375250df6edSDave Chinner spin_unlock(&inode->i_lock); 13769c6ac78eSTejun Heo 13770ae45f63STheodore Ts'o if (dirty & I_DIRTY_TIME) 13780ae45f63STheodore Ts'o mark_inode_dirty_sync(inode); 137926821ed4SChristoph Hellwig /* Don't write the inode if only I_DIRTY_PAGES was set */ 13800ae45f63STheodore Ts'o if (dirty & ~I_DIRTY_PAGES) { 1381a9185b41SChristoph Hellwig int err = write_inode(inode, wbc); 13821da177e4SLinus Torvalds if (ret == 0) 13831da177e4SLinus Torvalds ret = err; 13841da177e4SLinus Torvalds } 13854f8ad655SJan Kara trace_writeback_single_inode(inode, wbc, nr_to_write); 13864f8ad655SJan Kara return ret; 13874f8ad655SJan Kara } 13884f8ad655SJan Kara 13894f8ad655SJan Kara /* 13904f8ad655SJan Kara * Write out an inode's dirty pages. Either the caller has an active reference 13914f8ad655SJan Kara * on the inode or the inode has I_WILL_FREE set. 13924f8ad655SJan Kara * 13934f8ad655SJan Kara * This function is designed to be called for writing back one inode which 13944f8ad655SJan Kara * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode() 13954f8ad655SJan Kara * and does more profound writeback list handling in writeback_sb_inodes(). 13964f8ad655SJan Kara */ 1397aaf25593STejun Heo static int writeback_single_inode(struct inode *inode, 13984f8ad655SJan Kara struct writeback_control *wbc) 13994f8ad655SJan Kara { 1400aaf25593STejun Heo struct bdi_writeback *wb; 14014f8ad655SJan Kara int ret = 0; 14024f8ad655SJan Kara 14034f8ad655SJan Kara spin_lock(&inode->i_lock); 14044f8ad655SJan Kara if (!atomic_read(&inode->i_count)) 14054f8ad655SJan Kara WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); 14064f8ad655SJan Kara else 14074f8ad655SJan Kara WARN_ON(inode->i_state & I_WILL_FREE); 14084f8ad655SJan Kara 14094f8ad655SJan Kara if (inode->i_state & I_SYNC) { 14104f8ad655SJan Kara if (wbc->sync_mode != WB_SYNC_ALL) 14114f8ad655SJan Kara goto out; 14124f8ad655SJan Kara /* 1413169ebd90SJan Kara * It's a data-integrity sync. We must wait. Since callers hold 1414169ebd90SJan Kara * inode reference or inode has I_WILL_FREE set, it cannot go 1415169ebd90SJan Kara * away under us. 14164f8ad655SJan Kara */ 1417169ebd90SJan Kara __inode_wait_for_writeback(inode); 14184f8ad655SJan Kara } 14194f8ad655SJan Kara WARN_ON(inode->i_state & I_SYNC); 14204f8ad655SJan Kara /* 1421f9b0e058SJan Kara * Skip inode if it is clean and we have no outstanding writeback in 1422f9b0e058SJan Kara * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this 1423f9b0e058SJan Kara * function since flusher thread may be doing for example sync in 1424f9b0e058SJan Kara * parallel and if we move the inode, it could get skipped. So here we 1425f9b0e058SJan Kara * make sure inode is on some writeback list and leave it there unless 1426f9b0e058SJan Kara * we have completely cleaned the inode. 14274f8ad655SJan Kara */ 14280ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL) && 1429f9b0e058SJan Kara (wbc->sync_mode != WB_SYNC_ALL || 1430f9b0e058SJan Kara !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))) 14314f8ad655SJan Kara goto out; 14324f8ad655SJan Kara inode->i_state |= I_SYNC; 1433b16b1debSTejun Heo wbc_attach_and_unlock_inode(wbc, inode); 14344f8ad655SJan Kara 1435cd8ed2a4SYan Hong ret = __writeback_single_inode(inode, wbc); 14361da177e4SLinus Torvalds 1437b16b1debSTejun Heo wbc_detach_inode(wbc); 1438aaf25593STejun Heo 1439aaf25593STejun Heo wb = inode_to_wb_and_lock_list(inode); 1440250df6edSDave Chinner spin_lock(&inode->i_lock); 14414f8ad655SJan Kara /* 14424f8ad655SJan Kara * If inode is clean, remove it from writeback lists. Otherwise don't 14434f8ad655SJan Kara * touch it. See comment above for explanation. 14444f8ad655SJan Kara */ 14450ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 1446c7f54084SDave Chinner inode_io_list_del_locked(inode, wb); 14474f8ad655SJan Kara spin_unlock(&wb->list_lock); 14481c0eeaf5SJoern Engel inode_sync_complete(inode); 14494f8ad655SJan Kara out: 14504f8ad655SJan Kara spin_unlock(&inode->i_lock); 14511da177e4SLinus Torvalds return ret; 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds 1454a88a341aSTejun Heo static long writeback_chunk_size(struct bdi_writeback *wb, 14551a12d8bdSWu Fengguang struct wb_writeback_work *work) 1456d46db3d5SWu Fengguang { 1457d46db3d5SWu Fengguang long pages; 1458d46db3d5SWu Fengguang 1459d46db3d5SWu Fengguang /* 1460d46db3d5SWu Fengguang * WB_SYNC_ALL mode does livelock avoidance by syncing dirty 1461d46db3d5SWu Fengguang * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX 1462d46db3d5SWu Fengguang * here avoids calling into writeback_inodes_wb() more than once. 1463d46db3d5SWu Fengguang * 1464d46db3d5SWu Fengguang * The intended call sequence for WB_SYNC_ALL writeback is: 1465d46db3d5SWu Fengguang * 1466d46db3d5SWu Fengguang * wb_writeback() 1467d46db3d5SWu Fengguang * writeback_sb_inodes() <== called only once 1468d46db3d5SWu Fengguang * write_cache_pages() <== called once for each inode 1469d46db3d5SWu Fengguang * (quickly) tag currently dirty pages 1470d46db3d5SWu Fengguang * (maybe slowly) sync all tagged pages 1471d46db3d5SWu Fengguang */ 1472d46db3d5SWu Fengguang if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages) 1473d46db3d5SWu Fengguang pages = LONG_MAX; 14741a12d8bdSWu Fengguang else { 1475a88a341aSTejun Heo pages = min(wb->avg_write_bandwidth / 2, 1476dcc25ae7STejun Heo global_wb_domain.dirty_limit / DIRTY_SCOPE); 14771a12d8bdSWu Fengguang pages = min(pages, work->nr_pages); 14781a12d8bdSWu Fengguang pages = round_down(pages + MIN_WRITEBACK_PAGES, 14791a12d8bdSWu Fengguang MIN_WRITEBACK_PAGES); 14801a12d8bdSWu Fengguang } 1481d46db3d5SWu Fengguang 1482d46db3d5SWu Fengguang return pages; 1483d46db3d5SWu Fengguang } 1484d46db3d5SWu Fengguang 148503ba3782SJens Axboe /* 1486f11c9c5cSEdward Shishkin * Write a portion of b_io inodes which belong to @sb. 1487edadfb10SChristoph Hellwig * 1488d46db3d5SWu Fengguang * Return the number of pages and/or inodes written. 14890ba13fd1SLinus Torvalds * 14900ba13fd1SLinus Torvalds * NOTE! This is called with wb->list_lock held, and will 14910ba13fd1SLinus Torvalds * unlock and relock that for each inode it ends up doing 14920ba13fd1SLinus Torvalds * IO for. 1493f11c9c5cSEdward Shishkin */ 1494d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb, 1495d46db3d5SWu Fengguang struct bdi_writeback *wb, 1496d46db3d5SWu Fengguang struct wb_writeback_work *work) 149703ba3782SJens Axboe { 1498d46db3d5SWu Fengguang struct writeback_control wbc = { 1499d46db3d5SWu Fengguang .sync_mode = work->sync_mode, 1500d46db3d5SWu Fengguang .tagged_writepages = work->tagged_writepages, 1501d46db3d5SWu Fengguang .for_kupdate = work->for_kupdate, 1502d46db3d5SWu Fengguang .for_background = work->for_background, 15037747bd4bSDave Chinner .for_sync = work->for_sync, 1504d46db3d5SWu Fengguang .range_cyclic = work->range_cyclic, 1505d46db3d5SWu Fengguang .range_start = 0, 1506d46db3d5SWu Fengguang .range_end = LLONG_MAX, 1507d46db3d5SWu Fengguang }; 1508d46db3d5SWu Fengguang unsigned long start_time = jiffies; 1509d46db3d5SWu Fengguang long write_chunk; 1510d46db3d5SWu Fengguang long wrote = 0; /* count both pages and inodes */ 1511d46db3d5SWu Fengguang 151203ba3782SJens Axboe while (!list_empty(&wb->b_io)) { 15137ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 1514aaf25593STejun Heo struct bdi_writeback *tmp_wb; 1515edadfb10SChristoph Hellwig 1516edadfb10SChristoph Hellwig if (inode->i_sb != sb) { 1517d46db3d5SWu Fengguang if (work->sb) { 1518edadfb10SChristoph Hellwig /* 1519edadfb10SChristoph Hellwig * We only want to write back data for this 1520edadfb10SChristoph Hellwig * superblock, move all inodes not belonging 1521edadfb10SChristoph Hellwig * to it back onto the dirty list. 1522edadfb10SChristoph Hellwig */ 1523f758eeabSChristoph Hellwig redirty_tail(inode, wb); 152466f3b8e2SJens Axboe continue; 152566f3b8e2SJens Axboe } 1526edadfb10SChristoph Hellwig 1527edadfb10SChristoph Hellwig /* 1528edadfb10SChristoph Hellwig * The inode belongs to a different superblock. 1529edadfb10SChristoph Hellwig * Bounce back to the caller to unpin this and 1530edadfb10SChristoph Hellwig * pin the next superblock. 1531edadfb10SChristoph Hellwig */ 1532d46db3d5SWu Fengguang break; 1533edadfb10SChristoph Hellwig } 1534edadfb10SChristoph Hellwig 15359843b76aSChristoph Hellwig /* 1536331cbdeeSWanpeng Li * Don't bother with new inodes or inodes being freed, first 1537331cbdeeSWanpeng Li * kind does not need periodic writeout yet, and for the latter 15389843b76aSChristoph Hellwig * kind writeout is handled by the freer. 15399843b76aSChristoph Hellwig */ 1540250df6edSDave Chinner spin_lock(&inode->i_lock); 15419843b76aSChristoph Hellwig if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { 1542250df6edSDave Chinner spin_unlock(&inode->i_lock); 1543fcc5c222SWu Fengguang redirty_tail(inode, wb); 15447ef0d737SNick Piggin continue; 15457ef0d737SNick Piggin } 1546cc1676d9SJan Kara if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) { 1547cc1676d9SJan Kara /* 1548cc1676d9SJan Kara * If this inode is locked for writeback and we are not 1549cc1676d9SJan Kara * doing writeback-for-data-integrity, move it to 1550cc1676d9SJan Kara * b_more_io so that writeback can proceed with the 1551cc1676d9SJan Kara * other inodes on s_io. 1552cc1676d9SJan Kara * 1553cc1676d9SJan Kara * We'll have another go at writing back this inode 1554cc1676d9SJan Kara * when we completed a full scan of b_io. 1555cc1676d9SJan Kara */ 1556cc1676d9SJan Kara spin_unlock(&inode->i_lock); 1557cc1676d9SJan Kara requeue_io(inode, wb); 1558cc1676d9SJan Kara trace_writeback_sb_inodes_requeue(inode); 1559cc1676d9SJan Kara continue; 1560cc1676d9SJan Kara } 1561f0d07b7fSJan Kara spin_unlock(&wb->list_lock); 1562f0d07b7fSJan Kara 15634f8ad655SJan Kara /* 15644f8ad655SJan Kara * We already requeued the inode if it had I_SYNC set and we 15654f8ad655SJan Kara * are doing WB_SYNC_NONE writeback. So this catches only the 15664f8ad655SJan Kara * WB_SYNC_ALL case. 15674f8ad655SJan Kara */ 1568169ebd90SJan Kara if (inode->i_state & I_SYNC) { 1569169ebd90SJan Kara /* Wait for I_SYNC. This function drops i_lock... */ 1570169ebd90SJan Kara inode_sleep_on_writeback(inode); 1571169ebd90SJan Kara /* Inode may be gone, start again */ 1572ead188f9SJan Kara spin_lock(&wb->list_lock); 1573169ebd90SJan Kara continue; 1574169ebd90SJan Kara } 15754f8ad655SJan Kara inode->i_state |= I_SYNC; 1576b16b1debSTejun Heo wbc_attach_and_unlock_inode(&wbc, inode); 1577169ebd90SJan Kara 1578a88a341aSTejun Heo write_chunk = writeback_chunk_size(wb, work); 1579d46db3d5SWu Fengguang wbc.nr_to_write = write_chunk; 1580d46db3d5SWu Fengguang wbc.pages_skipped = 0; 1581250df6edSDave Chinner 1582169ebd90SJan Kara /* 1583169ebd90SJan Kara * We use I_SYNC to pin the inode in memory. While it is set 1584169ebd90SJan Kara * evict_inode() will wait so the inode cannot be freed. 1585169ebd90SJan Kara */ 1586cd8ed2a4SYan Hong __writeback_single_inode(inode, &wbc); 1587d46db3d5SWu Fengguang 1588b16b1debSTejun Heo wbc_detach_inode(&wbc); 1589d46db3d5SWu Fengguang work->nr_pages -= write_chunk - wbc.nr_to_write; 1590d46db3d5SWu Fengguang wrote += write_chunk - wbc.nr_to_write; 1591590dca3aSChris Mason 1592590dca3aSChris Mason if (need_resched()) { 1593590dca3aSChris Mason /* 1594590dca3aSChris Mason * We're trying to balance between building up a nice 1595590dca3aSChris Mason * long list of IOs to improve our merge rate, and 1596590dca3aSChris Mason * getting those IOs out quickly for anyone throttling 1597590dca3aSChris Mason * in balance_dirty_pages(). cond_resched() doesn't 1598590dca3aSChris Mason * unplug, so get our IOs out the door before we 1599590dca3aSChris Mason * give up the CPU. 1600590dca3aSChris Mason */ 1601590dca3aSChris Mason blk_flush_plug(current); 1602590dca3aSChris Mason cond_resched(); 1603590dca3aSChris Mason } 1604590dca3aSChris Mason 1605aaf25593STejun Heo /* 1606aaf25593STejun Heo * Requeue @inode if still dirty. Be careful as @inode may 1607aaf25593STejun Heo * have been switched to another wb in the meantime. 1608aaf25593STejun Heo */ 1609aaf25593STejun Heo tmp_wb = inode_to_wb_and_lock_list(inode); 16104f8ad655SJan Kara spin_lock(&inode->i_lock); 16110ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 1612d46db3d5SWu Fengguang wrote++; 1613aaf25593STejun Heo requeue_inode(inode, tmp_wb, &wbc); 16144f8ad655SJan Kara inode_sync_complete(inode); 16150f1b1fd8SDave Chinner spin_unlock(&inode->i_lock); 1616590dca3aSChris Mason 1617aaf25593STejun Heo if (unlikely(tmp_wb != wb)) { 1618aaf25593STejun Heo spin_unlock(&tmp_wb->list_lock); 1619aaf25593STejun Heo spin_lock(&wb->list_lock); 1620aaf25593STejun Heo } 1621aaf25593STejun Heo 1622d46db3d5SWu Fengguang /* 1623d46db3d5SWu Fengguang * bail out to wb_writeback() often enough to check 1624d46db3d5SWu Fengguang * background threshold and other termination conditions. 1625d46db3d5SWu Fengguang */ 1626d46db3d5SWu Fengguang if (wrote) { 1627d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 1628d46db3d5SWu Fengguang break; 1629d46db3d5SWu Fengguang if (work->nr_pages <= 0) 1630d46db3d5SWu Fengguang break; 16311da177e4SLinus Torvalds } 16328bc3be27SFengguang Wu } 1633d46db3d5SWu Fengguang return wrote; 1634f11c9c5cSEdward Shishkin } 163538f21977SNick Piggin 1636d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb, 1637d46db3d5SWu Fengguang struct wb_writeback_work *work) 1638f11c9c5cSEdward Shishkin { 1639d46db3d5SWu Fengguang unsigned long start_time = jiffies; 1640d46db3d5SWu Fengguang long wrote = 0; 1641f11c9c5cSEdward Shishkin 1642f11c9c5cSEdward Shishkin while (!list_empty(&wb->b_io)) { 16437ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 1644f11c9c5cSEdward Shishkin struct super_block *sb = inode->i_sb; 1645f11c9c5cSEdward Shishkin 1646eb6ef3dfSKonstantin Khlebnikov if (!trylock_super(sb)) { 16470e995816SWu Fengguang /* 1648eb6ef3dfSKonstantin Khlebnikov * trylock_super() may fail consistently due to 16490e995816SWu Fengguang * s_umount being grabbed by someone else. Don't use 16500e995816SWu Fengguang * requeue_io() to avoid busy retrying the inode/sb. 16510e995816SWu Fengguang */ 16520e995816SWu Fengguang redirty_tail(inode, wb); 1653d19de7edSChristoph Hellwig continue; 1654334132aeSChristoph Hellwig } 1655d46db3d5SWu Fengguang wrote += writeback_sb_inodes(sb, wb, work); 1656eb6ef3dfSKonstantin Khlebnikov up_read(&sb->s_umount); 1657f11c9c5cSEdward Shishkin 1658d46db3d5SWu Fengguang /* refer to the same tests at the end of writeback_sb_inodes */ 1659d46db3d5SWu Fengguang if (wrote) { 1660d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 1661d46db3d5SWu Fengguang break; 1662d46db3d5SWu Fengguang if (work->nr_pages <= 0) 1663f11c9c5cSEdward Shishkin break; 1664f11c9c5cSEdward Shishkin } 1665d46db3d5SWu Fengguang } 166666f3b8e2SJens Axboe /* Leave any unwritten inodes on b_io */ 1667d46db3d5SWu Fengguang return wrote; 166866f3b8e2SJens Axboe } 166966f3b8e2SJens Axboe 16707d9f073bSWanpeng Li static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages, 16710e175a18SCurt Wohlgemuth enum wb_reason reason) 1672edadfb10SChristoph Hellwig { 1673d46db3d5SWu Fengguang struct wb_writeback_work work = { 1674d46db3d5SWu Fengguang .nr_pages = nr_pages, 1675d46db3d5SWu Fengguang .sync_mode = WB_SYNC_NONE, 1676d46db3d5SWu Fengguang .range_cyclic = 1, 16770e175a18SCurt Wohlgemuth .reason = reason, 1678d46db3d5SWu Fengguang }; 1679505a666eSLinus Torvalds struct blk_plug plug; 1680edadfb10SChristoph Hellwig 1681505a666eSLinus Torvalds blk_start_plug(&plug); 1682f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 1683424b351fSWu Fengguang if (list_empty(&wb->b_io)) 1684ad4e38ddSCurt Wohlgemuth queue_io(wb, &work); 1685d46db3d5SWu Fengguang __writeback_inodes_wb(wb, &work); 1686f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 1687505a666eSLinus Torvalds blk_finish_plug(&plug); 1688edadfb10SChristoph Hellwig 1689d46db3d5SWu Fengguang return nr_pages - work.nr_pages; 169066f3b8e2SJens Axboe } 169166f3b8e2SJens Axboe 169203ba3782SJens Axboe /* 169303ba3782SJens Axboe * Explicit flushing or periodic writeback of "old" data. 169403ba3782SJens Axboe * 169503ba3782SJens Axboe * Define "old": the first time one of an inode's pages is dirtied, we mark the 169603ba3782SJens Axboe * dirtying-time in the inode's address_space. So this periodic writeback code 169703ba3782SJens Axboe * just walks the superblock inode list, writing back any inodes which are 169803ba3782SJens Axboe * older than a specific point in time. 169903ba3782SJens Axboe * 170003ba3782SJens Axboe * Try to run once per dirty_writeback_interval. But if a writeback event 170103ba3782SJens Axboe * takes longer than a dirty_writeback_interval interval, then leave a 170203ba3782SJens Axboe * one-second gap. 170303ba3782SJens Axboe * 170403ba3782SJens Axboe * older_than_this takes precedence over nr_to_write. So we'll only write back 170503ba3782SJens Axboe * all dirty pages if they are all attached to "old" mappings. 170603ba3782SJens Axboe */ 1707c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb, 170883ba7b07SChristoph Hellwig struct wb_writeback_work *work) 170903ba3782SJens Axboe { 1710e98be2d5SWu Fengguang unsigned long wb_start = jiffies; 1711d46db3d5SWu Fengguang long nr_pages = work->nr_pages; 17120dc83bd3SJan Kara unsigned long oldest_jif; 1713a5989bdcSJan Kara struct inode *inode; 1714d46db3d5SWu Fengguang long progress; 1715505a666eSLinus Torvalds struct blk_plug plug; 171603ba3782SJens Axboe 17170dc83bd3SJan Kara oldest_jif = jiffies; 17180dc83bd3SJan Kara work->older_than_this = &oldest_jif; 171903ba3782SJens Axboe 1720505a666eSLinus Torvalds blk_start_plug(&plug); 1721e8dfc305SWu Fengguang spin_lock(&wb->list_lock); 172203ba3782SJens Axboe for (;;) { 172303ba3782SJens Axboe /* 1724d3ddec76SWu Fengguang * Stop writeback when nr_pages has been consumed 172503ba3782SJens Axboe */ 172683ba7b07SChristoph Hellwig if (work->nr_pages <= 0) 172703ba3782SJens Axboe break; 172803ba3782SJens Axboe 172903ba3782SJens Axboe /* 1730aa373cf5SJan Kara * Background writeout and kupdate-style writeback may 1731aa373cf5SJan Kara * run forever. Stop them if there is other work to do 1732aa373cf5SJan Kara * so that e.g. sync can proceed. They'll be restarted 1733aa373cf5SJan Kara * after the other works are all done. 1734aa373cf5SJan Kara */ 1735aa373cf5SJan Kara if ((work->for_background || work->for_kupdate) && 1736f0054bb1STejun Heo !list_empty(&wb->work_list)) 1737aa373cf5SJan Kara break; 1738aa373cf5SJan Kara 1739aa373cf5SJan Kara /* 1740d3ddec76SWu Fengguang * For background writeout, stop when we are below the 1741d3ddec76SWu Fengguang * background dirty threshold 174203ba3782SJens Axboe */ 1743aa661bbeSTejun Heo if (work->for_background && !wb_over_bg_thresh(wb)) 174403ba3782SJens Axboe break; 174503ba3782SJens Axboe 17461bc36b64SJan Kara /* 17471bc36b64SJan Kara * Kupdate and background works are special and we want to 17481bc36b64SJan Kara * include all inodes that need writing. Livelock avoidance is 17491bc36b64SJan Kara * handled by these works yielding to any other work so we are 17501bc36b64SJan Kara * safe. 17511bc36b64SJan Kara */ 1752ba9aa839SWu Fengguang if (work->for_kupdate) { 17530dc83bd3SJan Kara oldest_jif = jiffies - 1754ba9aa839SWu Fengguang msecs_to_jiffies(dirty_expire_interval * 10); 17551bc36b64SJan Kara } else if (work->for_background) 17560dc83bd3SJan Kara oldest_jif = jiffies; 1757028c2dd1SDave Chinner 17585634cc2aSTejun Heo trace_writeback_start(wb, work); 1759e8dfc305SWu Fengguang if (list_empty(&wb->b_io)) 1760ad4e38ddSCurt Wohlgemuth queue_io(wb, work); 176183ba7b07SChristoph Hellwig if (work->sb) 1762d46db3d5SWu Fengguang progress = writeback_sb_inodes(work->sb, wb, work); 1763edadfb10SChristoph Hellwig else 1764d46db3d5SWu Fengguang progress = __writeback_inodes_wb(wb, work); 17655634cc2aSTejun Heo trace_writeback_written(wb, work); 1766028c2dd1SDave Chinner 1767e98be2d5SWu Fengguang wb_update_bandwidth(wb, wb_start); 176803ba3782SJens Axboe 176903ba3782SJens Axboe /* 177071fd05a8SJens Axboe * Did we write something? Try for more 1771e6fb6da2SWu Fengguang * 1772e6fb6da2SWu Fengguang * Dirty inodes are moved to b_io for writeback in batches. 1773e6fb6da2SWu Fengguang * The completion of the current batch does not necessarily 1774e6fb6da2SWu Fengguang * mean the overall work is done. So we keep looping as long 1775e6fb6da2SWu Fengguang * as made some progress on cleaning pages or inodes. 177671fd05a8SJens Axboe */ 1777d46db3d5SWu Fengguang if (progress) 177803ba3782SJens Axboe continue; 1779a5989bdcSJan Kara /* 1780e6fb6da2SWu Fengguang * No more inodes for IO, bail 1781a5989bdcSJan Kara */ 1782b7a2441fSWu Fengguang if (list_empty(&wb->b_more_io)) 178303ba3782SJens Axboe break; 178403ba3782SJens Axboe /* 17858010c3b6SJens Axboe * Nothing written. Wait for some inode to 17868010c3b6SJens Axboe * become available for writeback. Otherwise 17878010c3b6SJens Axboe * we'll just busyloop. 178803ba3782SJens Axboe */ 17895634cc2aSTejun Heo trace_writeback_wait(wb, work); 179003ba3782SJens Axboe inode = wb_inode(wb->b_more_io.prev); 1791250df6edSDave Chinner spin_lock(&inode->i_lock); 1792f0d07b7fSJan Kara spin_unlock(&wb->list_lock); 1793169ebd90SJan Kara /* This function drops i_lock... */ 1794169ebd90SJan Kara inode_sleep_on_writeback(inode); 1795f0d07b7fSJan Kara spin_lock(&wb->list_lock); 179603ba3782SJens Axboe } 1797e8dfc305SWu Fengguang spin_unlock(&wb->list_lock); 1798505a666eSLinus Torvalds blk_finish_plug(&plug); 179903ba3782SJens Axboe 1800d46db3d5SWu Fengguang return nr_pages - work->nr_pages; 180103ba3782SJens Axboe } 180203ba3782SJens Axboe 180303ba3782SJens Axboe /* 180483ba7b07SChristoph Hellwig * Return the next wb_writeback_work struct that hasn't been processed yet. 180503ba3782SJens Axboe */ 1806f0054bb1STejun Heo static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb) 180703ba3782SJens Axboe { 180883ba7b07SChristoph Hellwig struct wb_writeback_work *work = NULL; 180903ba3782SJens Axboe 1810f0054bb1STejun Heo spin_lock_bh(&wb->work_lock); 1811f0054bb1STejun Heo if (!list_empty(&wb->work_list)) { 1812f0054bb1STejun Heo work = list_entry(wb->work_list.next, 181383ba7b07SChristoph Hellwig struct wb_writeback_work, list); 181483ba7b07SChristoph Hellwig list_del_init(&work->list); 181503ba3782SJens Axboe } 1816f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 181783ba7b07SChristoph Hellwig return work; 181803ba3782SJens Axboe } 181903ba3782SJens Axboe 18206585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb) 18216585027aSJan Kara { 1822aa661bbeSTejun Heo if (wb_over_bg_thresh(wb)) { 18236585027aSJan Kara 18246585027aSJan Kara struct wb_writeback_work work = { 18256585027aSJan Kara .nr_pages = LONG_MAX, 18266585027aSJan Kara .sync_mode = WB_SYNC_NONE, 18276585027aSJan Kara .for_background = 1, 18286585027aSJan Kara .range_cyclic = 1, 18290e175a18SCurt Wohlgemuth .reason = WB_REASON_BACKGROUND, 18306585027aSJan Kara }; 18316585027aSJan Kara 18326585027aSJan Kara return wb_writeback(wb, &work); 18336585027aSJan Kara } 18346585027aSJan Kara 18356585027aSJan Kara return 0; 18366585027aSJan Kara } 18376585027aSJan Kara 183803ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb) 183903ba3782SJens Axboe { 184003ba3782SJens Axboe unsigned long expired; 184103ba3782SJens Axboe long nr_pages; 184203ba3782SJens Axboe 184369b62d01SJens Axboe /* 184469b62d01SJens Axboe * When set to zero, disable periodic writeback 184569b62d01SJens Axboe */ 184669b62d01SJens Axboe if (!dirty_writeback_interval) 184769b62d01SJens Axboe return 0; 184869b62d01SJens Axboe 184903ba3782SJens Axboe expired = wb->last_old_flush + 185003ba3782SJens Axboe msecs_to_jiffies(dirty_writeback_interval * 10); 185103ba3782SJens Axboe if (time_before(jiffies, expired)) 185203ba3782SJens Axboe return 0; 185303ba3782SJens Axboe 185403ba3782SJens Axboe wb->last_old_flush = jiffies; 1855cdf01dd5SLinus Torvalds nr_pages = get_nr_dirty_pages(); 185603ba3782SJens Axboe 1857c4a77a6cSJens Axboe if (nr_pages) { 185883ba7b07SChristoph Hellwig struct wb_writeback_work work = { 1859c4a77a6cSJens Axboe .nr_pages = nr_pages, 1860c4a77a6cSJens Axboe .sync_mode = WB_SYNC_NONE, 1861c4a77a6cSJens Axboe .for_kupdate = 1, 1862c4a77a6cSJens Axboe .range_cyclic = 1, 18630e175a18SCurt Wohlgemuth .reason = WB_REASON_PERIODIC, 1864c4a77a6cSJens Axboe }; 1865c4a77a6cSJens Axboe 186683ba7b07SChristoph Hellwig return wb_writeback(wb, &work); 1867c4a77a6cSJens Axboe } 186803ba3782SJens Axboe 186903ba3782SJens Axboe return 0; 187003ba3782SJens Axboe } 187103ba3782SJens Axboe 187285009b4fSJens Axboe static long wb_check_start_all(struct bdi_writeback *wb) 187385009b4fSJens Axboe { 187485009b4fSJens Axboe long nr_pages; 187585009b4fSJens Axboe 187685009b4fSJens Axboe if (!test_bit(WB_start_all, &wb->state)) 187785009b4fSJens Axboe return 0; 187885009b4fSJens Axboe 187985009b4fSJens Axboe nr_pages = get_nr_dirty_pages(); 188085009b4fSJens Axboe if (nr_pages) { 188185009b4fSJens Axboe struct wb_writeback_work work = { 188285009b4fSJens Axboe .nr_pages = wb_split_bdi_pages(wb, nr_pages), 188385009b4fSJens Axboe .sync_mode = WB_SYNC_NONE, 188485009b4fSJens Axboe .range_cyclic = 1, 188585009b4fSJens Axboe .reason = wb->start_all_reason, 188685009b4fSJens Axboe }; 188785009b4fSJens Axboe 188885009b4fSJens Axboe nr_pages = wb_writeback(wb, &work); 188985009b4fSJens Axboe } 189085009b4fSJens Axboe 189185009b4fSJens Axboe clear_bit(WB_start_all, &wb->state); 189285009b4fSJens Axboe return nr_pages; 189385009b4fSJens Axboe } 189485009b4fSJens Axboe 189585009b4fSJens Axboe 189603ba3782SJens Axboe /* 189703ba3782SJens Axboe * Retrieve work items and do the writeback they describe 189803ba3782SJens Axboe */ 189925d130baSWanpeng Li static long wb_do_writeback(struct bdi_writeback *wb) 190003ba3782SJens Axboe { 190183ba7b07SChristoph Hellwig struct wb_writeback_work *work; 1902c4a77a6cSJens Axboe long wrote = 0; 190303ba3782SJens Axboe 19044452226eSTejun Heo set_bit(WB_writeback_running, &wb->state); 1905f0054bb1STejun Heo while ((work = get_next_work_item(wb)) != NULL) { 19065634cc2aSTejun Heo trace_writeback_exec(wb, work); 190783ba7b07SChristoph Hellwig wrote += wb_writeback(wb, work); 19084a3a485bSTahsin Erdogan finish_writeback_work(wb, work); 190903ba3782SJens Axboe } 191003ba3782SJens Axboe 191103ba3782SJens Axboe /* 191285009b4fSJens Axboe * Check for a flush-everything request 191385009b4fSJens Axboe */ 191485009b4fSJens Axboe wrote += wb_check_start_all(wb); 191585009b4fSJens Axboe 191685009b4fSJens Axboe /* 191703ba3782SJens Axboe * Check for periodic writeback, kupdated() style 191803ba3782SJens Axboe */ 191903ba3782SJens Axboe wrote += wb_check_old_data_flush(wb); 19206585027aSJan Kara wrote += wb_check_background_flush(wb); 19214452226eSTejun Heo clear_bit(WB_writeback_running, &wb->state); 192203ba3782SJens Axboe 192303ba3782SJens Axboe return wrote; 192403ba3782SJens Axboe } 192503ba3782SJens Axboe 192603ba3782SJens Axboe /* 192703ba3782SJens Axboe * Handle writeback of dirty data for the device backed by this bdi. Also 1928839a8e86STejun Heo * reschedules periodically and does kupdated style flushing. 192903ba3782SJens Axboe */ 1930f0054bb1STejun Heo void wb_workfn(struct work_struct *work) 193103ba3782SJens Axboe { 1932839a8e86STejun Heo struct bdi_writeback *wb = container_of(to_delayed_work(work), 1933839a8e86STejun Heo struct bdi_writeback, dwork); 193403ba3782SJens Axboe long pages_written; 193503ba3782SJens Axboe 1936f0054bb1STejun Heo set_worker_desc("flush-%s", dev_name(wb->bdi->dev)); 1937766f9164SPeter Zijlstra current->flags |= PF_SWAPWRITE; 193803ba3782SJens Axboe 1939839a8e86STejun Heo if (likely(!current_is_workqueue_rescuer() || 19404452226eSTejun Heo !test_bit(WB_registered, &wb->state))) { 194103ba3782SJens Axboe /* 1942f0054bb1STejun Heo * The normal path. Keep writing back @wb until its 1943839a8e86STejun Heo * work_list is empty. Note that this path is also taken 1944f0054bb1STejun Heo * if @wb is shutting down even when we're running off the 1945839a8e86STejun Heo * rescuer as work_list needs to be drained. 194603ba3782SJens Axboe */ 1947839a8e86STejun Heo do { 194825d130baSWanpeng Li pages_written = wb_do_writeback(wb); 1949455b2864SDave Chinner trace_writeback_pages_written(pages_written); 1950f0054bb1STejun Heo } while (!list_empty(&wb->work_list)); 1951839a8e86STejun Heo } else { 1952253c34e9SArtem Bityutskiy /* 1953839a8e86STejun Heo * bdi_wq can't get enough workers and we're running off 1954839a8e86STejun Heo * the emergency worker. Don't hog it. Hopefully, 1024 is 1955839a8e86STejun Heo * enough for efficient IO. 1956253c34e9SArtem Bityutskiy */ 1957f0054bb1STejun Heo pages_written = writeback_inodes_wb(wb, 1024, 1958839a8e86STejun Heo WB_REASON_FORKER_THREAD); 1959839a8e86STejun Heo trace_writeback_pages_written(pages_written); 196003ba3782SJens Axboe } 196103ba3782SJens Axboe 1962f0054bb1STejun Heo if (!list_empty(&wb->work_list)) 19636ca738d6SDerek Basehore mod_delayed_work(bdi_wq, &wb->dwork, 0); 19646ca738d6SDerek Basehore else if (wb_has_dirty_io(wb) && dirty_writeback_interval) 1965f0054bb1STejun Heo wb_wakeup_delayed(wb); 1966455b2864SDave Chinner 1967839a8e86STejun Heo current->flags &= ~PF_SWAPWRITE; 196803ba3782SJens Axboe } 196903ba3782SJens Axboe 197003ba3782SJens Axboe /* 1971595043e5SJens Axboe * Start writeback of `nr_pages' pages on this bdi. If `nr_pages' is zero, 1972595043e5SJens Axboe * write back the whole world. 1973595043e5SJens Axboe */ 1974595043e5SJens Axboe static void __wakeup_flusher_threads_bdi(struct backing_dev_info *bdi, 1975e8e8a0c6SJens Axboe enum wb_reason reason) 1976595043e5SJens Axboe { 1977595043e5SJens Axboe struct bdi_writeback *wb; 1978595043e5SJens Axboe 1979595043e5SJens Axboe if (!bdi_has_dirty_io(bdi)) 1980595043e5SJens Axboe return; 1981595043e5SJens Axboe 1982595043e5SJens Axboe list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node) 1983e8e8a0c6SJens Axboe wb_start_writeback(wb, reason); 1984595043e5SJens Axboe } 1985595043e5SJens Axboe 1986595043e5SJens Axboe void wakeup_flusher_threads_bdi(struct backing_dev_info *bdi, 1987595043e5SJens Axboe enum wb_reason reason) 1988595043e5SJens Axboe { 1989595043e5SJens Axboe rcu_read_lock(); 1990e8e8a0c6SJens Axboe __wakeup_flusher_threads_bdi(bdi, reason); 1991595043e5SJens Axboe rcu_read_unlock(); 1992595043e5SJens Axboe } 1993595043e5SJens Axboe 1994595043e5SJens Axboe /* 19959ba4b2dfSJens Axboe * Wakeup the flusher threads to start writeback of all currently dirty pages 199603ba3782SJens Axboe */ 19979ba4b2dfSJens Axboe void wakeup_flusher_threads(enum wb_reason reason) 199803ba3782SJens Axboe { 1999b8c2f347SChristoph Hellwig struct backing_dev_info *bdi; 2000b8c2f347SChristoph Hellwig 200151350ea0SKonstantin Khlebnikov /* 200251350ea0SKonstantin Khlebnikov * If we are expecting writeback progress we must submit plugged IO. 200351350ea0SKonstantin Khlebnikov */ 200451350ea0SKonstantin Khlebnikov if (blk_needs_flush_plug(current)) 200551350ea0SKonstantin Khlebnikov blk_schedule_flush_plug(current); 200651350ea0SKonstantin Khlebnikov 2007b8c2f347SChristoph Hellwig rcu_read_lock(); 2008595043e5SJens Axboe list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) 2009e8e8a0c6SJens Axboe __wakeup_flusher_threads_bdi(bdi, reason); 2010b8c2f347SChristoph Hellwig rcu_read_unlock(); 201103ba3782SJens Axboe } 201203ba3782SJens Axboe 2013a2f48706STheodore Ts'o /* 2014a2f48706STheodore Ts'o * Wake up bdi's periodically to make sure dirtytime inodes gets 2015a2f48706STheodore Ts'o * written back periodically. We deliberately do *not* check the 2016a2f48706STheodore Ts'o * b_dirtytime list in wb_has_dirty_io(), since this would cause the 2017a2f48706STheodore Ts'o * kernel to be constantly waking up once there are any dirtytime 2018a2f48706STheodore Ts'o * inodes on the system. So instead we define a separate delayed work 2019a2f48706STheodore Ts'o * function which gets called much more rarely. (By default, only 2020a2f48706STheodore Ts'o * once every 12 hours.) 2021a2f48706STheodore Ts'o * 2022a2f48706STheodore Ts'o * If there is any other write activity going on in the file system, 2023a2f48706STheodore Ts'o * this function won't be necessary. But if the only thing that has 2024a2f48706STheodore Ts'o * happened on the file system is a dirtytime inode caused by an atime 2025a2f48706STheodore Ts'o * update, we need this infrastructure below to make sure that inode 2026a2f48706STheodore Ts'o * eventually gets pushed out to disk. 2027a2f48706STheodore Ts'o */ 2028a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w); 2029a2f48706STheodore Ts'o static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback); 2030a2f48706STheodore Ts'o 2031a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w) 2032a2f48706STheodore Ts'o { 2033a2f48706STheodore Ts'o struct backing_dev_info *bdi; 2034a2f48706STheodore Ts'o 2035a2f48706STheodore Ts'o rcu_read_lock(); 2036a2f48706STheodore Ts'o list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 2037001fe6f6STejun Heo struct bdi_writeback *wb; 2038001fe6f6STejun Heo 2039b817525aSTejun Heo list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node) 20406fdf860fSTejun Heo if (!list_empty(&wb->b_dirty_time)) 20416fdf860fSTejun Heo wb_wakeup(wb); 2042a2f48706STheodore Ts'o } 2043a2f48706STheodore Ts'o rcu_read_unlock(); 2044a2f48706STheodore Ts'o schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ); 2045a2f48706STheodore Ts'o } 2046a2f48706STheodore Ts'o 2047a2f48706STheodore Ts'o static int __init start_dirtytime_writeback(void) 2048a2f48706STheodore Ts'o { 2049a2f48706STheodore Ts'o schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ); 2050a2f48706STheodore Ts'o return 0; 2051a2f48706STheodore Ts'o } 2052a2f48706STheodore Ts'o __initcall(start_dirtytime_writeback); 2053a2f48706STheodore Ts'o 20541efff914STheodore Ts'o int dirtytime_interval_handler(struct ctl_table *table, int write, 20551efff914STheodore Ts'o void __user *buffer, size_t *lenp, loff_t *ppos) 20561efff914STheodore Ts'o { 20571efff914STheodore Ts'o int ret; 20581efff914STheodore Ts'o 20591efff914STheodore Ts'o ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 20601efff914STheodore Ts'o if (ret == 0 && write) 20611efff914STheodore Ts'o mod_delayed_work(system_wq, &dirtytime_work, 0); 20621efff914STheodore Ts'o return ret; 20631efff914STheodore Ts'o } 20641efff914STheodore Ts'o 206503ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode) 206603ba3782SJens Axboe { 206703ba3782SJens Axboe if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { 206803ba3782SJens Axboe struct dentry *dentry; 206903ba3782SJens Axboe const char *name = "?"; 207003ba3782SJens Axboe 207103ba3782SJens Axboe dentry = d_find_alias(inode); 207203ba3782SJens Axboe if (dentry) { 207303ba3782SJens Axboe spin_lock(&dentry->d_lock); 207403ba3782SJens Axboe name = (const char *) dentry->d_name.name; 207503ba3782SJens Axboe } 207603ba3782SJens Axboe printk(KERN_DEBUG 207703ba3782SJens Axboe "%s(%d): dirtied inode %lu (%s) on %s\n", 207803ba3782SJens Axboe current->comm, task_pid_nr(current), inode->i_ino, 207903ba3782SJens Axboe name, inode->i_sb->s_id); 208003ba3782SJens Axboe if (dentry) { 208103ba3782SJens Axboe spin_unlock(&dentry->d_lock); 208203ba3782SJens Axboe dput(dentry); 208303ba3782SJens Axboe } 208403ba3782SJens Axboe } 208503ba3782SJens Axboe } 208603ba3782SJens Axboe 208703ba3782SJens Axboe /** 208803ba3782SJens Axboe * __mark_inode_dirty - internal function 20890117d427SMauro Carvalho Chehab * 209003ba3782SJens Axboe * @inode: inode to mark 209103ba3782SJens Axboe * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) 20920117d427SMauro Carvalho Chehab * 209303ba3782SJens Axboe * Mark an inode as dirty. Callers should use mark_inode_dirty or 209403ba3782SJens Axboe * mark_inode_dirty_sync. 209503ba3782SJens Axboe * 209603ba3782SJens Axboe * Put the inode on the super block's dirty list. 209703ba3782SJens Axboe * 209803ba3782SJens Axboe * CAREFUL! We mark it dirty unconditionally, but move it onto the 209903ba3782SJens Axboe * dirty list only if it is hashed or if it refers to a blockdev. 210003ba3782SJens Axboe * If it was not hashed, it will never be added to the dirty list 210103ba3782SJens Axboe * even if it is later hashed, as it will have been marked dirty already. 210203ba3782SJens Axboe * 210303ba3782SJens Axboe * In short, make sure you hash any inodes _before_ you start marking 210403ba3782SJens Axboe * them dirty. 210503ba3782SJens Axboe * 210603ba3782SJens Axboe * Note that for blockdevs, inode->dirtied_when represents the dirtying time of 210703ba3782SJens Axboe * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of 210803ba3782SJens Axboe * the kernel-internal blockdev inode represents the dirtying time of the 210903ba3782SJens Axboe * blockdev's pages. This is why for I_DIRTY_PAGES we always use 211003ba3782SJens Axboe * page->mapping->host, so the page-dirtying time is recorded in the internal 211103ba3782SJens Axboe * blockdev inode. 211203ba3782SJens Axboe */ 211303ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags) 211403ba3782SJens Axboe { 211503ba3782SJens Axboe struct super_block *sb = inode->i_sb; 21160ae45f63STheodore Ts'o int dirtytime; 21170ae45f63STheodore Ts'o 21180ae45f63STheodore Ts'o trace_writeback_mark_inode_dirty(inode, flags); 211903ba3782SJens Axboe 212003ba3782SJens Axboe /* 212103ba3782SJens Axboe * Don't do this for I_DIRTY_PAGES - that doesn't actually 212203ba3782SJens Axboe * dirty the inode itself 212303ba3782SJens Axboe */ 21240e11f644SChristoph Hellwig if (flags & (I_DIRTY_INODE | I_DIRTY_TIME)) { 21259fb0a7daSTejun Heo trace_writeback_dirty_inode_start(inode, flags); 21269fb0a7daSTejun Heo 212703ba3782SJens Axboe if (sb->s_op->dirty_inode) 2128aa385729SChristoph Hellwig sb->s_op->dirty_inode(inode, flags); 21299fb0a7daSTejun Heo 21309fb0a7daSTejun Heo trace_writeback_dirty_inode(inode, flags); 213103ba3782SJens Axboe } 21320ae45f63STheodore Ts'o if (flags & I_DIRTY_INODE) 21330ae45f63STheodore Ts'o flags &= ~I_DIRTY_TIME; 21340ae45f63STheodore Ts'o dirtytime = flags & I_DIRTY_TIME; 213503ba3782SJens Axboe 213603ba3782SJens Axboe /* 21379c6ac78eSTejun Heo * Paired with smp_mb() in __writeback_single_inode() for the 21389c6ac78eSTejun Heo * following lockless i_state test. See there for details. 213903ba3782SJens Axboe */ 214003ba3782SJens Axboe smp_mb(); 214103ba3782SJens Axboe 21420ae45f63STheodore Ts'o if (((inode->i_state & flags) == flags) || 21430ae45f63STheodore Ts'o (dirtytime && (inode->i_state & I_DIRTY_INODE))) 214403ba3782SJens Axboe return; 214503ba3782SJens Axboe 214603ba3782SJens Axboe if (unlikely(block_dump)) 214703ba3782SJens Axboe block_dump___mark_inode_dirty(inode); 214803ba3782SJens Axboe 2149250df6edSDave Chinner spin_lock(&inode->i_lock); 21500ae45f63STheodore Ts'o if (dirtytime && (inode->i_state & I_DIRTY_INODE)) 21510ae45f63STheodore Ts'o goto out_unlock_inode; 215203ba3782SJens Axboe if ((inode->i_state & flags) != flags) { 215303ba3782SJens Axboe const int was_dirty = inode->i_state & I_DIRTY; 215403ba3782SJens Axboe 215552ebea74STejun Heo inode_attach_wb(inode, NULL); 215652ebea74STejun Heo 21570ae45f63STheodore Ts'o if (flags & I_DIRTY_INODE) 21580ae45f63STheodore Ts'o inode->i_state &= ~I_DIRTY_TIME; 215903ba3782SJens Axboe inode->i_state |= flags; 216003ba3782SJens Axboe 216103ba3782SJens Axboe /* 216203ba3782SJens Axboe * If the inode is being synced, just update its dirty state. 216303ba3782SJens Axboe * The unlocker will place the inode on the appropriate 216403ba3782SJens Axboe * superblock list, based upon its state. 216503ba3782SJens Axboe */ 216603ba3782SJens Axboe if (inode->i_state & I_SYNC) 2167250df6edSDave Chinner goto out_unlock_inode; 216803ba3782SJens Axboe 216903ba3782SJens Axboe /* 217003ba3782SJens Axboe * Only add valid (hashed) inodes to the superblock's 217103ba3782SJens Axboe * dirty list. Add blockdev inodes as well. 217203ba3782SJens Axboe */ 217303ba3782SJens Axboe if (!S_ISBLK(inode->i_mode)) { 21741d3382cbSAl Viro if (inode_unhashed(inode)) 2175250df6edSDave Chinner goto out_unlock_inode; 217603ba3782SJens Axboe } 2177a4ffdde6SAl Viro if (inode->i_state & I_FREEING) 2178250df6edSDave Chinner goto out_unlock_inode; 217903ba3782SJens Axboe 218003ba3782SJens Axboe /* 218103ba3782SJens Axboe * If the inode was already on b_dirty/b_io/b_more_io, don't 218203ba3782SJens Axboe * reposition it (that would break b_dirty time-ordering). 218303ba3782SJens Axboe */ 218403ba3782SJens Axboe if (!was_dirty) { 218587e1d789STejun Heo struct bdi_writeback *wb; 2186d6c10f1fSTejun Heo struct list_head *dirty_list; 2187a66979abSDave Chinner bool wakeup_bdi = false; 2188500b067cSJens Axboe 218987e1d789STejun Heo wb = locked_inode_to_wb_and_lock_list(inode); 2190253c34e9SArtem Bityutskiy 21910747259dSTejun Heo WARN(bdi_cap_writeback_dirty(wb->bdi) && 21920747259dSTejun Heo !test_bit(WB_registered, &wb->state), 21930747259dSTejun Heo "bdi-%s not registered\n", wb->bdi->name); 219403ba3782SJens Axboe 219503ba3782SJens Axboe inode->dirtied_when = jiffies; 2196a2f48706STheodore Ts'o if (dirtytime) 2197a2f48706STheodore Ts'o inode->dirtied_time_when = jiffies; 2198d6c10f1fSTejun Heo 21990e11f644SChristoph Hellwig if (inode->i_state & I_DIRTY) 22000747259dSTejun Heo dirty_list = &wb->b_dirty; 2201a2f48706STheodore Ts'o else 22020747259dSTejun Heo dirty_list = &wb->b_dirty_time; 2203d6c10f1fSTejun Heo 2204c7f54084SDave Chinner wakeup_bdi = inode_io_list_move_locked(inode, wb, 2205d6c10f1fSTejun Heo dirty_list); 2206d6c10f1fSTejun Heo 22070747259dSTejun Heo spin_unlock(&wb->list_lock); 22080ae45f63STheodore Ts'o trace_writeback_dirty_inode_enqueue(inode); 2209253c34e9SArtem Bityutskiy 2210d6c10f1fSTejun Heo /* 2211d6c10f1fSTejun Heo * If this is the first dirty inode for this bdi, 2212d6c10f1fSTejun Heo * we have to wake-up the corresponding bdi thread 2213d6c10f1fSTejun Heo * to make sure background write-back happens 2214d6c10f1fSTejun Heo * later. 2215d6c10f1fSTejun Heo */ 22160747259dSTejun Heo if (bdi_cap_writeback_dirty(wb->bdi) && wakeup_bdi) 22170747259dSTejun Heo wb_wakeup_delayed(wb); 2218a66979abSDave Chinner return; 2219a66979abSDave Chinner } 2220a66979abSDave Chinner } 2221a66979abSDave Chinner out_unlock_inode: 2222a66979abSDave Chinner spin_unlock(&inode->i_lock); 222303ba3782SJens Axboe } 222403ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty); 222503ba3782SJens Axboe 2226e97fedb9SDave Chinner /* 2227e97fedb9SDave Chinner * The @s_sync_lock is used to serialise concurrent sync operations 2228e97fedb9SDave Chinner * to avoid lock contention problems with concurrent wait_sb_inodes() calls. 2229e97fedb9SDave Chinner * Concurrent callers will block on the s_sync_lock rather than doing contending 2230e97fedb9SDave Chinner * walks. The queueing maintains sync(2) required behaviour as all the IO that 2231e97fedb9SDave Chinner * has been issued up to the time this function is enter is guaranteed to be 2232e97fedb9SDave Chinner * completed by the time we have gained the lock and waited for all IO that is 2233e97fedb9SDave Chinner * in progress regardless of the order callers are granted the lock. 2234e97fedb9SDave Chinner */ 2235b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb) 223666f3b8e2SJens Axboe { 22376c60d2b5SDave Chinner LIST_HEAD(sync_list); 223838f21977SNick Piggin 223903ba3782SJens Axboe /* 224003ba3782SJens Axboe * We need to be protected against the filesystem going from 224103ba3782SJens Axboe * r/o to r/w or vice versa. 224203ba3782SJens Axboe */ 2243b6e51316SJens Axboe WARN_ON(!rwsem_is_locked(&sb->s_umount)); 224403ba3782SJens Axboe 2245e97fedb9SDave Chinner mutex_lock(&sb->s_sync_lock); 224666f3b8e2SJens Axboe 224738f21977SNick Piggin /* 22486c60d2b5SDave Chinner * Splice the writeback list onto a temporary list to avoid waiting on 22496c60d2b5SDave Chinner * inodes that have started writeback after this point. 22506c60d2b5SDave Chinner * 22516c60d2b5SDave Chinner * Use rcu_read_lock() to keep the inodes around until we have a 22526c60d2b5SDave Chinner * reference. s_inode_wblist_lock protects sb->s_inodes_wb as well as 22536c60d2b5SDave Chinner * the local list because inodes can be dropped from either by writeback 22546c60d2b5SDave Chinner * completion. 225538f21977SNick Piggin */ 22566c60d2b5SDave Chinner rcu_read_lock(); 22576c60d2b5SDave Chinner spin_lock_irq(&sb->s_inode_wblist_lock); 22586c60d2b5SDave Chinner list_splice_init(&sb->s_inodes_wb, &sync_list); 22596c60d2b5SDave Chinner 22606c60d2b5SDave Chinner /* 22616c60d2b5SDave Chinner * Data integrity sync. Must wait for all pages under writeback, because 22626c60d2b5SDave Chinner * there may have been pages dirtied before our sync call, but which had 22636c60d2b5SDave Chinner * writeout started before we write it out. In which case, the inode 22646c60d2b5SDave Chinner * may not be on the dirty list, but we still have to wait for that 22656c60d2b5SDave Chinner * writeout. 22666c60d2b5SDave Chinner */ 22676c60d2b5SDave Chinner while (!list_empty(&sync_list)) { 22686c60d2b5SDave Chinner struct inode *inode = list_first_entry(&sync_list, struct inode, 22696c60d2b5SDave Chinner i_wb_list); 2270250df6edSDave Chinner struct address_space *mapping = inode->i_mapping; 227138f21977SNick Piggin 22726c60d2b5SDave Chinner /* 22736c60d2b5SDave Chinner * Move each inode back to the wb list before we drop the lock 22746c60d2b5SDave Chinner * to preserve consistency between i_wb_list and the mapping 22756c60d2b5SDave Chinner * writeback tag. Writeback completion is responsible to remove 22766c60d2b5SDave Chinner * the inode from either list once the writeback tag is cleared. 22776c60d2b5SDave Chinner */ 22786c60d2b5SDave Chinner list_move_tail(&inode->i_wb_list, &sb->s_inodes_wb); 22796c60d2b5SDave Chinner 22806c60d2b5SDave Chinner /* 22816c60d2b5SDave Chinner * The mapping can appear untagged while still on-list since we 22826c60d2b5SDave Chinner * do not have the mapping lock. Skip it here, wb completion 22836c60d2b5SDave Chinner * will remove it. 22846c60d2b5SDave Chinner */ 22856c60d2b5SDave Chinner if (!mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK)) 22866c60d2b5SDave Chinner continue; 22876c60d2b5SDave Chinner 22886c60d2b5SDave Chinner spin_unlock_irq(&sb->s_inode_wblist_lock); 22896c60d2b5SDave Chinner 2290250df6edSDave Chinner spin_lock(&inode->i_lock); 22916c60d2b5SDave Chinner if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) { 2292250df6edSDave Chinner spin_unlock(&inode->i_lock); 22936c60d2b5SDave Chinner 22946c60d2b5SDave Chinner spin_lock_irq(&sb->s_inode_wblist_lock); 229538f21977SNick Piggin continue; 2296250df6edSDave Chinner } 229738f21977SNick Piggin __iget(inode); 2298250df6edSDave Chinner spin_unlock(&inode->i_lock); 22996c60d2b5SDave Chinner rcu_read_unlock(); 230038f21977SNick Piggin 2301aa750fd7SJunichi Nomura /* 2302aa750fd7SJunichi Nomura * We keep the error status of individual mapping so that 2303aa750fd7SJunichi Nomura * applications can catch the writeback error using fsync(2). 2304aa750fd7SJunichi Nomura * See filemap_fdatawait_keep_errors() for details. 2305aa750fd7SJunichi Nomura */ 2306aa750fd7SJunichi Nomura filemap_fdatawait_keep_errors(mapping); 230738f21977SNick Piggin 230838f21977SNick Piggin cond_resched(); 230938f21977SNick Piggin 23106c60d2b5SDave Chinner iput(inode); 23116c60d2b5SDave Chinner 23126c60d2b5SDave Chinner rcu_read_lock(); 23136c60d2b5SDave Chinner spin_lock_irq(&sb->s_inode_wblist_lock); 231438f21977SNick Piggin } 23156c60d2b5SDave Chinner spin_unlock_irq(&sb->s_inode_wblist_lock); 23166c60d2b5SDave Chinner rcu_read_unlock(); 2317e97fedb9SDave Chinner mutex_unlock(&sb->s_sync_lock); 231866f3b8e2SJens Axboe } 23191da177e4SLinus Torvalds 2320f30a7d0cSTejun Heo static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr, 2321f30a7d0cSTejun Heo enum wb_reason reason, bool skip_if_busy) 23221da177e4SLinus Torvalds { 2323cc395d7fSTejun Heo DEFINE_WB_COMPLETION_ONSTACK(done); 232483ba7b07SChristoph Hellwig struct wb_writeback_work work = { 23253c4d7165SChristoph Hellwig .sb = sb, 23263c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_NONE, 23276e6938b6SWu Fengguang .tagged_writepages = 1, 232883ba7b07SChristoph Hellwig .done = &done, 23293259f8beSChris Mason .nr_pages = nr, 23300e175a18SCurt Wohlgemuth .reason = reason, 23313c4d7165SChristoph Hellwig }; 2332e7972912STejun Heo struct backing_dev_info *bdi = sb->s_bdi; 23330e3c9a22SJens Axboe 2334e7972912STejun Heo if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info) 23356eedc701SJan Kara return; 2336cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 2337f30a7d0cSTejun Heo 2338db125360STejun Heo bdi_split_work_to_wbs(sb->s_bdi, &work, skip_if_busy); 2339cc395d7fSTejun Heo wb_wait_for_completion(bdi, &done); 23401da177e4SLinus Torvalds } 2341f30a7d0cSTejun Heo 2342f30a7d0cSTejun Heo /** 2343f30a7d0cSTejun Heo * writeback_inodes_sb_nr - writeback dirty inodes from given super_block 2344f30a7d0cSTejun Heo * @sb: the superblock 2345f30a7d0cSTejun Heo * @nr: the number of pages to write 2346f30a7d0cSTejun Heo * @reason: reason why some writeback work initiated 2347f30a7d0cSTejun Heo * 2348f30a7d0cSTejun Heo * Start writeback on some inodes on this super_block. No guarantees are made 2349f30a7d0cSTejun Heo * on how many (if any) will be written, and this function does not wait 2350f30a7d0cSTejun Heo * for IO completion of submitted IO. 2351f30a7d0cSTejun Heo */ 2352f30a7d0cSTejun Heo void writeback_inodes_sb_nr(struct super_block *sb, 2353f30a7d0cSTejun Heo unsigned long nr, 2354f30a7d0cSTejun Heo enum wb_reason reason) 2355f30a7d0cSTejun Heo { 2356f30a7d0cSTejun Heo __writeback_inodes_sb_nr(sb, nr, reason, false); 2357f30a7d0cSTejun Heo } 23583259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr); 23593259f8beSChris Mason 23603259f8beSChris Mason /** 23613259f8beSChris Mason * writeback_inodes_sb - writeback dirty inodes from given super_block 23623259f8beSChris Mason * @sb: the superblock 2363786228abSMarcos Paulo de Souza * @reason: reason why some writeback work was initiated 23643259f8beSChris Mason * 23653259f8beSChris Mason * Start writeback on some inodes on this super_block. No guarantees are made 23663259f8beSChris Mason * on how many (if any) will be written, and this function does not wait 23673259f8beSChris Mason * for IO completion of submitted IO. 23683259f8beSChris Mason */ 23690e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 23703259f8beSChris Mason { 23710e175a18SCurt Wohlgemuth return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); 23723259f8beSChris Mason } 2373d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb); 2374d8a8559cSJens Axboe 2375d8a8559cSJens Axboe /** 237610ee27a0SMiao Xie * try_to_writeback_inodes_sb - try to start writeback if none underway 237710ee27a0SMiao Xie * @sb: the superblock 237810ee27a0SMiao Xie * @reason: reason why some writeback work was initiated 237910ee27a0SMiao Xie * 23808264c321SRakesh Pandit * Invoke __writeback_inodes_sb_nr if no writeback is currently underway. 238110ee27a0SMiao Xie */ 23828264c321SRakesh Pandit void try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 238310ee27a0SMiao Xie { 23848264c321SRakesh Pandit if (!down_read_trylock(&sb->s_umount)) 23858264c321SRakesh Pandit return; 23868264c321SRakesh Pandit 23878264c321SRakesh Pandit __writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason, true); 23888264c321SRakesh Pandit up_read(&sb->s_umount); 238910ee27a0SMiao Xie } 239010ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb); 23913259f8beSChris Mason 23923259f8beSChris Mason /** 2393d8a8559cSJens Axboe * sync_inodes_sb - sync sb inode pages 2394d8a8559cSJens Axboe * @sb: the superblock 2395d8a8559cSJens Axboe * 2396d8a8559cSJens Axboe * This function writes and waits on any dirty inode belonging to this 23970dc83bd3SJan Kara * super_block. 2398d8a8559cSJens Axboe */ 23990dc83bd3SJan Kara void sync_inodes_sb(struct super_block *sb) 2400d8a8559cSJens Axboe { 2401cc395d7fSTejun Heo DEFINE_WB_COMPLETION_ONSTACK(done); 240283ba7b07SChristoph Hellwig struct wb_writeback_work work = { 24033c4d7165SChristoph Hellwig .sb = sb, 24043c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_ALL, 24053c4d7165SChristoph Hellwig .nr_pages = LONG_MAX, 24063c4d7165SChristoph Hellwig .range_cyclic = 0, 240783ba7b07SChristoph Hellwig .done = &done, 24080e175a18SCurt Wohlgemuth .reason = WB_REASON_SYNC, 24097747bd4bSDave Chinner .for_sync = 1, 24103c4d7165SChristoph Hellwig }; 2411e7972912STejun Heo struct backing_dev_info *bdi = sb->s_bdi; 24123c4d7165SChristoph Hellwig 2413006a0973STejun Heo /* 2414006a0973STejun Heo * Can't skip on !bdi_has_dirty() because we should wait for !dirty 2415006a0973STejun Heo * inodes under writeback and I_DIRTY_TIME inodes ignored by 2416006a0973STejun Heo * bdi_has_dirty() need to be written out too. 2417006a0973STejun Heo */ 2418006a0973STejun Heo if (bdi == &noop_backing_dev_info) 24196eedc701SJan Kara return; 2420cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 2421cf37e972SChristoph Hellwig 2422db125360STejun Heo bdi_split_work_to_wbs(bdi, &work, false); 2423cc395d7fSTejun Heo wb_wait_for_completion(bdi, &done); 242483ba7b07SChristoph Hellwig 2425b6e51316SJens Axboe wait_sb_inodes(sb); 2426d8a8559cSJens Axboe } 2427d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb); 24281da177e4SLinus Torvalds 24291da177e4SLinus Torvalds /** 24301da177e4SLinus Torvalds * write_inode_now - write an inode to disk 24311da177e4SLinus Torvalds * @inode: inode to write to disk 24321da177e4SLinus Torvalds * @sync: whether the write should be synchronous or not 24331da177e4SLinus Torvalds * 24347f04c26dSAndrea Arcangeli * This function commits an inode to disk immediately if it is dirty. This is 24357f04c26dSAndrea Arcangeli * primarily needed by knfsd. 24367f04c26dSAndrea Arcangeli * 24377f04c26dSAndrea Arcangeli * The caller must either have a ref on the inode or must have set I_WILL_FREE. 24381da177e4SLinus Torvalds */ 24391da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync) 24401da177e4SLinus Torvalds { 24411da177e4SLinus Torvalds struct writeback_control wbc = { 24421da177e4SLinus Torvalds .nr_to_write = LONG_MAX, 244318914b18SMike Galbraith .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 2444111ebb6eSOGAWA Hirofumi .range_start = 0, 2445111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 24461da177e4SLinus Torvalds }; 24471da177e4SLinus Torvalds 24481da177e4SLinus Torvalds if (!mapping_cap_writeback_dirty(inode->i_mapping)) 244949364ce2SAndrew Morton wbc.nr_to_write = 0; 24501da177e4SLinus Torvalds 24511da177e4SLinus Torvalds might_sleep(); 2452aaf25593STejun Heo return writeback_single_inode(inode, &wbc); 24531da177e4SLinus Torvalds } 24541da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now); 24551da177e4SLinus Torvalds 24561da177e4SLinus Torvalds /** 24571da177e4SLinus Torvalds * sync_inode - write an inode and its pages to disk. 24581da177e4SLinus Torvalds * @inode: the inode to sync 24591da177e4SLinus Torvalds * @wbc: controls the writeback mode 24601da177e4SLinus Torvalds * 24611da177e4SLinus Torvalds * sync_inode() will write an inode and its pages to disk. It will also 24621da177e4SLinus Torvalds * correctly update the inode on its superblock's dirty inode lists and will 24631da177e4SLinus Torvalds * update inode->i_state. 24641da177e4SLinus Torvalds * 24651da177e4SLinus Torvalds * The caller must have a ref on the inode. 24661da177e4SLinus Torvalds */ 24671da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc) 24681da177e4SLinus Torvalds { 2469aaf25593STejun Heo return writeback_single_inode(inode, wbc); 24701da177e4SLinus Torvalds } 24711da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode); 2472c3765016SChristoph Hellwig 2473c3765016SChristoph Hellwig /** 2474c691b9d9SAndrew Morton * sync_inode_metadata - write an inode to disk 2475c3765016SChristoph Hellwig * @inode: the inode to sync 2476c3765016SChristoph Hellwig * @wait: wait for I/O to complete. 2477c3765016SChristoph Hellwig * 2478c691b9d9SAndrew Morton * Write an inode to disk and adjust its dirty state after completion. 2479c3765016SChristoph Hellwig * 2480c3765016SChristoph Hellwig * Note: only writes the actual inode, no associated data or other metadata. 2481c3765016SChristoph Hellwig */ 2482c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait) 2483c3765016SChristoph Hellwig { 2484c3765016SChristoph Hellwig struct writeback_control wbc = { 2485c3765016SChristoph Hellwig .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, 2486c3765016SChristoph Hellwig .nr_to_write = 0, /* metadata-only */ 2487c3765016SChristoph Hellwig }; 2488c3765016SChristoph Hellwig 2489c3765016SChristoph Hellwig return sync_inode(inode, &wbc); 2490c3765016SChristoph Hellwig } 2491c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata); 2492