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 129d6c10f1fSTejun Heo * @head: one of @wb->b_{dirty|io|more_io} 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 176f0054bb1STejun Heo static void wb_queue_work(struct bdi_writeback *wb, 1776585027aSJan Kara struct wb_writeback_work *work) 1786585027aSJan Kara { 1795634cc2aSTejun Heo trace_writeback_queue(wb, work); 1806585027aSJan Kara 181f0054bb1STejun Heo spin_lock_bh(&wb->work_lock); 1828a1270cdSTejun Heo if (!test_bit(WB_registered, &wb->state)) 1835acda9d1SJan Kara goto out_unlock; 184cc395d7fSTejun Heo if (work->done) 185cc395d7fSTejun Heo atomic_inc(&work->done->cnt); 186f0054bb1STejun Heo list_add_tail(&work->list, &wb->work_list); 187f0054bb1STejun Heo mod_delayed_work(bdi_wq, &wb->dwork, 0); 1885acda9d1SJan Kara out_unlock: 189f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 19003ba3782SJens Axboe } 1911da177e4SLinus Torvalds 192cc395d7fSTejun Heo /** 193cc395d7fSTejun Heo * wb_wait_for_completion - wait for completion of bdi_writeback_works 194cc395d7fSTejun Heo * @bdi: bdi work items were issued to 195cc395d7fSTejun Heo * @done: target wb_completion 196cc395d7fSTejun Heo * 197cc395d7fSTejun Heo * Wait for one or more work items issued to @bdi with their ->done field 198cc395d7fSTejun Heo * set to @done, which should have been defined with 199cc395d7fSTejun Heo * DEFINE_WB_COMPLETION_ONSTACK(). This function returns after all such 200cc395d7fSTejun Heo * work items are completed. Work items which are waited upon aren't freed 201cc395d7fSTejun Heo * automatically on completion. 202cc395d7fSTejun Heo */ 203cc395d7fSTejun Heo static void wb_wait_for_completion(struct backing_dev_info *bdi, 204cc395d7fSTejun Heo struct wb_completion *done) 205cc395d7fSTejun Heo { 206cc395d7fSTejun Heo atomic_dec(&done->cnt); /* put down the initial count */ 207cc395d7fSTejun Heo wait_event(bdi->wb_waitq, !atomic_read(&done->cnt)); 208cc395d7fSTejun Heo } 209cc395d7fSTejun Heo 210703c2708STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 211703c2708STejun Heo 2122a814908STejun Heo /* parameters for foreign inode detection, see wb_detach_inode() */ 2132a814908STejun Heo #define WB_FRN_TIME_SHIFT 13 /* 1s = 2^13, upto 8 secs w/ 16bit */ 2142a814908STejun Heo #define WB_FRN_TIME_AVG_SHIFT 3 /* avg = avg * 7/8 + new * 1/8 */ 2152a814908STejun Heo #define WB_FRN_TIME_CUT_DIV 2 /* ignore rounds < avg / 2 */ 2162a814908STejun Heo #define WB_FRN_TIME_PERIOD (2 * (1 << WB_FRN_TIME_SHIFT)) /* 2s */ 2172a814908STejun Heo 2182a814908STejun Heo #define WB_FRN_HIST_SLOTS 16 /* inode->i_wb_frn_history is 16bit */ 2192a814908STejun Heo #define WB_FRN_HIST_UNIT (WB_FRN_TIME_PERIOD / WB_FRN_HIST_SLOTS) 2202a814908STejun Heo /* each slot's duration is 2s / 16 */ 2212a814908STejun Heo #define WB_FRN_HIST_THR_SLOTS (WB_FRN_HIST_SLOTS / 2) 2222a814908STejun Heo /* if foreign slots >= 8, switch */ 2232a814908STejun Heo #define WB_FRN_HIST_MAX_SLOTS (WB_FRN_HIST_THR_SLOTS / 2 + 1) 2242a814908STejun Heo /* one round can affect upto 5 slots */ 2252a814908STejun Heo 226a1a0e23eSTejun Heo static atomic_t isw_nr_in_flight = ATOMIC_INIT(0); 227a1a0e23eSTejun Heo static struct workqueue_struct *isw_wq; 228a1a0e23eSTejun Heo 22921c6321fSTejun Heo void __inode_attach_wb(struct inode *inode, struct page *page) 23021c6321fSTejun Heo { 23121c6321fSTejun Heo struct backing_dev_info *bdi = inode_to_bdi(inode); 23221c6321fSTejun Heo struct bdi_writeback *wb = NULL; 23321c6321fSTejun Heo 23421c6321fSTejun Heo if (inode_cgwb_enabled(inode)) { 23521c6321fSTejun Heo struct cgroup_subsys_state *memcg_css; 23621c6321fSTejun Heo 23721c6321fSTejun Heo if (page) { 23821c6321fSTejun Heo memcg_css = mem_cgroup_css_from_page(page); 23921c6321fSTejun Heo wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 24021c6321fSTejun Heo } else { 24121c6321fSTejun Heo /* must pin memcg_css, see wb_get_create() */ 24221c6321fSTejun Heo memcg_css = task_get_css(current, memory_cgrp_id); 24321c6321fSTejun Heo wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 24421c6321fSTejun Heo css_put(memcg_css); 24521c6321fSTejun Heo } 24621c6321fSTejun Heo } 24721c6321fSTejun Heo 24821c6321fSTejun Heo if (!wb) 24921c6321fSTejun Heo wb = &bdi->wb; 25021c6321fSTejun Heo 25121c6321fSTejun Heo /* 25221c6321fSTejun Heo * There may be multiple instances of this function racing to 25321c6321fSTejun Heo * update the same inode. Use cmpxchg() to tell the winner. 25421c6321fSTejun Heo */ 25521c6321fSTejun Heo if (unlikely(cmpxchg(&inode->i_wb, NULL, wb))) 25621c6321fSTejun Heo wb_put(wb); 25721c6321fSTejun Heo } 25821c6321fSTejun Heo 259703c2708STejun Heo /** 26087e1d789STejun Heo * locked_inode_to_wb_and_lock_list - determine a locked inode's wb and lock it 26187e1d789STejun Heo * @inode: inode of interest with i_lock held 26287e1d789STejun Heo * 26387e1d789STejun Heo * Returns @inode's wb with its list_lock held. @inode->i_lock must be 26487e1d789STejun Heo * held on entry and is released on return. The returned wb is guaranteed 26587e1d789STejun Heo * to stay @inode's associated wb until its list_lock is released. 26687e1d789STejun Heo */ 26787e1d789STejun Heo static struct bdi_writeback * 26887e1d789STejun Heo locked_inode_to_wb_and_lock_list(struct inode *inode) 26987e1d789STejun Heo __releases(&inode->i_lock) 27087e1d789STejun Heo __acquires(&wb->list_lock) 27187e1d789STejun Heo { 27287e1d789STejun Heo while (true) { 27387e1d789STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 27487e1d789STejun Heo 27587e1d789STejun Heo /* 27687e1d789STejun Heo * inode_to_wb() association is protected by both 27787e1d789STejun Heo * @inode->i_lock and @wb->list_lock but list_lock nests 27887e1d789STejun Heo * outside i_lock. Drop i_lock and verify that the 27987e1d789STejun Heo * association hasn't changed after acquiring list_lock. 28087e1d789STejun Heo */ 28187e1d789STejun Heo wb_get(wb); 28287e1d789STejun Heo spin_unlock(&inode->i_lock); 28387e1d789STejun Heo spin_lock(&wb->list_lock); 28487e1d789STejun Heo 285aaa2cacfSTejun Heo /* i_wb may have changed inbetween, can't use inode_to_wb() */ 286614a4e37STejun Heo if (likely(wb == inode->i_wb)) { 287614a4e37STejun Heo wb_put(wb); /* @inode already has ref */ 288614a4e37STejun Heo return wb; 289614a4e37STejun Heo } 29087e1d789STejun Heo 29187e1d789STejun Heo spin_unlock(&wb->list_lock); 292614a4e37STejun Heo wb_put(wb); 29387e1d789STejun Heo cpu_relax(); 29487e1d789STejun Heo spin_lock(&inode->i_lock); 29587e1d789STejun Heo } 29687e1d789STejun Heo } 29787e1d789STejun Heo 29887e1d789STejun Heo /** 29987e1d789STejun Heo * inode_to_wb_and_lock_list - determine an inode's wb and lock it 30087e1d789STejun Heo * @inode: inode of interest 30187e1d789STejun Heo * 30287e1d789STejun Heo * Same as locked_inode_to_wb_and_lock_list() but @inode->i_lock isn't held 30387e1d789STejun Heo * on entry. 30487e1d789STejun Heo */ 30587e1d789STejun Heo static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode) 30687e1d789STejun Heo __acquires(&wb->list_lock) 30787e1d789STejun Heo { 30887e1d789STejun Heo spin_lock(&inode->i_lock); 30987e1d789STejun Heo return locked_inode_to_wb_and_lock_list(inode); 31087e1d789STejun Heo } 31187e1d789STejun Heo 312682aa8e1STejun Heo struct inode_switch_wbs_context { 313682aa8e1STejun Heo struct inode *inode; 314682aa8e1STejun Heo struct bdi_writeback *new_wb; 315682aa8e1STejun Heo 316682aa8e1STejun Heo struct rcu_head rcu_head; 317682aa8e1STejun Heo struct work_struct work; 318682aa8e1STejun Heo }; 319682aa8e1STejun Heo 320682aa8e1STejun Heo static void inode_switch_wbs_work_fn(struct work_struct *work) 321682aa8e1STejun Heo { 322682aa8e1STejun Heo struct inode_switch_wbs_context *isw = 323682aa8e1STejun Heo container_of(work, struct inode_switch_wbs_context, work); 324682aa8e1STejun Heo struct inode *inode = isw->inode; 325d10c8095STejun Heo struct address_space *mapping = inode->i_mapping; 326d10c8095STejun Heo struct bdi_writeback *old_wb = inode->i_wb; 327682aa8e1STejun Heo struct bdi_writeback *new_wb = isw->new_wb; 328d10c8095STejun Heo struct radix_tree_iter iter; 329d10c8095STejun Heo bool switched = false; 330d10c8095STejun Heo void **slot; 331682aa8e1STejun Heo 332682aa8e1STejun Heo /* 333682aa8e1STejun Heo * By the time control reaches here, RCU grace period has passed 334682aa8e1STejun Heo * since I_WB_SWITCH assertion and all wb stat update transactions 335682aa8e1STejun Heo * between unlocked_inode_to_wb_begin/end() are guaranteed to be 336682aa8e1STejun Heo * synchronizing against mapping->tree_lock. 337d10c8095STejun Heo * 338d10c8095STejun Heo * Grabbing old_wb->list_lock, inode->i_lock and mapping->tree_lock 339d10c8095STejun Heo * gives us exclusion against all wb related operations on @inode 340d10c8095STejun Heo * including IO list manipulations and stat updates. 341682aa8e1STejun Heo */ 342d10c8095STejun Heo if (old_wb < new_wb) { 343d10c8095STejun Heo spin_lock(&old_wb->list_lock); 344d10c8095STejun Heo spin_lock_nested(&new_wb->list_lock, SINGLE_DEPTH_NESTING); 345d10c8095STejun Heo } else { 346d10c8095STejun Heo spin_lock(&new_wb->list_lock); 347d10c8095STejun Heo spin_lock_nested(&old_wb->list_lock, SINGLE_DEPTH_NESTING); 348d10c8095STejun Heo } 349682aa8e1STejun Heo spin_lock(&inode->i_lock); 350d10c8095STejun Heo spin_lock_irq(&mapping->tree_lock); 351682aa8e1STejun Heo 352d10c8095STejun Heo /* 353d10c8095STejun Heo * Once I_FREEING is visible under i_lock, the eviction path owns 354c7f54084SDave Chinner * the inode and we shouldn't modify ->i_io_list. 355d10c8095STejun Heo */ 356d10c8095STejun Heo if (unlikely(inode->i_state & I_FREEING)) 357d10c8095STejun Heo goto skip_switch; 358d10c8095STejun Heo 359d10c8095STejun Heo /* 360d10c8095STejun Heo * Count and transfer stats. Note that PAGECACHE_TAG_DIRTY points 361d10c8095STejun Heo * to possibly dirty pages while PAGECACHE_TAG_WRITEBACK points to 362d10c8095STejun Heo * pages actually under underwriteback. 363d10c8095STejun Heo */ 364d10c8095STejun Heo radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter, 0, 365d10c8095STejun Heo PAGECACHE_TAG_DIRTY) { 366d10c8095STejun Heo struct page *page = radix_tree_deref_slot_protected(slot, 367d10c8095STejun Heo &mapping->tree_lock); 368d10c8095STejun Heo if (likely(page) && PageDirty(page)) { 369d10c8095STejun Heo __dec_wb_stat(old_wb, WB_RECLAIMABLE); 370d10c8095STejun Heo __inc_wb_stat(new_wb, WB_RECLAIMABLE); 371d10c8095STejun Heo } 372d10c8095STejun Heo } 373d10c8095STejun Heo 374d10c8095STejun Heo radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter, 0, 375d10c8095STejun Heo PAGECACHE_TAG_WRITEBACK) { 376d10c8095STejun Heo struct page *page = radix_tree_deref_slot_protected(slot, 377d10c8095STejun Heo &mapping->tree_lock); 378d10c8095STejun Heo if (likely(page)) { 379d10c8095STejun Heo WARN_ON_ONCE(!PageWriteback(page)); 380d10c8095STejun Heo __dec_wb_stat(old_wb, WB_WRITEBACK); 381d10c8095STejun Heo __inc_wb_stat(new_wb, WB_WRITEBACK); 382d10c8095STejun Heo } 383d10c8095STejun Heo } 384d10c8095STejun Heo 385d10c8095STejun Heo wb_get(new_wb); 386d10c8095STejun Heo 387d10c8095STejun Heo /* 388d10c8095STejun Heo * Transfer to @new_wb's IO list if necessary. The specific list 389d10c8095STejun Heo * @inode was on is ignored and the inode is put on ->b_dirty which 390d10c8095STejun Heo * is always correct including from ->b_dirty_time. The transfer 391d10c8095STejun Heo * preserves @inode->dirtied_when ordering. 392d10c8095STejun Heo */ 393c7f54084SDave Chinner if (!list_empty(&inode->i_io_list)) { 394d10c8095STejun Heo struct inode *pos; 395d10c8095STejun Heo 396c7f54084SDave Chinner inode_io_list_del_locked(inode, old_wb); 397d10c8095STejun Heo inode->i_wb = new_wb; 398c7f54084SDave Chinner list_for_each_entry(pos, &new_wb->b_dirty, i_io_list) 399d10c8095STejun Heo if (time_after_eq(inode->dirtied_when, 400d10c8095STejun Heo pos->dirtied_when)) 401d10c8095STejun Heo break; 402c7f54084SDave Chinner inode_io_list_move_locked(inode, new_wb, pos->i_io_list.prev); 403d10c8095STejun Heo } else { 404d10c8095STejun Heo inode->i_wb = new_wb; 405d10c8095STejun Heo } 406d10c8095STejun Heo 407d10c8095STejun Heo /* ->i_wb_frn updates may race wbc_detach_inode() but doesn't matter */ 408682aa8e1STejun Heo inode->i_wb_frn_winner = 0; 409682aa8e1STejun Heo inode->i_wb_frn_avg_time = 0; 410682aa8e1STejun Heo inode->i_wb_frn_history = 0; 411d10c8095STejun Heo switched = true; 412d10c8095STejun Heo skip_switch: 413682aa8e1STejun Heo /* 414682aa8e1STejun Heo * Paired with load_acquire in unlocked_inode_to_wb_begin() and 415682aa8e1STejun Heo * ensures that the new wb is visible if they see !I_WB_SWITCH. 416682aa8e1STejun Heo */ 417682aa8e1STejun Heo smp_store_release(&inode->i_state, inode->i_state & ~I_WB_SWITCH); 418682aa8e1STejun Heo 419d10c8095STejun Heo spin_unlock_irq(&mapping->tree_lock); 420682aa8e1STejun Heo spin_unlock(&inode->i_lock); 421d10c8095STejun Heo spin_unlock(&new_wb->list_lock); 422d10c8095STejun Heo spin_unlock(&old_wb->list_lock); 423d10c8095STejun Heo 424d10c8095STejun Heo if (switched) { 425d10c8095STejun Heo wb_wakeup(new_wb); 426d10c8095STejun Heo wb_put(old_wb); 427d10c8095STejun Heo } 428d10c8095STejun Heo wb_put(new_wb); 429682aa8e1STejun Heo 430682aa8e1STejun Heo iput(inode); 431682aa8e1STejun Heo kfree(isw); 432a1a0e23eSTejun Heo 433a1a0e23eSTejun Heo atomic_dec(&isw_nr_in_flight); 434682aa8e1STejun Heo } 435682aa8e1STejun Heo 436682aa8e1STejun Heo static void inode_switch_wbs_rcu_fn(struct rcu_head *rcu_head) 437682aa8e1STejun Heo { 438682aa8e1STejun Heo struct inode_switch_wbs_context *isw = container_of(rcu_head, 439682aa8e1STejun Heo struct inode_switch_wbs_context, rcu_head); 440682aa8e1STejun Heo 441682aa8e1STejun Heo /* needs to grab bh-unsafe locks, bounce to work item */ 442682aa8e1STejun Heo INIT_WORK(&isw->work, inode_switch_wbs_work_fn); 443a1a0e23eSTejun Heo queue_work(isw_wq, &isw->work); 444682aa8e1STejun Heo } 445682aa8e1STejun Heo 446682aa8e1STejun Heo /** 447682aa8e1STejun Heo * inode_switch_wbs - change the wb association of an inode 448682aa8e1STejun Heo * @inode: target inode 449682aa8e1STejun Heo * @new_wb_id: ID of the new wb 450682aa8e1STejun Heo * 451682aa8e1STejun Heo * Switch @inode's wb association to the wb identified by @new_wb_id. The 452682aa8e1STejun Heo * switching is performed asynchronously and may fail silently. 453682aa8e1STejun Heo */ 454682aa8e1STejun Heo static void inode_switch_wbs(struct inode *inode, int new_wb_id) 455682aa8e1STejun Heo { 456682aa8e1STejun Heo struct backing_dev_info *bdi = inode_to_bdi(inode); 457682aa8e1STejun Heo struct cgroup_subsys_state *memcg_css; 458682aa8e1STejun Heo struct inode_switch_wbs_context *isw; 459682aa8e1STejun Heo 460682aa8e1STejun Heo /* noop if seems to be already in progress */ 461682aa8e1STejun Heo if (inode->i_state & I_WB_SWITCH) 462682aa8e1STejun Heo return; 463682aa8e1STejun Heo 464682aa8e1STejun Heo isw = kzalloc(sizeof(*isw), GFP_ATOMIC); 465682aa8e1STejun Heo if (!isw) 466682aa8e1STejun Heo return; 467682aa8e1STejun Heo 468682aa8e1STejun Heo /* find and pin the new wb */ 469682aa8e1STejun Heo rcu_read_lock(); 470682aa8e1STejun Heo memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys); 471682aa8e1STejun Heo if (memcg_css) 472682aa8e1STejun Heo isw->new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 473682aa8e1STejun Heo rcu_read_unlock(); 474682aa8e1STejun Heo if (!isw->new_wb) 475682aa8e1STejun Heo goto out_free; 476682aa8e1STejun Heo 477682aa8e1STejun Heo /* while holding I_WB_SWITCH, no one else can update the association */ 478682aa8e1STejun Heo spin_lock(&inode->i_lock); 479a1a0e23eSTejun Heo if (!(inode->i_sb->s_flags & MS_ACTIVE) || 480a1a0e23eSTejun Heo inode->i_state & (I_WB_SWITCH | I_FREEING) || 481a1a0e23eSTejun Heo inode_to_wb(inode) == isw->new_wb) { 482a1a0e23eSTejun Heo spin_unlock(&inode->i_lock); 483a1a0e23eSTejun Heo goto out_free; 484a1a0e23eSTejun Heo } 485682aa8e1STejun Heo inode->i_state |= I_WB_SWITCH; 486682aa8e1STejun Heo spin_unlock(&inode->i_lock); 487682aa8e1STejun Heo 488682aa8e1STejun Heo ihold(inode); 489682aa8e1STejun Heo isw->inode = inode; 490682aa8e1STejun Heo 491a1a0e23eSTejun Heo atomic_inc(&isw_nr_in_flight); 492a1a0e23eSTejun Heo 493682aa8e1STejun Heo /* 494682aa8e1STejun Heo * In addition to synchronizing among switchers, I_WB_SWITCH tells 495682aa8e1STejun Heo * the RCU protected stat update paths to grab the mapping's 496682aa8e1STejun Heo * tree_lock so that stat transfer can synchronize against them. 497682aa8e1STejun Heo * Let's continue after I_WB_SWITCH is guaranteed to be visible. 498682aa8e1STejun Heo */ 499682aa8e1STejun Heo call_rcu(&isw->rcu_head, inode_switch_wbs_rcu_fn); 500682aa8e1STejun Heo return; 501682aa8e1STejun Heo 502682aa8e1STejun Heo out_free: 503682aa8e1STejun Heo if (isw->new_wb) 504682aa8e1STejun Heo wb_put(isw->new_wb); 505682aa8e1STejun Heo kfree(isw); 506682aa8e1STejun Heo } 507682aa8e1STejun Heo 50887e1d789STejun Heo /** 509b16b1debSTejun Heo * wbc_attach_and_unlock_inode - associate wbc with target inode and unlock it 510b16b1debSTejun Heo * @wbc: writeback_control of interest 511b16b1debSTejun Heo * @inode: target inode 512b16b1debSTejun Heo * 513b16b1debSTejun Heo * @inode is locked and about to be written back under the control of @wbc. 514b16b1debSTejun Heo * Record @inode's writeback context into @wbc and unlock the i_lock. On 515b16b1debSTejun Heo * writeback completion, wbc_detach_inode() should be called. This is used 516b16b1debSTejun Heo * to track the cgroup writeback context. 517b16b1debSTejun Heo */ 518b16b1debSTejun Heo void wbc_attach_and_unlock_inode(struct writeback_control *wbc, 519b16b1debSTejun Heo struct inode *inode) 520b16b1debSTejun Heo { 521dd73e4b7STejun Heo if (!inode_cgwb_enabled(inode)) { 522dd73e4b7STejun Heo spin_unlock(&inode->i_lock); 523dd73e4b7STejun Heo return; 524dd73e4b7STejun Heo } 525dd73e4b7STejun Heo 526b16b1debSTejun Heo wbc->wb = inode_to_wb(inode); 5272a814908STejun Heo wbc->inode = inode; 5282a814908STejun Heo 5292a814908STejun Heo wbc->wb_id = wbc->wb->memcg_css->id; 5302a814908STejun Heo wbc->wb_lcand_id = inode->i_wb_frn_winner; 5312a814908STejun Heo wbc->wb_tcand_id = 0; 5322a814908STejun Heo wbc->wb_bytes = 0; 5332a814908STejun Heo wbc->wb_lcand_bytes = 0; 5342a814908STejun Heo wbc->wb_tcand_bytes = 0; 5352a814908STejun Heo 536b16b1debSTejun Heo wb_get(wbc->wb); 537b16b1debSTejun Heo spin_unlock(&inode->i_lock); 538e8a7abf5STejun Heo 539e8a7abf5STejun Heo /* 540e8a7abf5STejun Heo * A dying wb indicates that the memcg-blkcg mapping has changed 541e8a7abf5STejun Heo * and a new wb is already serving the memcg. Switch immediately. 542e8a7abf5STejun Heo */ 543e8a7abf5STejun Heo if (unlikely(wb_dying(wbc->wb))) 544e8a7abf5STejun Heo inode_switch_wbs(inode, wbc->wb_id); 545b16b1debSTejun Heo } 546b16b1debSTejun Heo 547b16b1debSTejun Heo /** 5482a814908STejun Heo * wbc_detach_inode - disassociate wbc from inode and perform foreign detection 5492a814908STejun Heo * @wbc: writeback_control of the just finished writeback 550b16b1debSTejun Heo * 551b16b1debSTejun Heo * To be called after a writeback attempt of an inode finishes and undoes 552b16b1debSTejun Heo * wbc_attach_and_unlock_inode(). Can be called under any context. 5532a814908STejun Heo * 5542a814908STejun Heo * As concurrent write sharing of an inode is expected to be very rare and 5552a814908STejun Heo * memcg only tracks page ownership on first-use basis severely confining 5562a814908STejun Heo * the usefulness of such sharing, cgroup writeback tracks ownership 5572a814908STejun Heo * per-inode. While the support for concurrent write sharing of an inode 5582a814908STejun Heo * is deemed unnecessary, an inode being written to by different cgroups at 5592a814908STejun Heo * different points in time is a lot more common, and, more importantly, 5602a814908STejun Heo * charging only by first-use can too readily lead to grossly incorrect 5612a814908STejun Heo * behaviors (single foreign page can lead to gigabytes of writeback to be 5622a814908STejun Heo * incorrectly attributed). 5632a814908STejun Heo * 5642a814908STejun Heo * To resolve this issue, cgroup writeback detects the majority dirtier of 5652a814908STejun Heo * an inode and transfers the ownership to it. To avoid unnnecessary 5662a814908STejun Heo * oscillation, the detection mechanism keeps track of history and gives 5672a814908STejun Heo * out the switch verdict only if the foreign usage pattern is stable over 5682a814908STejun Heo * a certain amount of time and/or writeback attempts. 5692a814908STejun Heo * 5702a814908STejun Heo * On each writeback attempt, @wbc tries to detect the majority writer 5712a814908STejun Heo * using Boyer-Moore majority vote algorithm. In addition to the byte 5722a814908STejun Heo * count from the majority voting, it also counts the bytes written for the 5732a814908STejun Heo * current wb and the last round's winner wb (max of last round's current 5742a814908STejun Heo * wb, the winner from two rounds ago, and the last round's majority 5752a814908STejun Heo * candidate). Keeping track of the historical winner helps the algorithm 5762a814908STejun Heo * to semi-reliably detect the most active writer even when it's not the 5772a814908STejun Heo * absolute majority. 5782a814908STejun Heo * 5792a814908STejun Heo * Once the winner of the round is determined, whether the winner is 5802a814908STejun Heo * foreign or not and how much IO time the round consumed is recorded in 5812a814908STejun Heo * inode->i_wb_frn_history. If the amount of recorded foreign IO time is 5822a814908STejun Heo * over a certain threshold, the switch verdict is given. 583b16b1debSTejun Heo */ 584b16b1debSTejun Heo void wbc_detach_inode(struct writeback_control *wbc) 585b16b1debSTejun Heo { 5862a814908STejun Heo struct bdi_writeback *wb = wbc->wb; 5872a814908STejun Heo struct inode *inode = wbc->inode; 588dd73e4b7STejun Heo unsigned long avg_time, max_bytes, max_time; 589dd73e4b7STejun Heo u16 history; 5902a814908STejun Heo int max_id; 5912a814908STejun Heo 592dd73e4b7STejun Heo if (!wb) 593dd73e4b7STejun Heo return; 594dd73e4b7STejun Heo 595dd73e4b7STejun Heo history = inode->i_wb_frn_history; 596dd73e4b7STejun Heo avg_time = inode->i_wb_frn_avg_time; 597dd73e4b7STejun Heo 5982a814908STejun Heo /* pick the winner of this round */ 5992a814908STejun Heo if (wbc->wb_bytes >= wbc->wb_lcand_bytes && 6002a814908STejun Heo wbc->wb_bytes >= wbc->wb_tcand_bytes) { 6012a814908STejun Heo max_id = wbc->wb_id; 6022a814908STejun Heo max_bytes = wbc->wb_bytes; 6032a814908STejun Heo } else if (wbc->wb_lcand_bytes >= wbc->wb_tcand_bytes) { 6042a814908STejun Heo max_id = wbc->wb_lcand_id; 6052a814908STejun Heo max_bytes = wbc->wb_lcand_bytes; 6062a814908STejun Heo } else { 6072a814908STejun Heo max_id = wbc->wb_tcand_id; 6082a814908STejun Heo max_bytes = wbc->wb_tcand_bytes; 6092a814908STejun Heo } 6102a814908STejun Heo 6112a814908STejun Heo /* 6122a814908STejun Heo * Calculate the amount of IO time the winner consumed and fold it 6132a814908STejun Heo * into the running average kept per inode. If the consumed IO 6142a814908STejun Heo * time is lower than avag / WB_FRN_TIME_CUT_DIV, ignore it for 6152a814908STejun Heo * deciding whether to switch or not. This is to prevent one-off 6162a814908STejun Heo * small dirtiers from skewing the verdict. 6172a814908STejun Heo */ 6182a814908STejun Heo max_time = DIV_ROUND_UP((max_bytes >> PAGE_SHIFT) << WB_FRN_TIME_SHIFT, 6192a814908STejun Heo wb->avg_write_bandwidth); 6202a814908STejun Heo if (avg_time) 6212a814908STejun Heo avg_time += (max_time >> WB_FRN_TIME_AVG_SHIFT) - 6222a814908STejun Heo (avg_time >> WB_FRN_TIME_AVG_SHIFT); 6232a814908STejun Heo else 6242a814908STejun Heo avg_time = max_time; /* immediate catch up on first run */ 6252a814908STejun Heo 6262a814908STejun Heo if (max_time >= avg_time / WB_FRN_TIME_CUT_DIV) { 6272a814908STejun Heo int slots; 6282a814908STejun Heo 6292a814908STejun Heo /* 6302a814908STejun Heo * The switch verdict is reached if foreign wb's consume 6312a814908STejun Heo * more than a certain proportion of IO time in a 6322a814908STejun Heo * WB_FRN_TIME_PERIOD. This is loosely tracked by 16 slot 6332a814908STejun Heo * history mask where each bit represents one sixteenth of 6342a814908STejun Heo * the period. Determine the number of slots to shift into 6352a814908STejun Heo * history from @max_time. 6362a814908STejun Heo */ 6372a814908STejun Heo slots = min(DIV_ROUND_UP(max_time, WB_FRN_HIST_UNIT), 6382a814908STejun Heo (unsigned long)WB_FRN_HIST_MAX_SLOTS); 6392a814908STejun Heo history <<= slots; 6402a814908STejun Heo if (wbc->wb_id != max_id) 6412a814908STejun Heo history |= (1U << slots) - 1; 6422a814908STejun Heo 6432a814908STejun Heo /* 6442a814908STejun Heo * Switch if the current wb isn't the consistent winner. 6452a814908STejun Heo * If there are multiple closely competing dirtiers, the 6462a814908STejun Heo * inode may switch across them repeatedly over time, which 6472a814908STejun Heo * is okay. The main goal is avoiding keeping an inode on 6482a814908STejun Heo * the wrong wb for an extended period of time. 6492a814908STejun Heo */ 650682aa8e1STejun Heo if (hweight32(history) > WB_FRN_HIST_THR_SLOTS) 651682aa8e1STejun Heo inode_switch_wbs(inode, max_id); 6522a814908STejun Heo } 6532a814908STejun Heo 6542a814908STejun Heo /* 6552a814908STejun Heo * Multiple instances of this function may race to update the 6562a814908STejun Heo * following fields but we don't mind occassional inaccuracies. 6572a814908STejun Heo */ 6582a814908STejun Heo inode->i_wb_frn_winner = max_id; 6592a814908STejun Heo inode->i_wb_frn_avg_time = min(avg_time, (unsigned long)U16_MAX); 6602a814908STejun Heo inode->i_wb_frn_history = history; 6612a814908STejun Heo 662b16b1debSTejun Heo wb_put(wbc->wb); 663b16b1debSTejun Heo wbc->wb = NULL; 664b16b1debSTejun Heo } 665b16b1debSTejun Heo 666b16b1debSTejun Heo /** 6672a814908STejun Heo * wbc_account_io - account IO issued during writeback 6682a814908STejun Heo * @wbc: writeback_control of the writeback in progress 6692a814908STejun Heo * @page: page being written out 6702a814908STejun Heo * @bytes: number of bytes being written out 6712a814908STejun Heo * 6722a814908STejun Heo * @bytes from @page are about to written out during the writeback 6732a814908STejun Heo * controlled by @wbc. Keep the book for foreign inode detection. See 6742a814908STejun Heo * wbc_detach_inode(). 6752a814908STejun Heo */ 6762a814908STejun Heo void wbc_account_io(struct writeback_control *wbc, struct page *page, 6772a814908STejun Heo size_t bytes) 6782a814908STejun Heo { 6792a814908STejun Heo int id; 6802a814908STejun Heo 6812a814908STejun Heo /* 6822a814908STejun Heo * pageout() path doesn't attach @wbc to the inode being written 6832a814908STejun Heo * out. This is intentional as we don't want the function to block 6842a814908STejun Heo * behind a slow cgroup. Ultimately, we want pageout() to kick off 6852a814908STejun Heo * regular writeback instead of writing things out itself. 6862a814908STejun Heo */ 6872a814908STejun Heo if (!wbc->wb) 6882a814908STejun Heo return; 6892a814908STejun Heo 6902a814908STejun Heo id = mem_cgroup_css_from_page(page)->id; 6912a814908STejun Heo 6922a814908STejun Heo if (id == wbc->wb_id) { 6932a814908STejun Heo wbc->wb_bytes += bytes; 6942a814908STejun Heo return; 6952a814908STejun Heo } 6962a814908STejun Heo 6972a814908STejun Heo if (id == wbc->wb_lcand_id) 6982a814908STejun Heo wbc->wb_lcand_bytes += bytes; 6992a814908STejun Heo 7002a814908STejun Heo /* Boyer-Moore majority vote algorithm */ 7012a814908STejun Heo if (!wbc->wb_tcand_bytes) 7022a814908STejun Heo wbc->wb_tcand_id = id; 7032a814908STejun Heo if (id == wbc->wb_tcand_id) 7042a814908STejun Heo wbc->wb_tcand_bytes += bytes; 7052a814908STejun Heo else 7062a814908STejun Heo wbc->wb_tcand_bytes -= min(bytes, wbc->wb_tcand_bytes); 7072a814908STejun Heo } 7085aa2a96bSTejun Heo EXPORT_SYMBOL_GPL(wbc_account_io); 7092a814908STejun Heo 7102a814908STejun Heo /** 711703c2708STejun Heo * inode_congested - test whether an inode is congested 71260292bccSTejun Heo * @inode: inode to test for congestion (may be NULL) 713703c2708STejun Heo * @cong_bits: mask of WB_[a]sync_congested bits to test 714703c2708STejun Heo * 715703c2708STejun Heo * Tests whether @inode is congested. @cong_bits is the mask of congestion 716703c2708STejun Heo * bits to test and the return value is the mask of set bits. 717703c2708STejun Heo * 718703c2708STejun Heo * If cgroup writeback is enabled for @inode, the congestion state is 719703c2708STejun Heo * determined by whether the cgwb (cgroup bdi_writeback) for the blkcg 720703c2708STejun Heo * associated with @inode is congested; otherwise, the root wb's congestion 721703c2708STejun Heo * state is used. 72260292bccSTejun Heo * 72360292bccSTejun Heo * @inode is allowed to be NULL as this function is often called on 72460292bccSTejun Heo * mapping->host which is NULL for the swapper space. 725703c2708STejun Heo */ 726703c2708STejun Heo int inode_congested(struct inode *inode, int cong_bits) 727703c2708STejun Heo { 7285cb8b824STejun Heo /* 7295cb8b824STejun Heo * Once set, ->i_wb never becomes NULL while the inode is alive. 7305cb8b824STejun Heo * Start transaction iff ->i_wb is visible. 7315cb8b824STejun Heo */ 732aaa2cacfSTejun Heo if (inode && inode_to_wb_is_valid(inode)) { 7335cb8b824STejun Heo struct bdi_writeback *wb; 7345cb8b824STejun Heo bool locked, congested; 7355cb8b824STejun Heo 7365cb8b824STejun Heo wb = unlocked_inode_to_wb_begin(inode, &locked); 7375cb8b824STejun Heo congested = wb_congested(wb, cong_bits); 7385cb8b824STejun Heo unlocked_inode_to_wb_end(inode, locked); 7395cb8b824STejun Heo return congested; 740703c2708STejun Heo } 741703c2708STejun Heo 742703c2708STejun Heo return wb_congested(&inode_to_bdi(inode)->wb, cong_bits); 743703c2708STejun Heo } 744703c2708STejun Heo EXPORT_SYMBOL_GPL(inode_congested); 745703c2708STejun Heo 746f2b65121STejun Heo /** 747f2b65121STejun Heo * wb_split_bdi_pages - split nr_pages to write according to bandwidth 748f2b65121STejun Heo * @wb: target bdi_writeback to split @nr_pages to 749f2b65121STejun Heo * @nr_pages: number of pages to write for the whole bdi 750f2b65121STejun Heo * 751f2b65121STejun Heo * Split @wb's portion of @nr_pages according to @wb's write bandwidth in 752f2b65121STejun Heo * relation to the total write bandwidth of all wb's w/ dirty inodes on 753f2b65121STejun Heo * @wb->bdi. 754f2b65121STejun Heo */ 755f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) 756f2b65121STejun Heo { 757f2b65121STejun Heo unsigned long this_bw = wb->avg_write_bandwidth; 758f2b65121STejun Heo unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth); 759f2b65121STejun Heo 760f2b65121STejun Heo if (nr_pages == LONG_MAX) 761f2b65121STejun Heo return LONG_MAX; 762f2b65121STejun Heo 763f2b65121STejun Heo /* 764f2b65121STejun Heo * This may be called on clean wb's and proportional distribution 765f2b65121STejun Heo * may not make sense, just use the original @nr_pages in those 766f2b65121STejun Heo * cases. In general, we wanna err on the side of writing more. 767f2b65121STejun Heo */ 768f2b65121STejun Heo if (!tot_bw || this_bw >= tot_bw) 769f2b65121STejun Heo return nr_pages; 770f2b65121STejun Heo else 771f2b65121STejun Heo return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw); 772f2b65121STejun Heo } 773f2b65121STejun Heo 774db125360STejun Heo /** 775db125360STejun Heo * bdi_split_work_to_wbs - split a wb_writeback_work to all wb's of a bdi 776db125360STejun Heo * @bdi: target backing_dev_info 777db125360STejun Heo * @base_work: wb_writeback_work to issue 778db125360STejun Heo * @skip_if_busy: skip wb's which already have writeback in progress 779db125360STejun Heo * 780db125360STejun Heo * Split and issue @base_work to all wb's (bdi_writeback's) of @bdi which 781db125360STejun Heo * have dirty inodes. If @base_work->nr_page isn't %LONG_MAX, it's 782db125360STejun Heo * distributed to the busy wbs according to each wb's proportion in the 783db125360STejun Heo * total active write bandwidth of @bdi. 784db125360STejun Heo */ 785db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, 786db125360STejun Heo struct wb_writeback_work *base_work, 787db125360STejun Heo bool skip_if_busy) 788db125360STejun Heo { 789b817525aSTejun Heo struct bdi_writeback *last_wb = NULL; 790b33e18f6STejun Heo struct bdi_writeback *wb = list_entry(&bdi->wb_list, 791b817525aSTejun Heo struct bdi_writeback, bdi_node); 792db125360STejun Heo 793db125360STejun Heo might_sleep(); 794db125360STejun Heo restart: 795db125360STejun Heo rcu_read_lock(); 796b817525aSTejun Heo list_for_each_entry_continue_rcu(wb, &bdi->wb_list, bdi_node) { 7978a1270cdSTejun Heo DEFINE_WB_COMPLETION_ONSTACK(fallback_work_done); 7988a1270cdSTejun Heo struct wb_writeback_work fallback_work; 7998a1270cdSTejun Heo struct wb_writeback_work *work; 8008a1270cdSTejun Heo long nr_pages; 8018a1270cdSTejun Heo 802b817525aSTejun Heo if (last_wb) { 803b817525aSTejun Heo wb_put(last_wb); 804b817525aSTejun Heo last_wb = NULL; 805b817525aSTejun Heo } 806b817525aSTejun Heo 807006a0973STejun Heo /* SYNC_ALL writes out I_DIRTY_TIME too */ 808006a0973STejun Heo if (!wb_has_dirty_io(wb) && 809006a0973STejun Heo (base_work->sync_mode == WB_SYNC_NONE || 810006a0973STejun Heo list_empty(&wb->b_dirty_time))) 811006a0973STejun Heo continue; 812006a0973STejun Heo if (skip_if_busy && writeback_in_progress(wb)) 813db125360STejun Heo continue; 814db125360STejun Heo 8158a1270cdSTejun Heo nr_pages = wb_split_bdi_pages(wb, base_work->nr_pages); 8168a1270cdSTejun Heo 8178a1270cdSTejun Heo work = kmalloc(sizeof(*work), GFP_ATOMIC); 8188a1270cdSTejun Heo if (work) { 8198a1270cdSTejun Heo *work = *base_work; 8208a1270cdSTejun Heo work->nr_pages = nr_pages; 8218a1270cdSTejun Heo work->auto_free = 1; 8228a1270cdSTejun Heo wb_queue_work(wb, work); 8238a1270cdSTejun Heo continue; 824db125360STejun Heo } 8258a1270cdSTejun Heo 8268a1270cdSTejun Heo /* alloc failed, execute synchronously using on-stack fallback */ 8278a1270cdSTejun Heo work = &fallback_work; 8288a1270cdSTejun Heo *work = *base_work; 8298a1270cdSTejun Heo work->nr_pages = nr_pages; 8308a1270cdSTejun Heo work->auto_free = 0; 8318a1270cdSTejun Heo work->done = &fallback_work_done; 8328a1270cdSTejun Heo 8338a1270cdSTejun Heo wb_queue_work(wb, work); 8348a1270cdSTejun Heo 835b817525aSTejun Heo /* 836b817525aSTejun Heo * Pin @wb so that it stays on @bdi->wb_list. This allows 837b817525aSTejun Heo * continuing iteration from @wb after dropping and 838b817525aSTejun Heo * regrabbing rcu read lock. 839b817525aSTejun Heo */ 840b817525aSTejun Heo wb_get(wb); 841b817525aSTejun Heo last_wb = wb; 842b817525aSTejun Heo 843db125360STejun Heo rcu_read_unlock(); 8448a1270cdSTejun Heo wb_wait_for_completion(bdi, &fallback_work_done); 845db125360STejun Heo goto restart; 846db125360STejun Heo } 847db125360STejun Heo rcu_read_unlock(); 848b817525aSTejun Heo 849b817525aSTejun Heo if (last_wb) 850b817525aSTejun Heo wb_put(last_wb); 851db125360STejun Heo } 852db125360STejun Heo 853a1a0e23eSTejun Heo /** 854a1a0e23eSTejun Heo * cgroup_writeback_umount - flush inode wb switches for umount 855a1a0e23eSTejun Heo * 856a1a0e23eSTejun Heo * This function is called when a super_block is about to be destroyed and 857a1a0e23eSTejun Heo * flushes in-flight inode wb switches. An inode wb switch goes through 858a1a0e23eSTejun Heo * RCU and then workqueue, so the two need to be flushed in order to ensure 859a1a0e23eSTejun Heo * that all previously scheduled switches are finished. As wb switches are 860a1a0e23eSTejun Heo * rare occurrences and synchronize_rcu() can take a while, perform 861a1a0e23eSTejun Heo * flushing iff wb switches are in flight. 862a1a0e23eSTejun Heo */ 863a1a0e23eSTejun Heo void cgroup_writeback_umount(void) 864a1a0e23eSTejun Heo { 865a1a0e23eSTejun Heo if (atomic_read(&isw_nr_in_flight)) { 866a1a0e23eSTejun Heo synchronize_rcu(); 867a1a0e23eSTejun Heo flush_workqueue(isw_wq); 868a1a0e23eSTejun Heo } 869a1a0e23eSTejun Heo } 870a1a0e23eSTejun Heo 871a1a0e23eSTejun Heo static int __init cgroup_writeback_init(void) 872a1a0e23eSTejun Heo { 873a1a0e23eSTejun Heo isw_wq = alloc_workqueue("inode_switch_wbs", 0, 0); 874a1a0e23eSTejun Heo if (!isw_wq) 875a1a0e23eSTejun Heo return -ENOMEM; 876a1a0e23eSTejun Heo return 0; 877a1a0e23eSTejun Heo } 878a1a0e23eSTejun Heo fs_initcall(cgroup_writeback_init); 879a1a0e23eSTejun Heo 880f2b65121STejun Heo #else /* CONFIG_CGROUP_WRITEBACK */ 881f2b65121STejun Heo 88287e1d789STejun Heo static struct bdi_writeback * 88387e1d789STejun Heo locked_inode_to_wb_and_lock_list(struct inode *inode) 88487e1d789STejun Heo __releases(&inode->i_lock) 88587e1d789STejun Heo __acquires(&wb->list_lock) 88687e1d789STejun Heo { 88787e1d789STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 88887e1d789STejun Heo 88987e1d789STejun Heo spin_unlock(&inode->i_lock); 89087e1d789STejun Heo spin_lock(&wb->list_lock); 89187e1d789STejun Heo return wb; 89287e1d789STejun Heo } 89387e1d789STejun Heo 89487e1d789STejun Heo static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode) 89587e1d789STejun Heo __acquires(&wb->list_lock) 89687e1d789STejun Heo { 89787e1d789STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 89887e1d789STejun Heo 89987e1d789STejun Heo spin_lock(&wb->list_lock); 90087e1d789STejun Heo return wb; 90187e1d789STejun Heo } 90287e1d789STejun Heo 903f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) 904f2b65121STejun Heo { 905f2b65121STejun Heo return nr_pages; 906f2b65121STejun Heo } 907f2b65121STejun Heo 908db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, 909db125360STejun Heo struct wb_writeback_work *base_work, 910db125360STejun Heo bool skip_if_busy) 911db125360STejun Heo { 912db125360STejun Heo might_sleep(); 913db125360STejun Heo 914006a0973STejun Heo if (!skip_if_busy || !writeback_in_progress(&bdi->wb)) { 915db125360STejun Heo base_work->auto_free = 0; 916db125360STejun Heo wb_queue_work(&bdi->wb, base_work); 917db125360STejun Heo } 918db125360STejun Heo } 919db125360STejun Heo 920703c2708STejun Heo #endif /* CONFIG_CGROUP_WRITEBACK */ 921703c2708STejun Heo 922c00ddad3STejun Heo void wb_start_writeback(struct bdi_writeback *wb, long nr_pages, 923c00ddad3STejun Heo bool range_cyclic, enum wb_reason reason) 924b6e51316SJens Axboe { 925c00ddad3STejun Heo struct wb_writeback_work *work; 926c00ddad3STejun Heo 927c00ddad3STejun Heo if (!wb_has_dirty_io(wb)) 928c00ddad3STejun Heo return; 929c00ddad3STejun Heo 930c00ddad3STejun Heo /* 931c00ddad3STejun Heo * This is WB_SYNC_NONE writeback, so if allocation fails just 932c00ddad3STejun Heo * wakeup the thread for old dirty data writeback 933c00ddad3STejun Heo */ 934c00ddad3STejun Heo work = kzalloc(sizeof(*work), GFP_ATOMIC); 935c00ddad3STejun Heo if (!work) { 9365634cc2aSTejun Heo trace_writeback_nowork(wb); 937c00ddad3STejun Heo wb_wakeup(wb); 938c00ddad3STejun Heo return; 939c00ddad3STejun Heo } 940c00ddad3STejun Heo 941c00ddad3STejun Heo work->sync_mode = WB_SYNC_NONE; 942c00ddad3STejun Heo work->nr_pages = nr_pages; 943c00ddad3STejun Heo work->range_cyclic = range_cyclic; 944c00ddad3STejun Heo work->reason = reason; 945ac7b19a3STejun Heo work->auto_free = 1; 946c00ddad3STejun Heo 947c00ddad3STejun Heo wb_queue_work(wb, work); 948d3ddec76SWu Fengguang } 949d3ddec76SWu Fengguang 950c5444198SChristoph Hellwig /** 9519ecf4866STejun Heo * wb_start_background_writeback - start background writeback 9529ecf4866STejun Heo * @wb: bdi_writback to write from 953c5444198SChristoph Hellwig * 954c5444198SChristoph Hellwig * Description: 9556585027aSJan Kara * This makes sure WB_SYNC_NONE background writeback happens. When 9569ecf4866STejun Heo * this function returns, it is only guaranteed that for given wb 9576585027aSJan Kara * some IO is happening if we are over background dirty threshold. 9586585027aSJan Kara * Caller need not hold sb s_umount semaphore. 959c5444198SChristoph Hellwig */ 9609ecf4866STejun Heo void wb_start_background_writeback(struct bdi_writeback *wb) 961c5444198SChristoph Hellwig { 9626585027aSJan Kara /* 9636585027aSJan Kara * We just wake up the flusher thread. It will perform background 9646585027aSJan Kara * writeback as soon as there is no other work to do. 9656585027aSJan Kara */ 9665634cc2aSTejun Heo trace_writeback_wake_background(wb); 9679ecf4866STejun Heo wb_wakeup(wb); 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds /* 971a66979abSDave Chinner * Remove the inode from the writeback list it is on. 972a66979abSDave Chinner */ 973c7f54084SDave Chinner void inode_io_list_del(struct inode *inode) 974a66979abSDave Chinner { 97587e1d789STejun Heo struct bdi_writeback *wb; 976a66979abSDave Chinner 97787e1d789STejun Heo wb = inode_to_wb_and_lock_list(inode); 978c7f54084SDave Chinner inode_io_list_del_locked(inode, wb); 97952ebea74STejun Heo spin_unlock(&wb->list_lock); 980f758eeabSChristoph Hellwig } 981a66979abSDave Chinner 982a66979abSDave Chinner /* 9836610a0bcSAndrew Morton * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 9846610a0bcSAndrew Morton * furthest end of its superblock's dirty-inode list. 9856610a0bcSAndrew Morton * 9866610a0bcSAndrew Morton * Before stamping the inode's ->dirtied_when, we check to see whether it is 98766f3b8e2SJens Axboe * already the most-recently-dirtied inode on the b_dirty list. If that is 9886610a0bcSAndrew Morton * the case then the inode must have been redirtied while it was being written 9896610a0bcSAndrew Morton * out and we don't reset its dirtied_when. 9906610a0bcSAndrew Morton */ 991f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb) 9926610a0bcSAndrew Morton { 99303ba3782SJens Axboe if (!list_empty(&wb->b_dirty)) { 99466f3b8e2SJens Axboe struct inode *tail; 9956610a0bcSAndrew Morton 9967ccf19a8SNick Piggin tail = wb_inode(wb->b_dirty.next); 99766f3b8e2SJens Axboe if (time_before(inode->dirtied_when, tail->dirtied_when)) 9986610a0bcSAndrew Morton inode->dirtied_when = jiffies; 9996610a0bcSAndrew Morton } 1000c7f54084SDave Chinner inode_io_list_move_locked(inode, wb, &wb->b_dirty); 10016610a0bcSAndrew Morton } 10026610a0bcSAndrew Morton 10036610a0bcSAndrew Morton /* 100466f3b8e2SJens Axboe * requeue inode for re-scanning after bdi->b_io list is exhausted. 1005c986d1e2SAndrew Morton */ 1006f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb) 1007c986d1e2SAndrew Morton { 1008c7f54084SDave Chinner inode_io_list_move_locked(inode, wb, &wb->b_more_io); 1009c986d1e2SAndrew Morton } 1010c986d1e2SAndrew Morton 10111c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode) 10121c0eeaf5SJoern Engel { 1013365b94aeSJan Kara inode->i_state &= ~I_SYNC; 10144eff96ddSJan Kara /* If inode is clean an unused, put it into LRU now... */ 10154eff96ddSJan Kara inode_add_lru(inode); 1016365b94aeSJan Kara /* Waiters must see I_SYNC cleared before being woken up */ 10171c0eeaf5SJoern Engel smp_mb(); 10181c0eeaf5SJoern Engel wake_up_bit(&inode->i_state, __I_SYNC); 10191c0eeaf5SJoern Engel } 10201c0eeaf5SJoern Engel 1021d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t) 1022d2caa3c5SJeff Layton { 1023d2caa3c5SJeff Layton bool ret = time_after(inode->dirtied_when, t); 1024d2caa3c5SJeff Layton #ifndef CONFIG_64BIT 1025d2caa3c5SJeff Layton /* 1026d2caa3c5SJeff Layton * For inodes being constantly redirtied, dirtied_when can get stuck. 1027d2caa3c5SJeff Layton * It _appears_ to be in the future, but is actually in distant past. 1028d2caa3c5SJeff Layton * This test is necessary to prevent such wrapped-around relative times 10295b0830cbSJens Axboe * from permanently stopping the whole bdi writeback. 1030d2caa3c5SJeff Layton */ 1031d2caa3c5SJeff Layton ret = ret && time_before_eq(inode->dirtied_when, jiffies); 1032d2caa3c5SJeff Layton #endif 1033d2caa3c5SJeff Layton return ret; 1034d2caa3c5SJeff Layton } 1035d2caa3c5SJeff Layton 10360ae45f63STheodore Ts'o #define EXPIRE_DIRTY_ATIME 0x0001 10370ae45f63STheodore Ts'o 1038c986d1e2SAndrew Morton /* 10390e2f2b23SWang Sheng-Hui * Move expired (dirtied before work->older_than_this) dirty inodes from 1040697e6fedSJan Kara * @delaying_queue to @dispatch_queue. 10412c136579SFengguang Wu */ 1042e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue, 10432c136579SFengguang Wu struct list_head *dispatch_queue, 10440ae45f63STheodore Ts'o int flags, 1045ad4e38ddSCurt Wohlgemuth struct wb_writeback_work *work) 10462c136579SFengguang Wu { 10470ae45f63STheodore Ts'o unsigned long *older_than_this = NULL; 10480ae45f63STheodore Ts'o unsigned long expire_time; 10495c03449dSShaohua Li LIST_HEAD(tmp); 10505c03449dSShaohua Li struct list_head *pos, *node; 1051cf137307SJens Axboe struct super_block *sb = NULL; 10525c03449dSShaohua Li struct inode *inode; 1053cf137307SJens Axboe int do_sb_sort = 0; 1054e84d0a4fSWu Fengguang int moved = 0; 10555c03449dSShaohua Li 10560ae45f63STheodore Ts'o if ((flags & EXPIRE_DIRTY_ATIME) == 0) 10570ae45f63STheodore Ts'o older_than_this = work->older_than_this; 1058a2f48706STheodore Ts'o else if (!work->for_sync) { 1059a2f48706STheodore Ts'o expire_time = jiffies - (dirtytime_expire_interval * HZ); 10600ae45f63STheodore Ts'o older_than_this = &expire_time; 10610ae45f63STheodore Ts'o } 10622c136579SFengguang Wu while (!list_empty(delaying_queue)) { 10637ccf19a8SNick Piggin inode = wb_inode(delaying_queue->prev); 10640ae45f63STheodore Ts'o if (older_than_this && 10650ae45f63STheodore Ts'o inode_dirtied_after(inode, *older_than_this)) 10662c136579SFengguang Wu break; 1067c7f54084SDave Chinner list_move(&inode->i_io_list, &tmp); 1068a8855990SJan Kara moved++; 10690ae45f63STheodore Ts'o if (flags & EXPIRE_DIRTY_ATIME) 10700ae45f63STheodore Ts'o set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state); 1071a8855990SJan Kara if (sb_is_blkdev_sb(inode->i_sb)) 1072a8855990SJan Kara continue; 1073cf137307SJens Axboe if (sb && sb != inode->i_sb) 1074cf137307SJens Axboe do_sb_sort = 1; 1075cf137307SJens Axboe sb = inode->i_sb; 10765c03449dSShaohua Li } 10775c03449dSShaohua Li 1078cf137307SJens Axboe /* just one sb in list, splice to dispatch_queue and we're done */ 1079cf137307SJens Axboe if (!do_sb_sort) { 1080cf137307SJens Axboe list_splice(&tmp, dispatch_queue); 1081e84d0a4fSWu Fengguang goto out; 1082cf137307SJens Axboe } 1083cf137307SJens Axboe 10845c03449dSShaohua Li /* Move inodes from one superblock together */ 10855c03449dSShaohua Li while (!list_empty(&tmp)) { 10867ccf19a8SNick Piggin sb = wb_inode(tmp.prev)->i_sb; 10875c03449dSShaohua Li list_for_each_prev_safe(pos, node, &tmp) { 10887ccf19a8SNick Piggin inode = wb_inode(pos); 10895c03449dSShaohua Li if (inode->i_sb == sb) 1090c7f54084SDave Chinner list_move(&inode->i_io_list, dispatch_queue); 10912c136579SFengguang Wu } 10922c136579SFengguang Wu } 1093e84d0a4fSWu Fengguang out: 1094e84d0a4fSWu Fengguang return moved; 10955c03449dSShaohua Li } 10962c136579SFengguang Wu 10972c136579SFengguang Wu /* 10982c136579SFengguang Wu * Queue all expired dirty inodes for io, eldest first. 10994ea879b9SWu Fengguang * Before 11004ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 11014ea879b9SWu Fengguang * =============> gf edc BA 11024ea879b9SWu Fengguang * After 11034ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 11044ea879b9SWu Fengguang * =============> g fBAedc 11054ea879b9SWu Fengguang * | 11064ea879b9SWu Fengguang * +--> dequeue for IO 11072c136579SFengguang Wu */ 1108ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work) 11092c136579SFengguang Wu { 1110e84d0a4fSWu Fengguang int moved; 11110ae45f63STheodore Ts'o 1112f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 11134ea879b9SWu Fengguang list_splice_init(&wb->b_more_io, &wb->b_io); 11140ae45f63STheodore Ts'o moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work); 11150ae45f63STheodore Ts'o moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io, 11160ae45f63STheodore Ts'o EXPIRE_DIRTY_ATIME, work); 1117d6c10f1fSTejun Heo if (moved) 1118d6c10f1fSTejun Heo wb_io_lists_populated(wb); 1119ad4e38ddSCurt Wohlgemuth trace_writeback_queue_io(wb, work, moved); 112066f3b8e2SJens Axboe } 112166f3b8e2SJens Axboe 1122a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc) 112366f3b8e2SJens Axboe { 11249fb0a7daSTejun Heo int ret; 11259fb0a7daSTejun Heo 11269fb0a7daSTejun Heo if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) { 11279fb0a7daSTejun Heo trace_writeback_write_inode_start(inode, wbc); 11289fb0a7daSTejun Heo ret = inode->i_sb->s_op->write_inode(inode, wbc); 11299fb0a7daSTejun Heo trace_writeback_write_inode(inode, wbc); 11309fb0a7daSTejun Heo return ret; 11319fb0a7daSTejun Heo } 113203ba3782SJens Axboe return 0; 113366f3b8e2SJens Axboe } 113408d8e974SFengguang Wu 11352c136579SFengguang Wu /* 1136169ebd90SJan Kara * Wait for writeback on an inode to complete. Called with i_lock held. 1137169ebd90SJan Kara * Caller must make sure inode cannot go away when we drop i_lock. 113801c03194SChristoph Hellwig */ 1139169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode) 1140169ebd90SJan Kara __releases(inode->i_lock) 1141169ebd90SJan Kara __acquires(inode->i_lock) 114201c03194SChristoph Hellwig { 114301c03194SChristoph Hellwig DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); 114401c03194SChristoph Hellwig wait_queue_head_t *wqh; 114501c03194SChristoph Hellwig 114601c03194SChristoph Hellwig wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 114758a9d3d8SRichard Kennedy while (inode->i_state & I_SYNC) { 1148250df6edSDave Chinner spin_unlock(&inode->i_lock); 114974316201SNeilBrown __wait_on_bit(wqh, &wq, bit_wait, 115074316201SNeilBrown TASK_UNINTERRUPTIBLE); 1151250df6edSDave Chinner spin_lock(&inode->i_lock); 115258a9d3d8SRichard Kennedy } 115301c03194SChristoph Hellwig } 115401c03194SChristoph Hellwig 115501c03194SChristoph Hellwig /* 1156169ebd90SJan Kara * Wait for writeback on an inode to complete. Caller must have inode pinned. 1157169ebd90SJan Kara */ 1158169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode) 1159169ebd90SJan Kara { 1160169ebd90SJan Kara spin_lock(&inode->i_lock); 1161169ebd90SJan Kara __inode_wait_for_writeback(inode); 1162169ebd90SJan Kara spin_unlock(&inode->i_lock); 1163169ebd90SJan Kara } 1164169ebd90SJan Kara 1165169ebd90SJan Kara /* 1166169ebd90SJan Kara * Sleep until I_SYNC is cleared. This function must be called with i_lock 1167169ebd90SJan Kara * held and drops it. It is aimed for callers not holding any inode reference 1168169ebd90SJan Kara * so once i_lock is dropped, inode can go away. 1169169ebd90SJan Kara */ 1170169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode) 1171169ebd90SJan Kara __releases(inode->i_lock) 1172169ebd90SJan Kara { 1173169ebd90SJan Kara DEFINE_WAIT(wait); 1174169ebd90SJan Kara wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 1175169ebd90SJan Kara int sleep; 1176169ebd90SJan Kara 1177169ebd90SJan Kara prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); 1178169ebd90SJan Kara sleep = inode->i_state & I_SYNC; 1179169ebd90SJan Kara spin_unlock(&inode->i_lock); 1180169ebd90SJan Kara if (sleep) 1181169ebd90SJan Kara schedule(); 1182169ebd90SJan Kara finish_wait(wqh, &wait); 1183169ebd90SJan Kara } 1184169ebd90SJan Kara 1185169ebd90SJan Kara /* 1186ccb26b5aSJan Kara * Find proper writeback list for the inode depending on its current state and 1187ccb26b5aSJan Kara * possibly also change of its state while we were doing writeback. Here we 1188ccb26b5aSJan Kara * handle things such as livelock prevention or fairness of writeback among 1189ccb26b5aSJan Kara * inodes. This function can be called only by flusher thread - noone else 1190ccb26b5aSJan Kara * processes all inodes in writeback lists and requeueing inodes behind flusher 1191ccb26b5aSJan Kara * thread's back can have unexpected consequences. 1192ccb26b5aSJan Kara */ 1193ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb, 1194ccb26b5aSJan Kara struct writeback_control *wbc) 1195ccb26b5aSJan Kara { 1196ccb26b5aSJan Kara if (inode->i_state & I_FREEING) 1197ccb26b5aSJan Kara return; 1198ccb26b5aSJan Kara 1199ccb26b5aSJan Kara /* 1200ccb26b5aSJan Kara * Sync livelock prevention. Each inode is tagged and synced in one 1201ccb26b5aSJan Kara * shot. If still dirty, it will be redirty_tail()'ed below. Update 1202ccb26b5aSJan Kara * the dirty time to prevent enqueue and sync it again. 1203ccb26b5aSJan Kara */ 1204ccb26b5aSJan Kara if ((inode->i_state & I_DIRTY) && 1205ccb26b5aSJan Kara (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)) 1206ccb26b5aSJan Kara inode->dirtied_when = jiffies; 1207ccb26b5aSJan Kara 12084f8ad655SJan Kara if (wbc->pages_skipped) { 12094f8ad655SJan Kara /* 12104f8ad655SJan Kara * writeback is not making progress due to locked 12114f8ad655SJan Kara * buffers. Skip this inode for now. 12124f8ad655SJan Kara */ 12134f8ad655SJan Kara redirty_tail(inode, wb); 12144f8ad655SJan Kara return; 12154f8ad655SJan Kara } 12164f8ad655SJan Kara 1217ccb26b5aSJan Kara if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) { 1218ccb26b5aSJan Kara /* 1219ccb26b5aSJan Kara * We didn't write back all the pages. nfs_writepages() 1220ccb26b5aSJan Kara * sometimes bales out without doing anything. 1221ccb26b5aSJan Kara */ 1222ccb26b5aSJan Kara if (wbc->nr_to_write <= 0) { 1223ccb26b5aSJan Kara /* Slice used up. Queue for next turn. */ 1224ccb26b5aSJan Kara requeue_io(inode, wb); 1225ccb26b5aSJan Kara } else { 1226ccb26b5aSJan Kara /* 1227ccb26b5aSJan Kara * Writeback blocked by something other than 1228ccb26b5aSJan Kara * congestion. Delay the inode for some time to 1229ccb26b5aSJan Kara * avoid spinning on the CPU (100% iowait) 1230ccb26b5aSJan Kara * retrying writeback of the dirty page/inode 1231ccb26b5aSJan Kara * that cannot be performed immediately. 1232ccb26b5aSJan Kara */ 1233ccb26b5aSJan Kara redirty_tail(inode, wb); 1234ccb26b5aSJan Kara } 1235ccb26b5aSJan Kara } else if (inode->i_state & I_DIRTY) { 1236ccb26b5aSJan Kara /* 1237ccb26b5aSJan Kara * Filesystems can dirty the inode during writeback operations, 1238ccb26b5aSJan Kara * such as delayed allocation during submission or metadata 1239ccb26b5aSJan Kara * updates after data IO completion. 1240ccb26b5aSJan Kara */ 1241ccb26b5aSJan Kara redirty_tail(inode, wb); 12420ae45f63STheodore Ts'o } else if (inode->i_state & I_DIRTY_TIME) { 1243a2f48706STheodore Ts'o inode->dirtied_when = jiffies; 1244c7f54084SDave Chinner inode_io_list_move_locked(inode, wb, &wb->b_dirty_time); 1245ccb26b5aSJan Kara } else { 1246ccb26b5aSJan Kara /* The inode is clean. Remove from writeback lists. */ 1247c7f54084SDave Chinner inode_io_list_del_locked(inode, wb); 1248ccb26b5aSJan Kara } 1249ccb26b5aSJan Kara } 1250ccb26b5aSJan Kara 1251ccb26b5aSJan Kara /* 12524f8ad655SJan Kara * Write out an inode and its dirty pages. Do not update the writeback list 12534f8ad655SJan Kara * linkage. That is left to the caller. The caller is also responsible for 12544f8ad655SJan Kara * setting I_SYNC flag and calling inode_sync_complete() to clear it. 12551da177e4SLinus Torvalds */ 12561da177e4SLinus Torvalds static int 1257cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) 12581da177e4SLinus Torvalds { 12591da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 1260251d6a47SWu Fengguang long nr_to_write = wbc->nr_to_write; 126101c03194SChristoph Hellwig unsigned dirty; 12621da177e4SLinus Torvalds int ret; 12631da177e4SLinus Torvalds 12644f8ad655SJan Kara WARN_ON(!(inode->i_state & I_SYNC)); 12651da177e4SLinus Torvalds 12669fb0a7daSTejun Heo trace_writeback_single_inode_start(inode, wbc, nr_to_write); 12679fb0a7daSTejun Heo 12681da177e4SLinus Torvalds ret = do_writepages(mapping, wbc); 12691da177e4SLinus Torvalds 127026821ed4SChristoph Hellwig /* 127126821ed4SChristoph Hellwig * Make sure to wait on the data before writing out the metadata. 127226821ed4SChristoph Hellwig * This is important for filesystems that modify metadata on data 12737747bd4bSDave Chinner * I/O completion. We don't do it for sync(2) writeback because it has a 12747747bd4bSDave Chinner * separate, external IO completion path and ->sync_fs for guaranteeing 12757747bd4bSDave Chinner * inode metadata is written back correctly. 127626821ed4SChristoph Hellwig */ 12777747bd4bSDave Chinner if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) { 127826821ed4SChristoph Hellwig int err = filemap_fdatawait(mapping); 12791da177e4SLinus Torvalds if (ret == 0) 12801da177e4SLinus Torvalds ret = err; 12811da177e4SLinus Torvalds } 12821da177e4SLinus Torvalds 12835547e8aaSDmitry Monakhov /* 12845547e8aaSDmitry Monakhov * Some filesystems may redirty the inode during the writeback 12855547e8aaSDmitry Monakhov * due to delalloc, clear dirty metadata flags right before 12865547e8aaSDmitry Monakhov * write_inode() 12875547e8aaSDmitry Monakhov */ 1288250df6edSDave Chinner spin_lock(&inode->i_lock); 12899c6ac78eSTejun Heo 12905547e8aaSDmitry Monakhov dirty = inode->i_state & I_DIRTY; 1291a2f48706STheodore Ts'o if (inode->i_state & I_DIRTY_TIME) { 1292a2f48706STheodore Ts'o if ((dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) || 1293a2f48706STheodore Ts'o unlikely(inode->i_state & I_DIRTY_TIME_EXPIRED) || 1294a2f48706STheodore Ts'o unlikely(time_after(jiffies, 1295a2f48706STheodore Ts'o (inode->dirtied_time_when + 1296a2f48706STheodore Ts'o dirtytime_expire_interval * HZ)))) { 12970ae45f63STheodore Ts'o dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED; 12980ae45f63STheodore Ts'o trace_writeback_lazytime(inode); 12990ae45f63STheodore Ts'o } 1300a2f48706STheodore Ts'o } else 1301a2f48706STheodore Ts'o inode->i_state &= ~I_DIRTY_TIME_EXPIRED; 13020ae45f63STheodore Ts'o inode->i_state &= ~dirty; 13039c6ac78eSTejun Heo 13049c6ac78eSTejun Heo /* 13059c6ac78eSTejun Heo * Paired with smp_mb() in __mark_inode_dirty(). This allows 13069c6ac78eSTejun Heo * __mark_inode_dirty() to test i_state without grabbing i_lock - 13079c6ac78eSTejun Heo * either they see the I_DIRTY bits cleared or we see the dirtied 13089c6ac78eSTejun Heo * inode. 13099c6ac78eSTejun Heo * 13109c6ac78eSTejun Heo * I_DIRTY_PAGES is always cleared together above even if @mapping 13119c6ac78eSTejun Heo * still has dirty pages. The flag is reinstated after smp_mb() if 13129c6ac78eSTejun Heo * necessary. This guarantees that either __mark_inode_dirty() 13139c6ac78eSTejun Heo * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY. 13149c6ac78eSTejun Heo */ 13159c6ac78eSTejun Heo smp_mb(); 13169c6ac78eSTejun Heo 13179c6ac78eSTejun Heo if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 13189c6ac78eSTejun Heo inode->i_state |= I_DIRTY_PAGES; 13199c6ac78eSTejun Heo 1320250df6edSDave Chinner spin_unlock(&inode->i_lock); 13219c6ac78eSTejun Heo 13220ae45f63STheodore Ts'o if (dirty & I_DIRTY_TIME) 13230ae45f63STheodore Ts'o mark_inode_dirty_sync(inode); 132426821ed4SChristoph Hellwig /* Don't write the inode if only I_DIRTY_PAGES was set */ 13250ae45f63STheodore Ts'o if (dirty & ~I_DIRTY_PAGES) { 1326a9185b41SChristoph Hellwig int err = write_inode(inode, wbc); 13271da177e4SLinus Torvalds if (ret == 0) 13281da177e4SLinus Torvalds ret = err; 13291da177e4SLinus Torvalds } 13304f8ad655SJan Kara trace_writeback_single_inode(inode, wbc, nr_to_write); 13314f8ad655SJan Kara return ret; 13324f8ad655SJan Kara } 13334f8ad655SJan Kara 13344f8ad655SJan Kara /* 13354f8ad655SJan Kara * Write out an inode's dirty pages. Either the caller has an active reference 13364f8ad655SJan Kara * on the inode or the inode has I_WILL_FREE set. 13374f8ad655SJan Kara * 13384f8ad655SJan Kara * This function is designed to be called for writing back one inode which 13394f8ad655SJan Kara * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode() 13404f8ad655SJan Kara * and does more profound writeback list handling in writeback_sb_inodes(). 13414f8ad655SJan Kara */ 1342aaf25593STejun Heo static int writeback_single_inode(struct inode *inode, 13434f8ad655SJan Kara struct writeback_control *wbc) 13444f8ad655SJan Kara { 1345aaf25593STejun Heo struct bdi_writeback *wb; 13464f8ad655SJan Kara int ret = 0; 13474f8ad655SJan Kara 13484f8ad655SJan Kara spin_lock(&inode->i_lock); 13494f8ad655SJan Kara if (!atomic_read(&inode->i_count)) 13504f8ad655SJan Kara WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); 13514f8ad655SJan Kara else 13524f8ad655SJan Kara WARN_ON(inode->i_state & I_WILL_FREE); 13534f8ad655SJan Kara 13544f8ad655SJan Kara if (inode->i_state & I_SYNC) { 13554f8ad655SJan Kara if (wbc->sync_mode != WB_SYNC_ALL) 13564f8ad655SJan Kara goto out; 13574f8ad655SJan Kara /* 1358169ebd90SJan Kara * It's a data-integrity sync. We must wait. Since callers hold 1359169ebd90SJan Kara * inode reference or inode has I_WILL_FREE set, it cannot go 1360169ebd90SJan Kara * away under us. 13614f8ad655SJan Kara */ 1362169ebd90SJan Kara __inode_wait_for_writeback(inode); 13634f8ad655SJan Kara } 13644f8ad655SJan Kara WARN_ON(inode->i_state & I_SYNC); 13654f8ad655SJan Kara /* 1366f9b0e058SJan Kara * Skip inode if it is clean and we have no outstanding writeback in 1367f9b0e058SJan Kara * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this 1368f9b0e058SJan Kara * function since flusher thread may be doing for example sync in 1369f9b0e058SJan Kara * parallel and if we move the inode, it could get skipped. So here we 1370f9b0e058SJan Kara * make sure inode is on some writeback list and leave it there unless 1371f9b0e058SJan Kara * we have completely cleaned the inode. 13724f8ad655SJan Kara */ 13730ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL) && 1374f9b0e058SJan Kara (wbc->sync_mode != WB_SYNC_ALL || 1375f9b0e058SJan Kara !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))) 13764f8ad655SJan Kara goto out; 13774f8ad655SJan Kara inode->i_state |= I_SYNC; 1378b16b1debSTejun Heo wbc_attach_and_unlock_inode(wbc, inode); 13794f8ad655SJan Kara 1380cd8ed2a4SYan Hong ret = __writeback_single_inode(inode, wbc); 13811da177e4SLinus Torvalds 1382b16b1debSTejun Heo wbc_detach_inode(wbc); 1383aaf25593STejun Heo 1384aaf25593STejun Heo wb = inode_to_wb_and_lock_list(inode); 1385250df6edSDave Chinner spin_lock(&inode->i_lock); 13864f8ad655SJan Kara /* 13874f8ad655SJan Kara * If inode is clean, remove it from writeback lists. Otherwise don't 13884f8ad655SJan Kara * touch it. See comment above for explanation. 13894f8ad655SJan Kara */ 13900ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 1391c7f54084SDave Chinner inode_io_list_del_locked(inode, wb); 13924f8ad655SJan Kara spin_unlock(&wb->list_lock); 13931c0eeaf5SJoern Engel inode_sync_complete(inode); 13944f8ad655SJan Kara out: 13954f8ad655SJan Kara spin_unlock(&inode->i_lock); 13961da177e4SLinus Torvalds return ret; 13971da177e4SLinus Torvalds } 13981da177e4SLinus Torvalds 1399a88a341aSTejun Heo static long writeback_chunk_size(struct bdi_writeback *wb, 14001a12d8bdSWu Fengguang struct wb_writeback_work *work) 1401d46db3d5SWu Fengguang { 1402d46db3d5SWu Fengguang long pages; 1403d46db3d5SWu Fengguang 1404d46db3d5SWu Fengguang /* 1405d46db3d5SWu Fengguang * WB_SYNC_ALL mode does livelock avoidance by syncing dirty 1406d46db3d5SWu Fengguang * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX 1407d46db3d5SWu Fengguang * here avoids calling into writeback_inodes_wb() more than once. 1408d46db3d5SWu Fengguang * 1409d46db3d5SWu Fengguang * The intended call sequence for WB_SYNC_ALL writeback is: 1410d46db3d5SWu Fengguang * 1411d46db3d5SWu Fengguang * wb_writeback() 1412d46db3d5SWu Fengguang * writeback_sb_inodes() <== called only once 1413d46db3d5SWu Fengguang * write_cache_pages() <== called once for each inode 1414d46db3d5SWu Fengguang * (quickly) tag currently dirty pages 1415d46db3d5SWu Fengguang * (maybe slowly) sync all tagged pages 1416d46db3d5SWu Fengguang */ 1417d46db3d5SWu Fengguang if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages) 1418d46db3d5SWu Fengguang pages = LONG_MAX; 14191a12d8bdSWu Fengguang else { 1420a88a341aSTejun Heo pages = min(wb->avg_write_bandwidth / 2, 1421dcc25ae7STejun Heo global_wb_domain.dirty_limit / DIRTY_SCOPE); 14221a12d8bdSWu Fengguang pages = min(pages, work->nr_pages); 14231a12d8bdSWu Fengguang pages = round_down(pages + MIN_WRITEBACK_PAGES, 14241a12d8bdSWu Fengguang MIN_WRITEBACK_PAGES); 14251a12d8bdSWu Fengguang } 1426d46db3d5SWu Fengguang 1427d46db3d5SWu Fengguang return pages; 1428d46db3d5SWu Fengguang } 1429d46db3d5SWu Fengguang 143003ba3782SJens Axboe /* 1431f11c9c5cSEdward Shishkin * Write a portion of b_io inodes which belong to @sb. 1432edadfb10SChristoph Hellwig * 1433d46db3d5SWu Fengguang * Return the number of pages and/or inodes written. 14340ba13fd1SLinus Torvalds * 14350ba13fd1SLinus Torvalds * NOTE! This is called with wb->list_lock held, and will 14360ba13fd1SLinus Torvalds * unlock and relock that for each inode it ends up doing 14370ba13fd1SLinus Torvalds * IO for. 1438f11c9c5cSEdward Shishkin */ 1439d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb, 1440d46db3d5SWu Fengguang struct bdi_writeback *wb, 1441d46db3d5SWu Fengguang struct wb_writeback_work *work) 144203ba3782SJens Axboe { 1443d46db3d5SWu Fengguang struct writeback_control wbc = { 1444d46db3d5SWu Fengguang .sync_mode = work->sync_mode, 1445d46db3d5SWu Fengguang .tagged_writepages = work->tagged_writepages, 1446d46db3d5SWu Fengguang .for_kupdate = work->for_kupdate, 1447d46db3d5SWu Fengguang .for_background = work->for_background, 14487747bd4bSDave Chinner .for_sync = work->for_sync, 1449d46db3d5SWu Fengguang .range_cyclic = work->range_cyclic, 1450d46db3d5SWu Fengguang .range_start = 0, 1451d46db3d5SWu Fengguang .range_end = LLONG_MAX, 1452d46db3d5SWu Fengguang }; 1453d46db3d5SWu Fengguang unsigned long start_time = jiffies; 1454d46db3d5SWu Fengguang long write_chunk; 1455d46db3d5SWu Fengguang long wrote = 0; /* count both pages and inodes */ 1456d46db3d5SWu Fengguang 145703ba3782SJens Axboe while (!list_empty(&wb->b_io)) { 14587ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 1459aaf25593STejun Heo struct bdi_writeback *tmp_wb; 1460edadfb10SChristoph Hellwig 1461edadfb10SChristoph Hellwig if (inode->i_sb != sb) { 1462d46db3d5SWu Fengguang if (work->sb) { 1463edadfb10SChristoph Hellwig /* 1464edadfb10SChristoph Hellwig * We only want to write back data for this 1465edadfb10SChristoph Hellwig * superblock, move all inodes not belonging 1466edadfb10SChristoph Hellwig * to it back onto the dirty list. 1467edadfb10SChristoph Hellwig */ 1468f758eeabSChristoph Hellwig redirty_tail(inode, wb); 146966f3b8e2SJens Axboe continue; 147066f3b8e2SJens Axboe } 1471edadfb10SChristoph Hellwig 1472edadfb10SChristoph Hellwig /* 1473edadfb10SChristoph Hellwig * The inode belongs to a different superblock. 1474edadfb10SChristoph Hellwig * Bounce back to the caller to unpin this and 1475edadfb10SChristoph Hellwig * pin the next superblock. 1476edadfb10SChristoph Hellwig */ 1477d46db3d5SWu Fengguang break; 1478edadfb10SChristoph Hellwig } 1479edadfb10SChristoph Hellwig 14809843b76aSChristoph Hellwig /* 1481331cbdeeSWanpeng Li * Don't bother with new inodes or inodes being freed, first 1482331cbdeeSWanpeng Li * kind does not need periodic writeout yet, and for the latter 14839843b76aSChristoph Hellwig * kind writeout is handled by the freer. 14849843b76aSChristoph Hellwig */ 1485250df6edSDave Chinner spin_lock(&inode->i_lock); 14869843b76aSChristoph Hellwig if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { 1487250df6edSDave Chinner spin_unlock(&inode->i_lock); 1488fcc5c222SWu Fengguang redirty_tail(inode, wb); 14897ef0d737SNick Piggin continue; 14907ef0d737SNick Piggin } 1491cc1676d9SJan Kara if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) { 1492cc1676d9SJan Kara /* 1493cc1676d9SJan Kara * If this inode is locked for writeback and we are not 1494cc1676d9SJan Kara * doing writeback-for-data-integrity, move it to 1495cc1676d9SJan Kara * b_more_io so that writeback can proceed with the 1496cc1676d9SJan Kara * other inodes on s_io. 1497cc1676d9SJan Kara * 1498cc1676d9SJan Kara * We'll have another go at writing back this inode 1499cc1676d9SJan Kara * when we completed a full scan of b_io. 1500cc1676d9SJan Kara */ 1501cc1676d9SJan Kara spin_unlock(&inode->i_lock); 1502cc1676d9SJan Kara requeue_io(inode, wb); 1503cc1676d9SJan Kara trace_writeback_sb_inodes_requeue(inode); 1504cc1676d9SJan Kara continue; 1505cc1676d9SJan Kara } 1506f0d07b7fSJan Kara spin_unlock(&wb->list_lock); 1507f0d07b7fSJan Kara 15084f8ad655SJan Kara /* 15094f8ad655SJan Kara * We already requeued the inode if it had I_SYNC set and we 15104f8ad655SJan Kara * are doing WB_SYNC_NONE writeback. So this catches only the 15114f8ad655SJan Kara * WB_SYNC_ALL case. 15124f8ad655SJan Kara */ 1513169ebd90SJan Kara if (inode->i_state & I_SYNC) { 1514169ebd90SJan Kara /* Wait for I_SYNC. This function drops i_lock... */ 1515169ebd90SJan Kara inode_sleep_on_writeback(inode); 1516169ebd90SJan Kara /* Inode may be gone, start again */ 1517ead188f9SJan Kara spin_lock(&wb->list_lock); 1518169ebd90SJan Kara continue; 1519169ebd90SJan Kara } 15204f8ad655SJan Kara inode->i_state |= I_SYNC; 1521b16b1debSTejun Heo wbc_attach_and_unlock_inode(&wbc, inode); 1522169ebd90SJan Kara 1523a88a341aSTejun Heo write_chunk = writeback_chunk_size(wb, work); 1524d46db3d5SWu Fengguang wbc.nr_to_write = write_chunk; 1525d46db3d5SWu Fengguang wbc.pages_skipped = 0; 1526250df6edSDave Chinner 1527169ebd90SJan Kara /* 1528169ebd90SJan Kara * We use I_SYNC to pin the inode in memory. While it is set 1529169ebd90SJan Kara * evict_inode() will wait so the inode cannot be freed. 1530169ebd90SJan Kara */ 1531cd8ed2a4SYan Hong __writeback_single_inode(inode, &wbc); 1532d46db3d5SWu Fengguang 1533b16b1debSTejun Heo wbc_detach_inode(&wbc); 1534d46db3d5SWu Fengguang work->nr_pages -= write_chunk - wbc.nr_to_write; 1535d46db3d5SWu Fengguang wrote += write_chunk - wbc.nr_to_write; 1536590dca3aSChris Mason 1537590dca3aSChris Mason if (need_resched()) { 1538590dca3aSChris Mason /* 1539590dca3aSChris Mason * We're trying to balance between building up a nice 1540590dca3aSChris Mason * long list of IOs to improve our merge rate, and 1541590dca3aSChris Mason * getting those IOs out quickly for anyone throttling 1542590dca3aSChris Mason * in balance_dirty_pages(). cond_resched() doesn't 1543590dca3aSChris Mason * unplug, so get our IOs out the door before we 1544590dca3aSChris Mason * give up the CPU. 1545590dca3aSChris Mason */ 1546590dca3aSChris Mason blk_flush_plug(current); 1547590dca3aSChris Mason cond_resched(); 1548590dca3aSChris Mason } 1549590dca3aSChris Mason 1550aaf25593STejun Heo /* 1551aaf25593STejun Heo * Requeue @inode if still dirty. Be careful as @inode may 1552aaf25593STejun Heo * have been switched to another wb in the meantime. 1553aaf25593STejun Heo */ 1554aaf25593STejun Heo tmp_wb = inode_to_wb_and_lock_list(inode); 15554f8ad655SJan Kara spin_lock(&inode->i_lock); 15560ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 1557d46db3d5SWu Fengguang wrote++; 1558aaf25593STejun Heo requeue_inode(inode, tmp_wb, &wbc); 15594f8ad655SJan Kara inode_sync_complete(inode); 15600f1b1fd8SDave Chinner spin_unlock(&inode->i_lock); 1561590dca3aSChris Mason 1562aaf25593STejun Heo if (unlikely(tmp_wb != wb)) { 1563aaf25593STejun Heo spin_unlock(&tmp_wb->list_lock); 1564aaf25593STejun Heo spin_lock(&wb->list_lock); 1565aaf25593STejun Heo } 1566aaf25593STejun Heo 1567d46db3d5SWu Fengguang /* 1568d46db3d5SWu Fengguang * bail out to wb_writeback() often enough to check 1569d46db3d5SWu Fengguang * background threshold and other termination conditions. 1570d46db3d5SWu Fengguang */ 1571d46db3d5SWu Fengguang if (wrote) { 1572d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 1573d46db3d5SWu Fengguang break; 1574d46db3d5SWu Fengguang if (work->nr_pages <= 0) 1575d46db3d5SWu Fengguang break; 15761da177e4SLinus Torvalds } 15778bc3be27SFengguang Wu } 1578d46db3d5SWu Fengguang return wrote; 1579f11c9c5cSEdward Shishkin } 158038f21977SNick Piggin 1581d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb, 1582d46db3d5SWu Fengguang struct wb_writeback_work *work) 1583f11c9c5cSEdward Shishkin { 1584d46db3d5SWu Fengguang unsigned long start_time = jiffies; 1585d46db3d5SWu Fengguang long wrote = 0; 1586f11c9c5cSEdward Shishkin 1587f11c9c5cSEdward Shishkin while (!list_empty(&wb->b_io)) { 15887ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 1589f11c9c5cSEdward Shishkin struct super_block *sb = inode->i_sb; 1590f11c9c5cSEdward Shishkin 1591eb6ef3dfSKonstantin Khlebnikov if (!trylock_super(sb)) { 15920e995816SWu Fengguang /* 1593eb6ef3dfSKonstantin Khlebnikov * trylock_super() may fail consistently due to 15940e995816SWu Fengguang * s_umount being grabbed by someone else. Don't use 15950e995816SWu Fengguang * requeue_io() to avoid busy retrying the inode/sb. 15960e995816SWu Fengguang */ 15970e995816SWu Fengguang redirty_tail(inode, wb); 1598d19de7edSChristoph Hellwig continue; 1599334132aeSChristoph Hellwig } 1600d46db3d5SWu Fengguang wrote += writeback_sb_inodes(sb, wb, work); 1601eb6ef3dfSKonstantin Khlebnikov up_read(&sb->s_umount); 1602f11c9c5cSEdward Shishkin 1603d46db3d5SWu Fengguang /* refer to the same tests at the end of writeback_sb_inodes */ 1604d46db3d5SWu Fengguang if (wrote) { 1605d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 1606d46db3d5SWu Fengguang break; 1607d46db3d5SWu Fengguang if (work->nr_pages <= 0) 1608f11c9c5cSEdward Shishkin break; 1609f11c9c5cSEdward Shishkin } 1610d46db3d5SWu Fengguang } 161166f3b8e2SJens Axboe /* Leave any unwritten inodes on b_io */ 1612d46db3d5SWu Fengguang return wrote; 161366f3b8e2SJens Axboe } 161466f3b8e2SJens Axboe 16157d9f073bSWanpeng Li static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages, 16160e175a18SCurt Wohlgemuth enum wb_reason reason) 1617edadfb10SChristoph Hellwig { 1618d46db3d5SWu Fengguang struct wb_writeback_work work = { 1619d46db3d5SWu Fengguang .nr_pages = nr_pages, 1620d46db3d5SWu Fengguang .sync_mode = WB_SYNC_NONE, 1621d46db3d5SWu Fengguang .range_cyclic = 1, 16220e175a18SCurt Wohlgemuth .reason = reason, 1623d46db3d5SWu Fengguang }; 1624505a666eSLinus Torvalds struct blk_plug plug; 1625edadfb10SChristoph Hellwig 1626505a666eSLinus Torvalds blk_start_plug(&plug); 1627f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 1628424b351fSWu Fengguang if (list_empty(&wb->b_io)) 1629ad4e38ddSCurt Wohlgemuth queue_io(wb, &work); 1630d46db3d5SWu Fengguang __writeback_inodes_wb(wb, &work); 1631f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 1632505a666eSLinus Torvalds blk_finish_plug(&plug); 1633edadfb10SChristoph Hellwig 1634d46db3d5SWu Fengguang return nr_pages - work.nr_pages; 163566f3b8e2SJens Axboe } 163666f3b8e2SJens Axboe 163703ba3782SJens Axboe /* 163803ba3782SJens Axboe * Explicit flushing or periodic writeback of "old" data. 163903ba3782SJens Axboe * 164003ba3782SJens Axboe * Define "old": the first time one of an inode's pages is dirtied, we mark the 164103ba3782SJens Axboe * dirtying-time in the inode's address_space. So this periodic writeback code 164203ba3782SJens Axboe * just walks the superblock inode list, writing back any inodes which are 164303ba3782SJens Axboe * older than a specific point in time. 164403ba3782SJens Axboe * 164503ba3782SJens Axboe * Try to run once per dirty_writeback_interval. But if a writeback event 164603ba3782SJens Axboe * takes longer than a dirty_writeback_interval interval, then leave a 164703ba3782SJens Axboe * one-second gap. 164803ba3782SJens Axboe * 164903ba3782SJens Axboe * older_than_this takes precedence over nr_to_write. So we'll only write back 165003ba3782SJens Axboe * all dirty pages if they are all attached to "old" mappings. 165103ba3782SJens Axboe */ 1652c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb, 165383ba7b07SChristoph Hellwig struct wb_writeback_work *work) 165403ba3782SJens Axboe { 1655e98be2d5SWu Fengguang unsigned long wb_start = jiffies; 1656d46db3d5SWu Fengguang long nr_pages = work->nr_pages; 16570dc83bd3SJan Kara unsigned long oldest_jif; 1658a5989bdcSJan Kara struct inode *inode; 1659d46db3d5SWu Fengguang long progress; 1660505a666eSLinus Torvalds struct blk_plug plug; 166103ba3782SJens Axboe 16620dc83bd3SJan Kara oldest_jif = jiffies; 16630dc83bd3SJan Kara work->older_than_this = &oldest_jif; 166403ba3782SJens Axboe 1665505a666eSLinus Torvalds blk_start_plug(&plug); 1666e8dfc305SWu Fengguang spin_lock(&wb->list_lock); 166703ba3782SJens Axboe for (;;) { 166803ba3782SJens Axboe /* 1669d3ddec76SWu Fengguang * Stop writeback when nr_pages has been consumed 167003ba3782SJens Axboe */ 167183ba7b07SChristoph Hellwig if (work->nr_pages <= 0) 167203ba3782SJens Axboe break; 167303ba3782SJens Axboe 167403ba3782SJens Axboe /* 1675aa373cf5SJan Kara * Background writeout and kupdate-style writeback may 1676aa373cf5SJan Kara * run forever. Stop them if there is other work to do 1677aa373cf5SJan Kara * so that e.g. sync can proceed. They'll be restarted 1678aa373cf5SJan Kara * after the other works are all done. 1679aa373cf5SJan Kara */ 1680aa373cf5SJan Kara if ((work->for_background || work->for_kupdate) && 1681f0054bb1STejun Heo !list_empty(&wb->work_list)) 1682aa373cf5SJan Kara break; 1683aa373cf5SJan Kara 1684aa373cf5SJan Kara /* 1685d3ddec76SWu Fengguang * For background writeout, stop when we are below the 1686d3ddec76SWu Fengguang * background dirty threshold 168703ba3782SJens Axboe */ 1688aa661bbeSTejun Heo if (work->for_background && !wb_over_bg_thresh(wb)) 168903ba3782SJens Axboe break; 169003ba3782SJens Axboe 16911bc36b64SJan Kara /* 16921bc36b64SJan Kara * Kupdate and background works are special and we want to 16931bc36b64SJan Kara * include all inodes that need writing. Livelock avoidance is 16941bc36b64SJan Kara * handled by these works yielding to any other work so we are 16951bc36b64SJan Kara * safe. 16961bc36b64SJan Kara */ 1697ba9aa839SWu Fengguang if (work->for_kupdate) { 16980dc83bd3SJan Kara oldest_jif = jiffies - 1699ba9aa839SWu Fengguang msecs_to_jiffies(dirty_expire_interval * 10); 17001bc36b64SJan Kara } else if (work->for_background) 17010dc83bd3SJan Kara oldest_jif = jiffies; 1702028c2dd1SDave Chinner 17035634cc2aSTejun Heo trace_writeback_start(wb, work); 1704e8dfc305SWu Fengguang if (list_empty(&wb->b_io)) 1705ad4e38ddSCurt Wohlgemuth queue_io(wb, work); 170683ba7b07SChristoph Hellwig if (work->sb) 1707d46db3d5SWu Fengguang progress = writeback_sb_inodes(work->sb, wb, work); 1708edadfb10SChristoph Hellwig else 1709d46db3d5SWu Fengguang progress = __writeback_inodes_wb(wb, work); 17105634cc2aSTejun Heo trace_writeback_written(wb, work); 1711028c2dd1SDave Chinner 1712e98be2d5SWu Fengguang wb_update_bandwidth(wb, wb_start); 171303ba3782SJens Axboe 171403ba3782SJens Axboe /* 171571fd05a8SJens Axboe * Did we write something? Try for more 1716e6fb6da2SWu Fengguang * 1717e6fb6da2SWu Fengguang * Dirty inodes are moved to b_io for writeback in batches. 1718e6fb6da2SWu Fengguang * The completion of the current batch does not necessarily 1719e6fb6da2SWu Fengguang * mean the overall work is done. So we keep looping as long 1720e6fb6da2SWu Fengguang * as made some progress on cleaning pages or inodes. 172171fd05a8SJens Axboe */ 1722d46db3d5SWu Fengguang if (progress) 172303ba3782SJens Axboe continue; 1724a5989bdcSJan Kara /* 1725e6fb6da2SWu Fengguang * No more inodes for IO, bail 1726a5989bdcSJan Kara */ 1727b7a2441fSWu Fengguang if (list_empty(&wb->b_more_io)) 172803ba3782SJens Axboe break; 172903ba3782SJens Axboe /* 17308010c3b6SJens Axboe * Nothing written. Wait for some inode to 17318010c3b6SJens Axboe * become available for writeback. Otherwise 17328010c3b6SJens Axboe * we'll just busyloop. 173303ba3782SJens Axboe */ 173403ba3782SJens Axboe if (!list_empty(&wb->b_more_io)) { 17355634cc2aSTejun Heo trace_writeback_wait(wb, work); 173603ba3782SJens Axboe inode = wb_inode(wb->b_more_io.prev); 1737250df6edSDave Chinner spin_lock(&inode->i_lock); 1738f0d07b7fSJan Kara spin_unlock(&wb->list_lock); 1739169ebd90SJan Kara /* This function drops i_lock... */ 1740169ebd90SJan Kara inode_sleep_on_writeback(inode); 1741f0d07b7fSJan Kara spin_lock(&wb->list_lock); 174203ba3782SJens Axboe } 174303ba3782SJens Axboe } 1744e8dfc305SWu Fengguang spin_unlock(&wb->list_lock); 1745505a666eSLinus Torvalds blk_finish_plug(&plug); 174603ba3782SJens Axboe 1747d46db3d5SWu Fengguang return nr_pages - work->nr_pages; 174803ba3782SJens Axboe } 174903ba3782SJens Axboe 175003ba3782SJens Axboe /* 175183ba7b07SChristoph Hellwig * Return the next wb_writeback_work struct that hasn't been processed yet. 175203ba3782SJens Axboe */ 1753f0054bb1STejun Heo static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb) 175403ba3782SJens Axboe { 175583ba7b07SChristoph Hellwig struct wb_writeback_work *work = NULL; 175603ba3782SJens Axboe 1757f0054bb1STejun Heo spin_lock_bh(&wb->work_lock); 1758f0054bb1STejun Heo if (!list_empty(&wb->work_list)) { 1759f0054bb1STejun Heo work = list_entry(wb->work_list.next, 176083ba7b07SChristoph Hellwig struct wb_writeback_work, list); 176183ba7b07SChristoph Hellwig list_del_init(&work->list); 176203ba3782SJens Axboe } 1763f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 176483ba7b07SChristoph Hellwig return work; 176503ba3782SJens Axboe } 176603ba3782SJens Axboe 1767cdf01dd5SLinus Torvalds /* 1768cdf01dd5SLinus Torvalds * Add in the number of potentially dirty inodes, because each inode 1769cdf01dd5SLinus Torvalds * write can dirty pagecache in the underlying blockdev. 1770cdf01dd5SLinus Torvalds */ 1771cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void) 1772cdf01dd5SLinus Torvalds { 1773cdf01dd5SLinus Torvalds return global_page_state(NR_FILE_DIRTY) + 1774cdf01dd5SLinus Torvalds global_page_state(NR_UNSTABLE_NFS) + 1775cdf01dd5SLinus Torvalds get_nr_dirty_inodes(); 1776cdf01dd5SLinus Torvalds } 1777cdf01dd5SLinus Torvalds 17786585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb) 17796585027aSJan Kara { 1780aa661bbeSTejun Heo if (wb_over_bg_thresh(wb)) { 17816585027aSJan Kara 17826585027aSJan Kara struct wb_writeback_work work = { 17836585027aSJan Kara .nr_pages = LONG_MAX, 17846585027aSJan Kara .sync_mode = WB_SYNC_NONE, 17856585027aSJan Kara .for_background = 1, 17866585027aSJan Kara .range_cyclic = 1, 17870e175a18SCurt Wohlgemuth .reason = WB_REASON_BACKGROUND, 17886585027aSJan Kara }; 17896585027aSJan Kara 17906585027aSJan Kara return wb_writeback(wb, &work); 17916585027aSJan Kara } 17926585027aSJan Kara 17936585027aSJan Kara return 0; 17946585027aSJan Kara } 17956585027aSJan Kara 179603ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb) 179703ba3782SJens Axboe { 179803ba3782SJens Axboe unsigned long expired; 179903ba3782SJens Axboe long nr_pages; 180003ba3782SJens Axboe 180169b62d01SJens Axboe /* 180269b62d01SJens Axboe * When set to zero, disable periodic writeback 180369b62d01SJens Axboe */ 180469b62d01SJens Axboe if (!dirty_writeback_interval) 180569b62d01SJens Axboe return 0; 180669b62d01SJens Axboe 180703ba3782SJens Axboe expired = wb->last_old_flush + 180803ba3782SJens Axboe msecs_to_jiffies(dirty_writeback_interval * 10); 180903ba3782SJens Axboe if (time_before(jiffies, expired)) 181003ba3782SJens Axboe return 0; 181103ba3782SJens Axboe 181203ba3782SJens Axboe wb->last_old_flush = jiffies; 1813cdf01dd5SLinus Torvalds nr_pages = get_nr_dirty_pages(); 181403ba3782SJens Axboe 1815c4a77a6cSJens Axboe if (nr_pages) { 181683ba7b07SChristoph Hellwig struct wb_writeback_work work = { 1817c4a77a6cSJens Axboe .nr_pages = nr_pages, 1818c4a77a6cSJens Axboe .sync_mode = WB_SYNC_NONE, 1819c4a77a6cSJens Axboe .for_kupdate = 1, 1820c4a77a6cSJens Axboe .range_cyclic = 1, 18210e175a18SCurt Wohlgemuth .reason = WB_REASON_PERIODIC, 1822c4a77a6cSJens Axboe }; 1823c4a77a6cSJens Axboe 182483ba7b07SChristoph Hellwig return wb_writeback(wb, &work); 1825c4a77a6cSJens Axboe } 182603ba3782SJens Axboe 182703ba3782SJens Axboe return 0; 182803ba3782SJens Axboe } 182903ba3782SJens Axboe 183003ba3782SJens Axboe /* 183103ba3782SJens Axboe * Retrieve work items and do the writeback they describe 183203ba3782SJens Axboe */ 183325d130baSWanpeng Li static long wb_do_writeback(struct bdi_writeback *wb) 183403ba3782SJens Axboe { 183583ba7b07SChristoph Hellwig struct wb_writeback_work *work; 1836c4a77a6cSJens Axboe long wrote = 0; 183703ba3782SJens Axboe 18384452226eSTejun Heo set_bit(WB_writeback_running, &wb->state); 1839f0054bb1STejun Heo while ((work = get_next_work_item(wb)) != NULL) { 1840cc395d7fSTejun Heo struct wb_completion *done = work->done; 184183ba7b07SChristoph Hellwig 18425634cc2aSTejun Heo trace_writeback_exec(wb, work); 1843455b2864SDave Chinner 184483ba7b07SChristoph Hellwig wrote += wb_writeback(wb, work); 184503ba3782SJens Axboe 18468a1270cdSTejun Heo if (work->auto_free) 184783ba7b07SChristoph Hellwig kfree(work); 1848cc395d7fSTejun Heo if (done && atomic_dec_and_test(&done->cnt)) 1849cc395d7fSTejun Heo wake_up_all(&wb->bdi->wb_waitq); 185003ba3782SJens Axboe } 185103ba3782SJens Axboe 185203ba3782SJens Axboe /* 185303ba3782SJens Axboe * Check for periodic writeback, kupdated() style 185403ba3782SJens Axboe */ 185503ba3782SJens Axboe wrote += wb_check_old_data_flush(wb); 18566585027aSJan Kara wrote += wb_check_background_flush(wb); 18574452226eSTejun Heo clear_bit(WB_writeback_running, &wb->state); 185803ba3782SJens Axboe 185903ba3782SJens Axboe return wrote; 186003ba3782SJens Axboe } 186103ba3782SJens Axboe 186203ba3782SJens Axboe /* 186303ba3782SJens Axboe * Handle writeback of dirty data for the device backed by this bdi. Also 1864839a8e86STejun Heo * reschedules periodically and does kupdated style flushing. 186503ba3782SJens Axboe */ 1866f0054bb1STejun Heo void wb_workfn(struct work_struct *work) 186703ba3782SJens Axboe { 1868839a8e86STejun Heo struct bdi_writeback *wb = container_of(to_delayed_work(work), 1869839a8e86STejun Heo struct bdi_writeback, dwork); 187003ba3782SJens Axboe long pages_written; 187103ba3782SJens Axboe 1872f0054bb1STejun Heo set_worker_desc("flush-%s", dev_name(wb->bdi->dev)); 1873766f9164SPeter Zijlstra current->flags |= PF_SWAPWRITE; 187403ba3782SJens Axboe 1875839a8e86STejun Heo if (likely(!current_is_workqueue_rescuer() || 18764452226eSTejun Heo !test_bit(WB_registered, &wb->state))) { 187703ba3782SJens Axboe /* 1878f0054bb1STejun Heo * The normal path. Keep writing back @wb until its 1879839a8e86STejun Heo * work_list is empty. Note that this path is also taken 1880f0054bb1STejun Heo * if @wb is shutting down even when we're running off the 1881839a8e86STejun Heo * rescuer as work_list needs to be drained. 188203ba3782SJens Axboe */ 1883839a8e86STejun Heo do { 188425d130baSWanpeng Li pages_written = wb_do_writeback(wb); 1885455b2864SDave Chinner trace_writeback_pages_written(pages_written); 1886f0054bb1STejun Heo } while (!list_empty(&wb->work_list)); 1887839a8e86STejun Heo } else { 1888253c34e9SArtem Bityutskiy /* 1889839a8e86STejun Heo * bdi_wq can't get enough workers and we're running off 1890839a8e86STejun Heo * the emergency worker. Don't hog it. Hopefully, 1024 is 1891839a8e86STejun Heo * enough for efficient IO. 1892253c34e9SArtem Bityutskiy */ 1893f0054bb1STejun Heo pages_written = writeback_inodes_wb(wb, 1024, 1894839a8e86STejun Heo WB_REASON_FORKER_THREAD); 1895839a8e86STejun Heo trace_writeback_pages_written(pages_written); 189603ba3782SJens Axboe } 189703ba3782SJens Axboe 1898f0054bb1STejun Heo if (!list_empty(&wb->work_list)) 18996ca738d6SDerek Basehore mod_delayed_work(bdi_wq, &wb->dwork, 0); 19006ca738d6SDerek Basehore else if (wb_has_dirty_io(wb) && dirty_writeback_interval) 1901f0054bb1STejun Heo wb_wakeup_delayed(wb); 1902455b2864SDave Chinner 1903839a8e86STejun Heo current->flags &= ~PF_SWAPWRITE; 190403ba3782SJens Axboe } 190503ba3782SJens Axboe 190603ba3782SJens Axboe /* 190703ba3782SJens Axboe * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back 190803ba3782SJens Axboe * the whole world. 190903ba3782SJens Axboe */ 19100e175a18SCurt Wohlgemuth void wakeup_flusher_threads(long nr_pages, enum wb_reason reason) 191103ba3782SJens Axboe { 1912b8c2f347SChristoph Hellwig struct backing_dev_info *bdi; 1913b8c2f347SChristoph Hellwig 191447df3ddeSJan Kara if (!nr_pages) 191547df3ddeSJan Kara nr_pages = get_nr_dirty_pages(); 1916b8c2f347SChristoph Hellwig 1917b8c2f347SChristoph Hellwig rcu_read_lock(); 1918f2b65121STejun Heo list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 1919f2b65121STejun Heo struct bdi_writeback *wb; 1920f2b65121STejun Heo 1921f2b65121STejun Heo if (!bdi_has_dirty_io(bdi)) 1922f2b65121STejun Heo continue; 1923f2b65121STejun Heo 1924b817525aSTejun Heo list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node) 1925f2b65121STejun Heo wb_start_writeback(wb, wb_split_bdi_pages(wb, nr_pages), 1926f2b65121STejun Heo false, reason); 1927f2b65121STejun Heo } 1928b8c2f347SChristoph Hellwig rcu_read_unlock(); 192903ba3782SJens Axboe } 193003ba3782SJens Axboe 1931a2f48706STheodore Ts'o /* 1932a2f48706STheodore Ts'o * Wake up bdi's periodically to make sure dirtytime inodes gets 1933a2f48706STheodore Ts'o * written back periodically. We deliberately do *not* check the 1934a2f48706STheodore Ts'o * b_dirtytime list in wb_has_dirty_io(), since this would cause the 1935a2f48706STheodore Ts'o * kernel to be constantly waking up once there are any dirtytime 1936a2f48706STheodore Ts'o * inodes on the system. So instead we define a separate delayed work 1937a2f48706STheodore Ts'o * function which gets called much more rarely. (By default, only 1938a2f48706STheodore Ts'o * once every 12 hours.) 1939a2f48706STheodore Ts'o * 1940a2f48706STheodore Ts'o * If there is any other write activity going on in the file system, 1941a2f48706STheodore Ts'o * this function won't be necessary. But if the only thing that has 1942a2f48706STheodore Ts'o * happened on the file system is a dirtytime inode caused by an atime 1943a2f48706STheodore Ts'o * update, we need this infrastructure below to make sure that inode 1944a2f48706STheodore Ts'o * eventually gets pushed out to disk. 1945a2f48706STheodore Ts'o */ 1946a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w); 1947a2f48706STheodore Ts'o static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback); 1948a2f48706STheodore Ts'o 1949a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w) 1950a2f48706STheodore Ts'o { 1951a2f48706STheodore Ts'o struct backing_dev_info *bdi; 1952a2f48706STheodore Ts'o 1953a2f48706STheodore Ts'o rcu_read_lock(); 1954a2f48706STheodore Ts'o list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 1955001fe6f6STejun Heo struct bdi_writeback *wb; 1956001fe6f6STejun Heo 1957b817525aSTejun Heo list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node) 19586fdf860fSTejun Heo if (!list_empty(&wb->b_dirty_time)) 19596fdf860fSTejun Heo wb_wakeup(wb); 1960a2f48706STheodore Ts'o } 1961a2f48706STheodore Ts'o rcu_read_unlock(); 1962a2f48706STheodore Ts'o schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ); 1963a2f48706STheodore Ts'o } 1964a2f48706STheodore Ts'o 1965a2f48706STheodore Ts'o static int __init start_dirtytime_writeback(void) 1966a2f48706STheodore Ts'o { 1967a2f48706STheodore Ts'o schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ); 1968a2f48706STheodore Ts'o return 0; 1969a2f48706STheodore Ts'o } 1970a2f48706STheodore Ts'o __initcall(start_dirtytime_writeback); 1971a2f48706STheodore Ts'o 19721efff914STheodore Ts'o int dirtytime_interval_handler(struct ctl_table *table, int write, 19731efff914STheodore Ts'o void __user *buffer, size_t *lenp, loff_t *ppos) 19741efff914STheodore Ts'o { 19751efff914STheodore Ts'o int ret; 19761efff914STheodore Ts'o 19771efff914STheodore Ts'o ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 19781efff914STheodore Ts'o if (ret == 0 && write) 19791efff914STheodore Ts'o mod_delayed_work(system_wq, &dirtytime_work, 0); 19801efff914STheodore Ts'o return ret; 19811efff914STheodore Ts'o } 19821efff914STheodore Ts'o 198303ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode) 198403ba3782SJens Axboe { 198503ba3782SJens Axboe if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { 198603ba3782SJens Axboe struct dentry *dentry; 198703ba3782SJens Axboe const char *name = "?"; 198803ba3782SJens Axboe 198903ba3782SJens Axboe dentry = d_find_alias(inode); 199003ba3782SJens Axboe if (dentry) { 199103ba3782SJens Axboe spin_lock(&dentry->d_lock); 199203ba3782SJens Axboe name = (const char *) dentry->d_name.name; 199303ba3782SJens Axboe } 199403ba3782SJens Axboe printk(KERN_DEBUG 199503ba3782SJens Axboe "%s(%d): dirtied inode %lu (%s) on %s\n", 199603ba3782SJens Axboe current->comm, task_pid_nr(current), inode->i_ino, 199703ba3782SJens Axboe name, inode->i_sb->s_id); 199803ba3782SJens Axboe if (dentry) { 199903ba3782SJens Axboe spin_unlock(&dentry->d_lock); 200003ba3782SJens Axboe dput(dentry); 200103ba3782SJens Axboe } 200203ba3782SJens Axboe } 200303ba3782SJens Axboe } 200403ba3782SJens Axboe 200503ba3782SJens Axboe /** 200603ba3782SJens Axboe * __mark_inode_dirty - internal function 200703ba3782SJens Axboe * @inode: inode to mark 200803ba3782SJens Axboe * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) 200903ba3782SJens Axboe * Mark an inode as dirty. Callers should use mark_inode_dirty or 201003ba3782SJens Axboe * mark_inode_dirty_sync. 201103ba3782SJens Axboe * 201203ba3782SJens Axboe * Put the inode on the super block's dirty list. 201303ba3782SJens Axboe * 201403ba3782SJens Axboe * CAREFUL! We mark it dirty unconditionally, but move it onto the 201503ba3782SJens Axboe * dirty list only if it is hashed or if it refers to a blockdev. 201603ba3782SJens Axboe * If it was not hashed, it will never be added to the dirty list 201703ba3782SJens Axboe * even if it is later hashed, as it will have been marked dirty already. 201803ba3782SJens Axboe * 201903ba3782SJens Axboe * In short, make sure you hash any inodes _before_ you start marking 202003ba3782SJens Axboe * them dirty. 202103ba3782SJens Axboe * 202203ba3782SJens Axboe * Note that for blockdevs, inode->dirtied_when represents the dirtying time of 202303ba3782SJens Axboe * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of 202403ba3782SJens Axboe * the kernel-internal blockdev inode represents the dirtying time of the 202503ba3782SJens Axboe * blockdev's pages. This is why for I_DIRTY_PAGES we always use 202603ba3782SJens Axboe * page->mapping->host, so the page-dirtying time is recorded in the internal 202703ba3782SJens Axboe * blockdev inode. 202803ba3782SJens Axboe */ 202903ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags) 203003ba3782SJens Axboe { 2031dbce03b9SRandy Dunlap #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC) 203203ba3782SJens Axboe struct super_block *sb = inode->i_sb; 20330ae45f63STheodore Ts'o int dirtytime; 20340ae45f63STheodore Ts'o 20350ae45f63STheodore Ts'o trace_writeback_mark_inode_dirty(inode, flags); 203603ba3782SJens Axboe 203703ba3782SJens Axboe /* 203803ba3782SJens Axboe * Don't do this for I_DIRTY_PAGES - that doesn't actually 203903ba3782SJens Axboe * dirty the inode itself 204003ba3782SJens Axboe */ 20410ae45f63STheodore Ts'o if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_TIME)) { 20429fb0a7daSTejun Heo trace_writeback_dirty_inode_start(inode, flags); 20439fb0a7daSTejun Heo 204403ba3782SJens Axboe if (sb->s_op->dirty_inode) 2045aa385729SChristoph Hellwig sb->s_op->dirty_inode(inode, flags); 20469fb0a7daSTejun Heo 20479fb0a7daSTejun Heo trace_writeback_dirty_inode(inode, flags); 204803ba3782SJens Axboe } 20490ae45f63STheodore Ts'o if (flags & I_DIRTY_INODE) 20500ae45f63STheodore Ts'o flags &= ~I_DIRTY_TIME; 20510ae45f63STheodore Ts'o dirtytime = flags & I_DIRTY_TIME; 205203ba3782SJens Axboe 205303ba3782SJens Axboe /* 20549c6ac78eSTejun Heo * Paired with smp_mb() in __writeback_single_inode() for the 20559c6ac78eSTejun Heo * following lockless i_state test. See there for details. 205603ba3782SJens Axboe */ 205703ba3782SJens Axboe smp_mb(); 205803ba3782SJens Axboe 20590ae45f63STheodore Ts'o if (((inode->i_state & flags) == flags) || 20600ae45f63STheodore Ts'o (dirtytime && (inode->i_state & I_DIRTY_INODE))) 206103ba3782SJens Axboe return; 206203ba3782SJens Axboe 206303ba3782SJens Axboe if (unlikely(block_dump)) 206403ba3782SJens Axboe block_dump___mark_inode_dirty(inode); 206503ba3782SJens Axboe 2066250df6edSDave Chinner spin_lock(&inode->i_lock); 20670ae45f63STheodore Ts'o if (dirtytime && (inode->i_state & I_DIRTY_INODE)) 20680ae45f63STheodore Ts'o goto out_unlock_inode; 206903ba3782SJens Axboe if ((inode->i_state & flags) != flags) { 207003ba3782SJens Axboe const int was_dirty = inode->i_state & I_DIRTY; 207103ba3782SJens Axboe 207252ebea74STejun Heo inode_attach_wb(inode, NULL); 207352ebea74STejun Heo 20740ae45f63STheodore Ts'o if (flags & I_DIRTY_INODE) 20750ae45f63STheodore Ts'o inode->i_state &= ~I_DIRTY_TIME; 207603ba3782SJens Axboe inode->i_state |= flags; 207703ba3782SJens Axboe 207803ba3782SJens Axboe /* 207903ba3782SJens Axboe * If the inode is being synced, just update its dirty state. 208003ba3782SJens Axboe * The unlocker will place the inode on the appropriate 208103ba3782SJens Axboe * superblock list, based upon its state. 208203ba3782SJens Axboe */ 208303ba3782SJens Axboe if (inode->i_state & I_SYNC) 2084250df6edSDave Chinner goto out_unlock_inode; 208503ba3782SJens Axboe 208603ba3782SJens Axboe /* 208703ba3782SJens Axboe * Only add valid (hashed) inodes to the superblock's 208803ba3782SJens Axboe * dirty list. Add blockdev inodes as well. 208903ba3782SJens Axboe */ 209003ba3782SJens Axboe if (!S_ISBLK(inode->i_mode)) { 20911d3382cbSAl Viro if (inode_unhashed(inode)) 2092250df6edSDave Chinner goto out_unlock_inode; 209303ba3782SJens Axboe } 2094a4ffdde6SAl Viro if (inode->i_state & I_FREEING) 2095250df6edSDave Chinner goto out_unlock_inode; 209603ba3782SJens Axboe 209703ba3782SJens Axboe /* 209803ba3782SJens Axboe * If the inode was already on b_dirty/b_io/b_more_io, don't 209903ba3782SJens Axboe * reposition it (that would break b_dirty time-ordering). 210003ba3782SJens Axboe */ 210103ba3782SJens Axboe if (!was_dirty) { 210287e1d789STejun Heo struct bdi_writeback *wb; 2103d6c10f1fSTejun Heo struct list_head *dirty_list; 2104a66979abSDave Chinner bool wakeup_bdi = false; 2105500b067cSJens Axboe 210687e1d789STejun Heo wb = locked_inode_to_wb_and_lock_list(inode); 2107253c34e9SArtem Bityutskiy 21080747259dSTejun Heo WARN(bdi_cap_writeback_dirty(wb->bdi) && 21090747259dSTejun Heo !test_bit(WB_registered, &wb->state), 21100747259dSTejun Heo "bdi-%s not registered\n", wb->bdi->name); 211103ba3782SJens Axboe 211203ba3782SJens Axboe inode->dirtied_when = jiffies; 2113a2f48706STheodore Ts'o if (dirtytime) 2114a2f48706STheodore Ts'o inode->dirtied_time_when = jiffies; 2115d6c10f1fSTejun Heo 2116a2f48706STheodore Ts'o if (inode->i_state & (I_DIRTY_INODE | I_DIRTY_PAGES)) 21170747259dSTejun Heo dirty_list = &wb->b_dirty; 2118a2f48706STheodore Ts'o else 21190747259dSTejun Heo dirty_list = &wb->b_dirty_time; 2120d6c10f1fSTejun Heo 2121c7f54084SDave Chinner wakeup_bdi = inode_io_list_move_locked(inode, wb, 2122d6c10f1fSTejun Heo dirty_list); 2123d6c10f1fSTejun Heo 21240747259dSTejun Heo spin_unlock(&wb->list_lock); 21250ae45f63STheodore Ts'o trace_writeback_dirty_inode_enqueue(inode); 2126253c34e9SArtem Bityutskiy 2127d6c10f1fSTejun Heo /* 2128d6c10f1fSTejun Heo * If this is the first dirty inode for this bdi, 2129d6c10f1fSTejun Heo * we have to wake-up the corresponding bdi thread 2130d6c10f1fSTejun Heo * to make sure background write-back happens 2131d6c10f1fSTejun Heo * later. 2132d6c10f1fSTejun Heo */ 21330747259dSTejun Heo if (bdi_cap_writeback_dirty(wb->bdi) && wakeup_bdi) 21340747259dSTejun Heo wb_wakeup_delayed(wb); 2135a66979abSDave Chinner return; 2136a66979abSDave Chinner } 2137a66979abSDave Chinner } 2138a66979abSDave Chinner out_unlock_inode: 2139a66979abSDave Chinner spin_unlock(&inode->i_lock); 2140a66979abSDave Chinner 2141dbce03b9SRandy Dunlap #undef I_DIRTY_INODE 214203ba3782SJens Axboe } 214303ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty); 214403ba3782SJens Axboe 2145e97fedb9SDave Chinner /* 2146e97fedb9SDave Chinner * The @s_sync_lock is used to serialise concurrent sync operations 2147e97fedb9SDave Chinner * to avoid lock contention problems with concurrent wait_sb_inodes() calls. 2148e97fedb9SDave Chinner * Concurrent callers will block on the s_sync_lock rather than doing contending 2149e97fedb9SDave Chinner * walks. The queueing maintains sync(2) required behaviour as all the IO that 2150e97fedb9SDave Chinner * has been issued up to the time this function is enter is guaranteed to be 2151e97fedb9SDave Chinner * completed by the time we have gained the lock and waited for all IO that is 2152e97fedb9SDave Chinner * in progress regardless of the order callers are granted the lock. 2153e97fedb9SDave Chinner */ 2154b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb) 215566f3b8e2SJens Axboe { 215638f21977SNick Piggin struct inode *inode, *old_inode = NULL; 215738f21977SNick Piggin 215803ba3782SJens Axboe /* 215903ba3782SJens Axboe * We need to be protected against the filesystem going from 216003ba3782SJens Axboe * r/o to r/w or vice versa. 216103ba3782SJens Axboe */ 2162b6e51316SJens Axboe WARN_ON(!rwsem_is_locked(&sb->s_umount)); 216303ba3782SJens Axboe 2164e97fedb9SDave Chinner mutex_lock(&sb->s_sync_lock); 216574278da9SDave Chinner spin_lock(&sb->s_inode_list_lock); 216666f3b8e2SJens Axboe 216738f21977SNick Piggin /* 216838f21977SNick Piggin * Data integrity sync. Must wait for all pages under writeback, 216938f21977SNick Piggin * because there may have been pages dirtied before our sync 217038f21977SNick Piggin * call, but which had writeout started before we write it out. 217138f21977SNick Piggin * In which case, the inode may not be on the dirty list, but 217238f21977SNick Piggin * we still have to wait for that writeout. 217338f21977SNick Piggin */ 2174b6e51316SJens Axboe list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 2175250df6edSDave Chinner struct address_space *mapping = inode->i_mapping; 217638f21977SNick Piggin 2177250df6edSDave Chinner spin_lock(&inode->i_lock); 2178250df6edSDave Chinner if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) || 2179250df6edSDave Chinner (mapping->nrpages == 0)) { 2180250df6edSDave Chinner spin_unlock(&inode->i_lock); 218138f21977SNick Piggin continue; 2182250df6edSDave Chinner } 218338f21977SNick Piggin __iget(inode); 2184250df6edSDave Chinner spin_unlock(&inode->i_lock); 218574278da9SDave Chinner spin_unlock(&sb->s_inode_list_lock); 218655fa6091SDave Chinner 218738f21977SNick Piggin /* 218855fa6091SDave Chinner * We hold a reference to 'inode' so it couldn't have been 218955fa6091SDave Chinner * removed from s_inodes list while we dropped the 219074278da9SDave Chinner * s_inode_list_lock. We cannot iput the inode now as we can 219155fa6091SDave Chinner * be holding the last reference and we cannot iput it under 219274278da9SDave Chinner * s_inode_list_lock. So we keep the reference and iput it 219355fa6091SDave Chinner * later. 219438f21977SNick Piggin */ 219538f21977SNick Piggin iput(old_inode); 219638f21977SNick Piggin old_inode = inode; 219738f21977SNick Piggin 2198aa750fd7SJunichi Nomura /* 2199aa750fd7SJunichi Nomura * We keep the error status of individual mapping so that 2200aa750fd7SJunichi Nomura * applications can catch the writeback error using fsync(2). 2201aa750fd7SJunichi Nomura * See filemap_fdatawait_keep_errors() for details. 2202aa750fd7SJunichi Nomura */ 2203aa750fd7SJunichi Nomura filemap_fdatawait_keep_errors(mapping); 220438f21977SNick Piggin 220538f21977SNick Piggin cond_resched(); 220638f21977SNick Piggin 220774278da9SDave Chinner spin_lock(&sb->s_inode_list_lock); 220838f21977SNick Piggin } 220974278da9SDave Chinner spin_unlock(&sb->s_inode_list_lock); 221038f21977SNick Piggin iput(old_inode); 2211e97fedb9SDave Chinner mutex_unlock(&sb->s_sync_lock); 221266f3b8e2SJens Axboe } 22131da177e4SLinus Torvalds 2214f30a7d0cSTejun Heo static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr, 2215f30a7d0cSTejun Heo enum wb_reason reason, bool skip_if_busy) 22161da177e4SLinus Torvalds { 2217cc395d7fSTejun Heo DEFINE_WB_COMPLETION_ONSTACK(done); 221883ba7b07SChristoph Hellwig struct wb_writeback_work work = { 22193c4d7165SChristoph Hellwig .sb = sb, 22203c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_NONE, 22216e6938b6SWu Fengguang .tagged_writepages = 1, 222283ba7b07SChristoph Hellwig .done = &done, 22233259f8beSChris Mason .nr_pages = nr, 22240e175a18SCurt Wohlgemuth .reason = reason, 22253c4d7165SChristoph Hellwig }; 2226e7972912STejun Heo struct backing_dev_info *bdi = sb->s_bdi; 22270e3c9a22SJens Axboe 2228e7972912STejun Heo if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info) 22296eedc701SJan Kara return; 2230cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 2231f30a7d0cSTejun Heo 2232db125360STejun Heo bdi_split_work_to_wbs(sb->s_bdi, &work, skip_if_busy); 2233cc395d7fSTejun Heo wb_wait_for_completion(bdi, &done); 22341da177e4SLinus Torvalds } 2235f30a7d0cSTejun Heo 2236f30a7d0cSTejun Heo /** 2237f30a7d0cSTejun Heo * writeback_inodes_sb_nr - writeback dirty inodes from given super_block 2238f30a7d0cSTejun Heo * @sb: the superblock 2239f30a7d0cSTejun Heo * @nr: the number of pages to write 2240f30a7d0cSTejun Heo * @reason: reason why some writeback work initiated 2241f30a7d0cSTejun Heo * 2242f30a7d0cSTejun Heo * Start writeback on some inodes on this super_block. No guarantees are made 2243f30a7d0cSTejun Heo * on how many (if any) will be written, and this function does not wait 2244f30a7d0cSTejun Heo * for IO completion of submitted IO. 2245f30a7d0cSTejun Heo */ 2246f30a7d0cSTejun Heo void writeback_inodes_sb_nr(struct super_block *sb, 2247f30a7d0cSTejun Heo unsigned long nr, 2248f30a7d0cSTejun Heo enum wb_reason reason) 2249f30a7d0cSTejun Heo { 2250f30a7d0cSTejun Heo __writeback_inodes_sb_nr(sb, nr, reason, false); 2251f30a7d0cSTejun Heo } 22523259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr); 22533259f8beSChris Mason 22543259f8beSChris Mason /** 22553259f8beSChris Mason * writeback_inodes_sb - writeback dirty inodes from given super_block 22563259f8beSChris Mason * @sb: the superblock 2257786228abSMarcos Paulo de Souza * @reason: reason why some writeback work was initiated 22583259f8beSChris Mason * 22593259f8beSChris Mason * Start writeback on some inodes on this super_block. No guarantees are made 22603259f8beSChris Mason * on how many (if any) will be written, and this function does not wait 22613259f8beSChris Mason * for IO completion of submitted IO. 22623259f8beSChris Mason */ 22630e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 22643259f8beSChris Mason { 22650e175a18SCurt Wohlgemuth return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); 22663259f8beSChris Mason } 2267d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb); 2268d8a8559cSJens Axboe 2269d8a8559cSJens Axboe /** 227010ee27a0SMiao Xie * try_to_writeback_inodes_sb_nr - try to start writeback if none underway 22713259f8beSChris Mason * @sb: the superblock 22723259f8beSChris Mason * @nr: the number of pages to write 227310ee27a0SMiao Xie * @reason: the reason of writeback 22743259f8beSChris Mason * 227510ee27a0SMiao Xie * Invoke writeback_inodes_sb_nr if no writeback is currently underway. 22763259f8beSChris Mason * Returns 1 if writeback was started, 0 if not. 22773259f8beSChris Mason */ 2278f30a7d0cSTejun Heo bool try_to_writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr, 22790e175a18SCurt Wohlgemuth enum wb_reason reason) 22803259f8beSChris Mason { 228110ee27a0SMiao Xie if (!down_read_trylock(&sb->s_umount)) 2282f30a7d0cSTejun Heo return false; 228310ee27a0SMiao Xie 2284f30a7d0cSTejun Heo __writeback_inodes_sb_nr(sb, nr, reason, true); 22853259f8beSChris Mason up_read(&sb->s_umount); 2286f30a7d0cSTejun Heo return true; 22873259f8beSChris Mason } 228810ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb_nr); 228910ee27a0SMiao Xie 229010ee27a0SMiao Xie /** 229110ee27a0SMiao Xie * try_to_writeback_inodes_sb - try to start writeback if none underway 229210ee27a0SMiao Xie * @sb: the superblock 229310ee27a0SMiao Xie * @reason: reason why some writeback work was initiated 229410ee27a0SMiao Xie * 229510ee27a0SMiao Xie * Implement by try_to_writeback_inodes_sb_nr() 229610ee27a0SMiao Xie * Returns 1 if writeback was started, 0 if not. 229710ee27a0SMiao Xie */ 2298f30a7d0cSTejun Heo bool try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 229910ee27a0SMiao Xie { 230010ee27a0SMiao Xie return try_to_writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); 230110ee27a0SMiao Xie } 230210ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb); 23033259f8beSChris Mason 23043259f8beSChris Mason /** 2305d8a8559cSJens Axboe * sync_inodes_sb - sync sb inode pages 2306d8a8559cSJens Axboe * @sb: the superblock 2307d8a8559cSJens Axboe * 2308d8a8559cSJens Axboe * This function writes and waits on any dirty inode belonging to this 23090dc83bd3SJan Kara * super_block. 2310d8a8559cSJens Axboe */ 23110dc83bd3SJan Kara void sync_inodes_sb(struct super_block *sb) 2312d8a8559cSJens Axboe { 2313cc395d7fSTejun Heo DEFINE_WB_COMPLETION_ONSTACK(done); 231483ba7b07SChristoph Hellwig struct wb_writeback_work work = { 23153c4d7165SChristoph Hellwig .sb = sb, 23163c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_ALL, 23173c4d7165SChristoph Hellwig .nr_pages = LONG_MAX, 23183c4d7165SChristoph Hellwig .range_cyclic = 0, 231983ba7b07SChristoph Hellwig .done = &done, 23200e175a18SCurt Wohlgemuth .reason = WB_REASON_SYNC, 23217747bd4bSDave Chinner .for_sync = 1, 23223c4d7165SChristoph Hellwig }; 2323e7972912STejun Heo struct backing_dev_info *bdi = sb->s_bdi; 23243c4d7165SChristoph Hellwig 2325006a0973STejun Heo /* 2326006a0973STejun Heo * Can't skip on !bdi_has_dirty() because we should wait for !dirty 2327006a0973STejun Heo * inodes under writeback and I_DIRTY_TIME inodes ignored by 2328006a0973STejun Heo * bdi_has_dirty() need to be written out too. 2329006a0973STejun Heo */ 2330006a0973STejun Heo if (bdi == &noop_backing_dev_info) 23316eedc701SJan Kara return; 2332cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 2333cf37e972SChristoph Hellwig 2334db125360STejun Heo bdi_split_work_to_wbs(bdi, &work, false); 2335cc395d7fSTejun Heo wb_wait_for_completion(bdi, &done); 233683ba7b07SChristoph Hellwig 2337b6e51316SJens Axboe wait_sb_inodes(sb); 2338d8a8559cSJens Axboe } 2339d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb); 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds /** 23421da177e4SLinus Torvalds * write_inode_now - write an inode to disk 23431da177e4SLinus Torvalds * @inode: inode to write to disk 23441da177e4SLinus Torvalds * @sync: whether the write should be synchronous or not 23451da177e4SLinus Torvalds * 23467f04c26dSAndrea Arcangeli * This function commits an inode to disk immediately if it is dirty. This is 23477f04c26dSAndrea Arcangeli * primarily needed by knfsd. 23487f04c26dSAndrea Arcangeli * 23497f04c26dSAndrea Arcangeli * The caller must either have a ref on the inode or must have set I_WILL_FREE. 23501da177e4SLinus Torvalds */ 23511da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync) 23521da177e4SLinus Torvalds { 23531da177e4SLinus Torvalds struct writeback_control wbc = { 23541da177e4SLinus Torvalds .nr_to_write = LONG_MAX, 235518914b18SMike Galbraith .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 2356111ebb6eSOGAWA Hirofumi .range_start = 0, 2357111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 23581da177e4SLinus Torvalds }; 23591da177e4SLinus Torvalds 23601da177e4SLinus Torvalds if (!mapping_cap_writeback_dirty(inode->i_mapping)) 236149364ce2SAndrew Morton wbc.nr_to_write = 0; 23621da177e4SLinus Torvalds 23631da177e4SLinus Torvalds might_sleep(); 2364aaf25593STejun Heo return writeback_single_inode(inode, &wbc); 23651da177e4SLinus Torvalds } 23661da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now); 23671da177e4SLinus Torvalds 23681da177e4SLinus Torvalds /** 23691da177e4SLinus Torvalds * sync_inode - write an inode and its pages to disk. 23701da177e4SLinus Torvalds * @inode: the inode to sync 23711da177e4SLinus Torvalds * @wbc: controls the writeback mode 23721da177e4SLinus Torvalds * 23731da177e4SLinus Torvalds * sync_inode() will write an inode and its pages to disk. It will also 23741da177e4SLinus Torvalds * correctly update the inode on its superblock's dirty inode lists and will 23751da177e4SLinus Torvalds * update inode->i_state. 23761da177e4SLinus Torvalds * 23771da177e4SLinus Torvalds * The caller must have a ref on the inode. 23781da177e4SLinus Torvalds */ 23791da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc) 23801da177e4SLinus Torvalds { 2381aaf25593STejun Heo return writeback_single_inode(inode, wbc); 23821da177e4SLinus Torvalds } 23831da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode); 2384c3765016SChristoph Hellwig 2385c3765016SChristoph Hellwig /** 2386c691b9d9SAndrew Morton * sync_inode_metadata - write an inode to disk 2387c3765016SChristoph Hellwig * @inode: the inode to sync 2388c3765016SChristoph Hellwig * @wait: wait for I/O to complete. 2389c3765016SChristoph Hellwig * 2390c691b9d9SAndrew Morton * Write an inode to disk and adjust its dirty state after completion. 2391c3765016SChristoph Hellwig * 2392c3765016SChristoph Hellwig * Note: only writes the actual inode, no associated data or other metadata. 2393c3765016SChristoph Hellwig */ 2394c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait) 2395c3765016SChristoph Hellwig { 2396c3765016SChristoph Hellwig struct writeback_control wbc = { 2397c3765016SChristoph Hellwig .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, 2398c3765016SChristoph Hellwig .nr_to_write = 0, /* metadata-only */ 2399c3765016SChristoph Hellwig }; 2400c3765016SChristoph Hellwig 2401c3765016SChristoph Hellwig return sync_inode(inode, &wbc); 2402c3765016SChristoph Hellwig } 2403c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata); 2404