1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * fs/fs-writeback.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 2002, Linus Torvalds. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Contains all the functions related to writing back and waiting 81da177e4SLinus Torvalds * upon dirty inodes against superblocks, and writing back dirty 91da177e4SLinus Torvalds * pages against inodes. ie: data writeback. Writeout of the 101da177e4SLinus Torvalds * inode itself is not handled here. 111da177e4SLinus Torvalds * 12e1f8e874SFrancois Cami * 10Apr2002 Andrew Morton 131da177e4SLinus Torvalds * Split out of fs/inode.c 141da177e4SLinus Torvalds * Additions for address_space-based writeback 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/kernel.h> 18630d9c47SPaul Gortmaker #include <linux/export.h> 191da177e4SLinus Torvalds #include <linux/spinlock.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 211da177e4SLinus Torvalds #include <linux/sched.h> 221da177e4SLinus Torvalds #include <linux/fs.h> 231da177e4SLinus Torvalds #include <linux/mm.h> 24bc31b86aSWu Fengguang #include <linux/pagemap.h> 2503ba3782SJens Axboe #include <linux/kthread.h> 261da177e4SLinus Torvalds #include <linux/writeback.h> 271da177e4SLinus Torvalds #include <linux/blkdev.h> 281da177e4SLinus Torvalds #include <linux/backing-dev.h> 29455b2864SDave Chinner #include <linux/tracepoint.h> 30719ea2fbSAl Viro #include <linux/device.h> 3121c6321fSTejun Heo #include <linux/memcontrol.h> 3207f3f05cSDavid Howells #include "internal.h" 331da177e4SLinus Torvalds 34d0bceac7SJens Axboe /* 35bc31b86aSWu Fengguang * 4MB minimal write chunk size 36bc31b86aSWu Fengguang */ 3709cbfeafSKirill A. Shutemov #define MIN_WRITEBACK_PAGES (4096UL >> (PAGE_SHIFT - 10)) 38bc31b86aSWu Fengguang 39bc31b86aSWu Fengguang /* 40c4a77a6cSJens Axboe * Passed into wb_writeback(), essentially a subset of writeback_control 41c4a77a6cSJens Axboe */ 4283ba7b07SChristoph Hellwig struct wb_writeback_work { 43c4a77a6cSJens Axboe long nr_pages; 44c4a77a6cSJens Axboe struct super_block *sb; 45c4a77a6cSJens Axboe enum writeback_sync_modes sync_mode; 466e6938b6SWu Fengguang unsigned int tagged_writepages:1; 4752957fe1SH Hartley Sweeten unsigned int for_kupdate:1; 4852957fe1SH Hartley Sweeten unsigned int range_cyclic:1; 4952957fe1SH Hartley Sweeten unsigned int for_background:1; 507747bd4bSDave Chinner unsigned int for_sync:1; /* sync(2) WB_SYNC_ALL writeback */ 51ac7b19a3STejun Heo unsigned int auto_free:1; /* free on completion */ 520e175a18SCurt Wohlgemuth enum wb_reason reason; /* why was writeback initiated? */ 53c4a77a6cSJens Axboe 548010c3b6SJens Axboe struct list_head list; /* pending work list */ 55cc395d7fSTejun Heo struct wb_completion *done; /* set if the caller waits */ 5603ba3782SJens Axboe }; 5703ba3782SJens Axboe 58a2f48706STheodore Ts'o /* 59a2f48706STheodore Ts'o * If an inode is constantly having its pages dirtied, but then the 60a2f48706STheodore Ts'o * updates stop dirtytime_expire_interval seconds in the past, it's 61a2f48706STheodore Ts'o * possible for the worst case time between when an inode has its 62a2f48706STheodore Ts'o * timestamps updated and when they finally get written out to be two 63a2f48706STheodore Ts'o * dirtytime_expire_intervals. We set the default to 12 hours (in 64a2f48706STheodore Ts'o * seconds), which means most of the time inodes will have their 65a2f48706STheodore Ts'o * timestamps written to disk after 12 hours, but in the worst case a 66a2f48706STheodore Ts'o * few inodes might not their timestamps updated for 24 hours. 67a2f48706STheodore Ts'o */ 68a2f48706STheodore Ts'o unsigned int dirtytime_expire_interval = 12 * 60 * 60; 69a2f48706STheodore Ts'o 707ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head) 717ccf19a8SNick Piggin { 72c7f54084SDave Chinner return list_entry(head, struct inode, i_io_list); 737ccf19a8SNick Piggin } 747ccf19a8SNick Piggin 7515eb77a0SWu Fengguang /* 7615eb77a0SWu Fengguang * Include the creation of the trace points after defining the 7715eb77a0SWu Fengguang * wb_writeback_work structure and inline functions so that the definition 7815eb77a0SWu Fengguang * remains local to this file. 7915eb77a0SWu Fengguang */ 8015eb77a0SWu Fengguang #define CREATE_TRACE_POINTS 8115eb77a0SWu Fengguang #include <trace/events/writeback.h> 8215eb77a0SWu Fengguang 83774016b2SSteven Whitehouse EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage); 84774016b2SSteven Whitehouse 85d6c10f1fSTejun Heo static bool wb_io_lists_populated(struct bdi_writeback *wb) 86d6c10f1fSTejun Heo { 87d6c10f1fSTejun Heo if (wb_has_dirty_io(wb)) { 88d6c10f1fSTejun Heo return false; 89d6c10f1fSTejun Heo } else { 90d6c10f1fSTejun Heo set_bit(WB_has_dirty_io, &wb->state); 9195a46c65STejun Heo WARN_ON_ONCE(!wb->avg_write_bandwidth); 92766a9d6eSTejun Heo atomic_long_add(wb->avg_write_bandwidth, 93766a9d6eSTejun Heo &wb->bdi->tot_write_bandwidth); 94d6c10f1fSTejun Heo return true; 95d6c10f1fSTejun Heo } 96d6c10f1fSTejun Heo } 97d6c10f1fSTejun Heo 98d6c10f1fSTejun Heo static void wb_io_lists_depopulated(struct bdi_writeback *wb) 99d6c10f1fSTejun Heo { 100d6c10f1fSTejun Heo if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) && 101766a9d6eSTejun Heo list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) { 102d6c10f1fSTejun Heo clear_bit(WB_has_dirty_io, &wb->state); 10395a46c65STejun Heo WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth, 10495a46c65STejun Heo &wb->bdi->tot_write_bandwidth) < 0); 105766a9d6eSTejun Heo } 106d6c10f1fSTejun Heo } 107d6c10f1fSTejun Heo 108d6c10f1fSTejun Heo /** 109c7f54084SDave Chinner * inode_io_list_move_locked - move an inode onto a bdi_writeback IO list 110d6c10f1fSTejun Heo * @inode: inode to be moved 111d6c10f1fSTejun Heo * @wb: target bdi_writeback 112bbbc3c1cSWang Long * @head: one of @wb->b_{dirty|io|more_io|dirty_time} 113d6c10f1fSTejun Heo * 114c7f54084SDave Chinner * Move @inode->i_io_list to @list of @wb and set %WB_has_dirty_io. 115d6c10f1fSTejun Heo * Returns %true if @inode is the first occupant of the !dirty_time IO 116d6c10f1fSTejun Heo * lists; otherwise, %false. 117d6c10f1fSTejun Heo */ 118c7f54084SDave Chinner static bool inode_io_list_move_locked(struct inode *inode, 119d6c10f1fSTejun Heo struct bdi_writeback *wb, 120d6c10f1fSTejun Heo struct list_head *head) 121d6c10f1fSTejun Heo { 122d6c10f1fSTejun Heo assert_spin_locked(&wb->list_lock); 123d6c10f1fSTejun Heo 124c7f54084SDave Chinner list_move(&inode->i_io_list, head); 125d6c10f1fSTejun Heo 126d6c10f1fSTejun Heo /* dirty_time doesn't count as dirty_io until expiration */ 127d6c10f1fSTejun Heo if (head != &wb->b_dirty_time) 128d6c10f1fSTejun Heo return wb_io_lists_populated(wb); 129d6c10f1fSTejun Heo 130d6c10f1fSTejun Heo wb_io_lists_depopulated(wb); 131d6c10f1fSTejun Heo return false; 132d6c10f1fSTejun Heo } 133d6c10f1fSTejun Heo 134f0054bb1STejun Heo static void wb_wakeup(struct bdi_writeback *wb) 1355acda9d1SJan Kara { 136f0054bb1STejun Heo spin_lock_bh(&wb->work_lock); 137f0054bb1STejun Heo if (test_bit(WB_registered, &wb->state)) 138f0054bb1STejun Heo mod_delayed_work(bdi_wq, &wb->dwork, 0); 139f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 1405acda9d1SJan Kara } 1415acda9d1SJan Kara 1424a3a485bSTahsin Erdogan static void finish_writeback_work(struct bdi_writeback *wb, 1434a3a485bSTahsin Erdogan struct wb_writeback_work *work) 1444a3a485bSTahsin Erdogan { 1454a3a485bSTahsin Erdogan struct wb_completion *done = work->done; 1464a3a485bSTahsin Erdogan 1474a3a485bSTahsin Erdogan if (work->auto_free) 1484a3a485bSTahsin Erdogan kfree(work); 1498e00c4e9STejun Heo if (done) { 1508e00c4e9STejun Heo wait_queue_head_t *waitq = done->waitq; 1518e00c4e9STejun Heo 1528e00c4e9STejun Heo /* @done can't be accessed after the following dec */ 1538e00c4e9STejun Heo if (atomic_dec_and_test(&done->cnt)) 1548e00c4e9STejun Heo wake_up_all(waitq); 1558e00c4e9STejun Heo } 1564a3a485bSTahsin Erdogan } 1574a3a485bSTahsin Erdogan 158f0054bb1STejun Heo static void wb_queue_work(struct bdi_writeback *wb, 1596585027aSJan Kara struct wb_writeback_work *work) 1606585027aSJan Kara { 1615634cc2aSTejun Heo trace_writeback_queue(wb, work); 1626585027aSJan Kara 163cc395d7fSTejun Heo if (work->done) 164cc395d7fSTejun Heo atomic_inc(&work->done->cnt); 1654a3a485bSTahsin Erdogan 1664a3a485bSTahsin Erdogan spin_lock_bh(&wb->work_lock); 1674a3a485bSTahsin Erdogan 1684a3a485bSTahsin Erdogan if (test_bit(WB_registered, &wb->state)) { 169f0054bb1STejun Heo list_add_tail(&work->list, &wb->work_list); 170f0054bb1STejun Heo mod_delayed_work(bdi_wq, &wb->dwork, 0); 1714a3a485bSTahsin Erdogan } else 1724a3a485bSTahsin Erdogan finish_writeback_work(wb, work); 1734a3a485bSTahsin Erdogan 174f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 17503ba3782SJens Axboe } 1761da177e4SLinus Torvalds 177cc395d7fSTejun Heo /** 178cc395d7fSTejun Heo * wb_wait_for_completion - wait for completion of bdi_writeback_works 179cc395d7fSTejun Heo * @done: target wb_completion 180cc395d7fSTejun Heo * 181cc395d7fSTejun Heo * Wait for one or more work items issued to @bdi with their ->done field 1825b9cce4cSTejun Heo * set to @done, which should have been initialized with 1835b9cce4cSTejun Heo * DEFINE_WB_COMPLETION(). This function returns after all such work items 1845b9cce4cSTejun Heo * are completed. Work items which are waited upon aren't freed 185cc395d7fSTejun Heo * automatically on completion. 186cc395d7fSTejun Heo */ 1875b9cce4cSTejun Heo void wb_wait_for_completion(struct wb_completion *done) 188cc395d7fSTejun Heo { 189cc395d7fSTejun Heo atomic_dec(&done->cnt); /* put down the initial count */ 1905b9cce4cSTejun Heo wait_event(*done->waitq, !atomic_read(&done->cnt)); 191cc395d7fSTejun Heo } 192cc395d7fSTejun Heo 193703c2708STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 194703c2708STejun Heo 19555a694dfSTejun Heo /* 19655a694dfSTejun Heo * Parameters for foreign inode detection, see wbc_detach_inode() to see 19755a694dfSTejun Heo * how they're used. 19855a694dfSTejun Heo * 19955a694dfSTejun Heo * These paramters are inherently heuristical as the detection target 20055a694dfSTejun Heo * itself is fuzzy. All we want to do is detaching an inode from the 20155a694dfSTejun Heo * current owner if it's being written to by some other cgroups too much. 20255a694dfSTejun Heo * 20355a694dfSTejun Heo * The current cgroup writeback is built on the assumption that multiple 20455a694dfSTejun Heo * cgroups writing to the same inode concurrently is very rare and a mode 20555a694dfSTejun Heo * of operation which isn't well supported. As such, the goal is not 20655a694dfSTejun Heo * taking too long when a different cgroup takes over an inode while 20755a694dfSTejun Heo * avoiding too aggressive flip-flops from occasional foreign writes. 20855a694dfSTejun Heo * 20955a694dfSTejun Heo * We record, very roughly, 2s worth of IO time history and if more than 21055a694dfSTejun Heo * half of that is foreign, trigger the switch. The recording is quantized 21155a694dfSTejun Heo * to 16 slots. To avoid tiny writes from swinging the decision too much, 21255a694dfSTejun Heo * writes smaller than 1/8 of avg size are ignored. 21355a694dfSTejun Heo */ 2142a814908STejun Heo #define WB_FRN_TIME_SHIFT 13 /* 1s = 2^13, upto 8 secs w/ 16bit */ 2152a814908STejun Heo #define WB_FRN_TIME_AVG_SHIFT 3 /* avg = avg * 7/8 + new * 1/8 */ 21655a694dfSTejun Heo #define WB_FRN_TIME_CUT_DIV 8 /* ignore rounds < avg / 8 */ 2172a814908STejun Heo #define WB_FRN_TIME_PERIOD (2 * (1 << WB_FRN_TIME_SHIFT)) /* 2s */ 2182a814908STejun Heo 2192a814908STejun Heo #define WB_FRN_HIST_SLOTS 16 /* inode->i_wb_frn_history is 16bit */ 2202a814908STejun Heo #define WB_FRN_HIST_UNIT (WB_FRN_TIME_PERIOD / WB_FRN_HIST_SLOTS) 2212a814908STejun Heo /* each slot's duration is 2s / 16 */ 2222a814908STejun Heo #define WB_FRN_HIST_THR_SLOTS (WB_FRN_HIST_SLOTS / 2) 2232a814908STejun Heo /* if foreign slots >= 8, switch */ 2242a814908STejun Heo #define WB_FRN_HIST_MAX_SLOTS (WB_FRN_HIST_THR_SLOTS / 2 + 1) 2252a814908STejun Heo /* one round can affect upto 5 slots */ 2266444f47eSTejun Heo #define WB_FRN_MAX_IN_FLIGHT 1024 /* don't queue too many concurrently */ 2272a814908STejun Heo 228a1a0e23eSTejun Heo static atomic_t isw_nr_in_flight = ATOMIC_INIT(0); 229a1a0e23eSTejun Heo static struct workqueue_struct *isw_wq; 230a1a0e23eSTejun Heo 23121c6321fSTejun Heo void __inode_attach_wb(struct inode *inode, struct page *page) 23221c6321fSTejun Heo { 23321c6321fSTejun Heo struct backing_dev_info *bdi = inode_to_bdi(inode); 23421c6321fSTejun Heo struct bdi_writeback *wb = NULL; 23521c6321fSTejun Heo 23621c6321fSTejun Heo if (inode_cgwb_enabled(inode)) { 23721c6321fSTejun Heo struct cgroup_subsys_state *memcg_css; 23821c6321fSTejun Heo 23921c6321fSTejun Heo if (page) { 24021c6321fSTejun Heo memcg_css = mem_cgroup_css_from_page(page); 24121c6321fSTejun Heo wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 24221c6321fSTejun Heo } else { 24321c6321fSTejun Heo /* must pin memcg_css, see wb_get_create() */ 24421c6321fSTejun Heo memcg_css = task_get_css(current, memory_cgrp_id); 24521c6321fSTejun Heo wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 24621c6321fSTejun Heo css_put(memcg_css); 24721c6321fSTejun Heo } 24821c6321fSTejun Heo } 24921c6321fSTejun Heo 25021c6321fSTejun Heo if (!wb) 25121c6321fSTejun Heo wb = &bdi->wb; 25221c6321fSTejun Heo 25321c6321fSTejun Heo /* 25421c6321fSTejun Heo * There may be multiple instances of this function racing to 25521c6321fSTejun Heo * update the same inode. Use cmpxchg() to tell the winner. 25621c6321fSTejun Heo */ 25721c6321fSTejun Heo if (unlikely(cmpxchg(&inode->i_wb, NULL, wb))) 25821c6321fSTejun Heo wb_put(wb); 25921c6321fSTejun Heo } 2609b0eb69bSTejun Heo EXPORT_SYMBOL_GPL(__inode_attach_wb); 26121c6321fSTejun Heo 262703c2708STejun Heo /** 263f3b6a6dfSRoman Gushchin * inode_cgwb_move_to_attached - put the inode onto wb->b_attached list 264f3b6a6dfSRoman Gushchin * @inode: inode of interest with i_lock held 265f3b6a6dfSRoman Gushchin * @wb: target bdi_writeback 266f3b6a6dfSRoman Gushchin * 267f3b6a6dfSRoman Gushchin * Remove the inode from wb's io lists and if necessarily put onto b_attached 268f3b6a6dfSRoman Gushchin * list. Only inodes attached to cgwb's are kept on this list. 269f3b6a6dfSRoman Gushchin */ 270f3b6a6dfSRoman Gushchin static void inode_cgwb_move_to_attached(struct inode *inode, 271f3b6a6dfSRoman Gushchin struct bdi_writeback *wb) 272f3b6a6dfSRoman Gushchin { 273f3b6a6dfSRoman Gushchin assert_spin_locked(&wb->list_lock); 274f3b6a6dfSRoman Gushchin assert_spin_locked(&inode->i_lock); 275f3b6a6dfSRoman Gushchin 276f3b6a6dfSRoman Gushchin inode->i_state &= ~I_SYNC_QUEUED; 277f3b6a6dfSRoman Gushchin if (wb != &wb->bdi->wb) 278f3b6a6dfSRoman Gushchin list_move(&inode->i_io_list, &wb->b_attached); 279f3b6a6dfSRoman Gushchin else 280f3b6a6dfSRoman Gushchin list_del_init(&inode->i_io_list); 281f3b6a6dfSRoman Gushchin wb_io_lists_depopulated(wb); 282f3b6a6dfSRoman Gushchin } 283f3b6a6dfSRoman Gushchin 284f3b6a6dfSRoman Gushchin /** 28587e1d789STejun Heo * locked_inode_to_wb_and_lock_list - determine a locked inode's wb and lock it 28687e1d789STejun Heo * @inode: inode of interest with i_lock held 28787e1d789STejun Heo * 28887e1d789STejun Heo * Returns @inode's wb with its list_lock held. @inode->i_lock must be 28987e1d789STejun Heo * held on entry and is released on return. The returned wb is guaranteed 29087e1d789STejun Heo * to stay @inode's associated wb until its list_lock is released. 29187e1d789STejun Heo */ 29287e1d789STejun Heo static struct bdi_writeback * 29387e1d789STejun Heo locked_inode_to_wb_and_lock_list(struct inode *inode) 29487e1d789STejun Heo __releases(&inode->i_lock) 29587e1d789STejun Heo __acquires(&wb->list_lock) 29687e1d789STejun Heo { 29787e1d789STejun Heo while (true) { 29887e1d789STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 29987e1d789STejun Heo 30087e1d789STejun Heo /* 30187e1d789STejun Heo * inode_to_wb() association is protected by both 30287e1d789STejun Heo * @inode->i_lock and @wb->list_lock but list_lock nests 30387e1d789STejun Heo * outside i_lock. Drop i_lock and verify that the 30487e1d789STejun Heo * association hasn't changed after acquiring list_lock. 30587e1d789STejun Heo */ 30687e1d789STejun Heo wb_get(wb); 30787e1d789STejun Heo spin_unlock(&inode->i_lock); 30887e1d789STejun Heo spin_lock(&wb->list_lock); 30987e1d789STejun Heo 310aaa2cacfSTejun Heo /* i_wb may have changed inbetween, can't use inode_to_wb() */ 311614a4e37STejun Heo if (likely(wb == inode->i_wb)) { 312614a4e37STejun Heo wb_put(wb); /* @inode already has ref */ 313614a4e37STejun Heo return wb; 314614a4e37STejun Heo } 31587e1d789STejun Heo 31687e1d789STejun Heo spin_unlock(&wb->list_lock); 317614a4e37STejun Heo wb_put(wb); 31887e1d789STejun Heo cpu_relax(); 31987e1d789STejun Heo spin_lock(&inode->i_lock); 32087e1d789STejun Heo } 32187e1d789STejun Heo } 32287e1d789STejun Heo 32387e1d789STejun Heo /** 32487e1d789STejun Heo * inode_to_wb_and_lock_list - determine an inode's wb and lock it 32587e1d789STejun Heo * @inode: inode of interest 32687e1d789STejun Heo * 32787e1d789STejun Heo * Same as locked_inode_to_wb_and_lock_list() but @inode->i_lock isn't held 32887e1d789STejun Heo * on entry. 32987e1d789STejun Heo */ 33087e1d789STejun Heo static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode) 33187e1d789STejun Heo __acquires(&wb->list_lock) 33287e1d789STejun Heo { 33387e1d789STejun Heo spin_lock(&inode->i_lock); 33487e1d789STejun Heo return locked_inode_to_wb_and_lock_list(inode); 33587e1d789STejun Heo } 33687e1d789STejun Heo 337682aa8e1STejun Heo struct inode_switch_wbs_context { 33829264d92SRoman Gushchin struct rcu_work work; 339*f5fbe6b7SRoman Gushchin 340*f5fbe6b7SRoman Gushchin /* 341*f5fbe6b7SRoman Gushchin * Multiple inodes can be switched at once. The switching procedure 342*f5fbe6b7SRoman Gushchin * consists of two parts, separated by a RCU grace period. To make 343*f5fbe6b7SRoman Gushchin * sure that the second part is executed for each inode gone through 344*f5fbe6b7SRoman Gushchin * the first part, all inode pointers are placed into a NULL-terminated 345*f5fbe6b7SRoman Gushchin * array embedded into struct inode_switch_wbs_context. Otherwise 346*f5fbe6b7SRoman Gushchin * an inode could be left in a non-consistent state. 347*f5fbe6b7SRoman Gushchin */ 348*f5fbe6b7SRoman Gushchin struct bdi_writeback *new_wb; 349*f5fbe6b7SRoman Gushchin struct inode *inodes[]; 350682aa8e1STejun Heo }; 351682aa8e1STejun Heo 3527fc5854fSTejun Heo static void bdi_down_write_wb_switch_rwsem(struct backing_dev_info *bdi) 3537fc5854fSTejun Heo { 3547fc5854fSTejun Heo down_write(&bdi->wb_switch_rwsem); 3557fc5854fSTejun Heo } 3567fc5854fSTejun Heo 3577fc5854fSTejun Heo static void bdi_up_write_wb_switch_rwsem(struct backing_dev_info *bdi) 3587fc5854fSTejun Heo { 3597fc5854fSTejun Heo up_write(&bdi->wb_switch_rwsem); 3607fc5854fSTejun Heo } 3617fc5854fSTejun Heo 362*f5fbe6b7SRoman Gushchin static bool inode_do_switch_wbs(struct inode *inode, 363*f5fbe6b7SRoman Gushchin struct bdi_writeback *old_wb, 36472d4512eSRoman Gushchin struct bdi_writeback *new_wb) 365682aa8e1STejun Heo { 366d10c8095STejun Heo struct address_space *mapping = inode->i_mapping; 36704edf02cSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, 0); 36804edf02cSMatthew Wilcox struct page *page; 369d10c8095STejun Heo bool switched = false; 370682aa8e1STejun Heo 371682aa8e1STejun Heo spin_lock(&inode->i_lock); 372b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 373682aa8e1STejun Heo 374d10c8095STejun Heo /* 3754ade5867SRoman Gushchin * Once I_FREEING or I_WILL_FREE are visible under i_lock, the eviction 3764ade5867SRoman Gushchin * path owns the inode and we shouldn't modify ->i_io_list. 377d10c8095STejun Heo */ 3784ade5867SRoman Gushchin if (unlikely(inode->i_state & (I_FREEING | I_WILL_FREE))) 379d10c8095STejun Heo goto skip_switch; 380d10c8095STejun Heo 3813a8e9ac8STejun Heo trace_inode_switch_wbs(inode, old_wb, new_wb); 3823a8e9ac8STejun Heo 383d10c8095STejun Heo /* 384d10c8095STejun Heo * Count and transfer stats. Note that PAGECACHE_TAG_DIRTY points 385d10c8095STejun Heo * to possibly dirty pages while PAGECACHE_TAG_WRITEBACK points to 386b93b0163SMatthew Wilcox * pages actually under writeback. 387d10c8095STejun Heo */ 38804edf02cSMatthew Wilcox xas_for_each_marked(&xas, page, ULONG_MAX, PAGECACHE_TAG_DIRTY) { 38904edf02cSMatthew Wilcox if (PageDirty(page)) { 3903e8f399dSNikolay Borisov dec_wb_stat(old_wb, WB_RECLAIMABLE); 3913e8f399dSNikolay Borisov inc_wb_stat(new_wb, WB_RECLAIMABLE); 392d10c8095STejun Heo } 393d10c8095STejun Heo } 394d10c8095STejun Heo 39504edf02cSMatthew Wilcox xas_set(&xas, 0); 39604edf02cSMatthew Wilcox xas_for_each_marked(&xas, page, ULONG_MAX, PAGECACHE_TAG_WRITEBACK) { 397d10c8095STejun Heo WARN_ON_ONCE(!PageWriteback(page)); 3983e8f399dSNikolay Borisov dec_wb_stat(old_wb, WB_WRITEBACK); 3993e8f399dSNikolay Borisov inc_wb_stat(new_wb, WB_WRITEBACK); 400d10c8095STejun Heo } 401d10c8095STejun Heo 402d10c8095STejun Heo wb_get(new_wb); 403d10c8095STejun Heo 404d10c8095STejun Heo /* 405f3b6a6dfSRoman Gushchin * Transfer to @new_wb's IO list if necessary. If the @inode is dirty, 406f3b6a6dfSRoman Gushchin * the specific list @inode was on is ignored and the @inode is put on 407f3b6a6dfSRoman Gushchin * ->b_dirty which is always correct including from ->b_dirty_time. 408f3b6a6dfSRoman Gushchin * The transfer preserves @inode->dirtied_when ordering. If the @inode 409f3b6a6dfSRoman Gushchin * was clean, it means it was on the b_attached list, so move it onto 410f3b6a6dfSRoman Gushchin * the b_attached list of @new_wb. 411d10c8095STejun Heo */ 412c7f54084SDave Chinner if (!list_empty(&inode->i_io_list)) { 413f3b6a6dfSRoman Gushchin inode->i_wb = new_wb; 414f3b6a6dfSRoman Gushchin 415f3b6a6dfSRoman Gushchin if (inode->i_state & I_DIRTY_ALL) { 416d10c8095STejun Heo struct inode *pos; 417d10c8095STejun Heo 418c7f54084SDave Chinner list_for_each_entry(pos, &new_wb->b_dirty, i_io_list) 419d10c8095STejun Heo if (time_after_eq(inode->dirtied_when, 420d10c8095STejun Heo pos->dirtied_when)) 421d10c8095STejun Heo break; 422f3b6a6dfSRoman Gushchin inode_io_list_move_locked(inode, new_wb, 423f3b6a6dfSRoman Gushchin pos->i_io_list.prev); 424f3b6a6dfSRoman Gushchin } else { 425f3b6a6dfSRoman Gushchin inode_cgwb_move_to_attached(inode, new_wb); 426f3b6a6dfSRoman Gushchin } 427d10c8095STejun Heo } else { 428d10c8095STejun Heo inode->i_wb = new_wb; 429d10c8095STejun Heo } 430d10c8095STejun Heo 431d10c8095STejun Heo /* ->i_wb_frn updates may race wbc_detach_inode() but doesn't matter */ 432682aa8e1STejun Heo inode->i_wb_frn_winner = 0; 433682aa8e1STejun Heo inode->i_wb_frn_avg_time = 0; 434682aa8e1STejun Heo inode->i_wb_frn_history = 0; 435d10c8095STejun Heo switched = true; 436d10c8095STejun Heo skip_switch: 437682aa8e1STejun Heo /* 438682aa8e1STejun Heo * Paired with load_acquire in unlocked_inode_to_wb_begin() and 439682aa8e1STejun Heo * ensures that the new wb is visible if they see !I_WB_SWITCH. 440682aa8e1STejun Heo */ 441682aa8e1STejun Heo smp_store_release(&inode->i_state, inode->i_state & ~I_WB_SWITCH); 442682aa8e1STejun Heo 443b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 444682aa8e1STejun Heo spin_unlock(&inode->i_lock); 445d10c8095STejun Heo 446*f5fbe6b7SRoman Gushchin return switched; 44772d4512eSRoman Gushchin } 448682aa8e1STejun Heo 44972d4512eSRoman Gushchin static void inode_switch_wbs_work_fn(struct work_struct *work) 45072d4512eSRoman Gushchin { 45172d4512eSRoman Gushchin struct inode_switch_wbs_context *isw = 45272d4512eSRoman Gushchin container_of(to_rcu_work(work), struct inode_switch_wbs_context, work); 453*f5fbe6b7SRoman Gushchin struct backing_dev_info *bdi = inode_to_bdi(isw->inodes[0]); 454*f5fbe6b7SRoman Gushchin struct bdi_writeback *old_wb = isw->inodes[0]->i_wb; 455*f5fbe6b7SRoman Gushchin struct bdi_writeback *new_wb = isw->new_wb; 456*f5fbe6b7SRoman Gushchin unsigned long nr_switched = 0; 457*f5fbe6b7SRoman Gushchin struct inode **inodep; 45872d4512eSRoman Gushchin 459*f5fbe6b7SRoman Gushchin /* 460*f5fbe6b7SRoman Gushchin * If @inode switches cgwb membership while sync_inodes_sb() is 461*f5fbe6b7SRoman Gushchin * being issued, sync_inodes_sb() might miss it. Synchronize. 462*f5fbe6b7SRoman Gushchin */ 463*f5fbe6b7SRoman Gushchin down_read(&bdi->wb_switch_rwsem); 464*f5fbe6b7SRoman Gushchin 465*f5fbe6b7SRoman Gushchin /* 466*f5fbe6b7SRoman Gushchin * By the time control reaches here, RCU grace period has passed 467*f5fbe6b7SRoman Gushchin * since I_WB_SWITCH assertion and all wb stat update transactions 468*f5fbe6b7SRoman Gushchin * between unlocked_inode_to_wb_begin/end() are guaranteed to be 469*f5fbe6b7SRoman Gushchin * synchronizing against the i_pages lock. 470*f5fbe6b7SRoman Gushchin * 471*f5fbe6b7SRoman Gushchin * Grabbing old_wb->list_lock, inode->i_lock and the i_pages lock 472*f5fbe6b7SRoman Gushchin * gives us exclusion against all wb related operations on @inode 473*f5fbe6b7SRoman Gushchin * including IO list manipulations and stat updates. 474*f5fbe6b7SRoman Gushchin */ 475*f5fbe6b7SRoman Gushchin if (old_wb < new_wb) { 476*f5fbe6b7SRoman Gushchin spin_lock(&old_wb->list_lock); 477*f5fbe6b7SRoman Gushchin spin_lock_nested(&new_wb->list_lock, SINGLE_DEPTH_NESTING); 478*f5fbe6b7SRoman Gushchin } else { 479*f5fbe6b7SRoman Gushchin spin_lock(&new_wb->list_lock); 480*f5fbe6b7SRoman Gushchin spin_lock_nested(&old_wb->list_lock, SINGLE_DEPTH_NESTING); 481*f5fbe6b7SRoman Gushchin } 482*f5fbe6b7SRoman Gushchin 483*f5fbe6b7SRoman Gushchin for (inodep = isw->inodes; *inodep; inodep++) { 484*f5fbe6b7SRoman Gushchin WARN_ON_ONCE((*inodep)->i_wb != old_wb); 485*f5fbe6b7SRoman Gushchin if (inode_do_switch_wbs(*inodep, old_wb, new_wb)) 486*f5fbe6b7SRoman Gushchin nr_switched++; 487*f5fbe6b7SRoman Gushchin } 488*f5fbe6b7SRoman Gushchin 489*f5fbe6b7SRoman Gushchin spin_unlock(&new_wb->list_lock); 490*f5fbe6b7SRoman Gushchin spin_unlock(&old_wb->list_lock); 491*f5fbe6b7SRoman Gushchin 492*f5fbe6b7SRoman Gushchin up_read(&bdi->wb_switch_rwsem); 493*f5fbe6b7SRoman Gushchin 494*f5fbe6b7SRoman Gushchin if (nr_switched) { 495*f5fbe6b7SRoman Gushchin wb_wakeup(new_wb); 496*f5fbe6b7SRoman Gushchin wb_put_many(old_wb, nr_switched); 497*f5fbe6b7SRoman Gushchin } 498*f5fbe6b7SRoman Gushchin 499*f5fbe6b7SRoman Gushchin for (inodep = isw->inodes; *inodep; inodep++) 500*f5fbe6b7SRoman Gushchin iput(*inodep); 501*f5fbe6b7SRoman Gushchin wb_put(new_wb); 502682aa8e1STejun Heo kfree(isw); 503a1a0e23eSTejun Heo atomic_dec(&isw_nr_in_flight); 504682aa8e1STejun Heo } 505682aa8e1STejun Heo 506682aa8e1STejun Heo /** 507682aa8e1STejun Heo * inode_switch_wbs - change the wb association of an inode 508682aa8e1STejun Heo * @inode: target inode 509682aa8e1STejun Heo * @new_wb_id: ID of the new wb 510682aa8e1STejun Heo * 511682aa8e1STejun Heo * Switch @inode's wb association to the wb identified by @new_wb_id. The 512682aa8e1STejun Heo * switching is performed asynchronously and may fail silently. 513682aa8e1STejun Heo */ 514682aa8e1STejun Heo static void inode_switch_wbs(struct inode *inode, int new_wb_id) 515682aa8e1STejun Heo { 516682aa8e1STejun Heo struct backing_dev_info *bdi = inode_to_bdi(inode); 517682aa8e1STejun Heo struct cgroup_subsys_state *memcg_css; 518682aa8e1STejun Heo struct inode_switch_wbs_context *isw; 519682aa8e1STejun Heo 520682aa8e1STejun Heo /* noop if seems to be already in progress */ 521682aa8e1STejun Heo if (inode->i_state & I_WB_SWITCH) 522682aa8e1STejun Heo return; 523682aa8e1STejun Heo 5246444f47eSTejun Heo /* avoid queueing a new switch if too many are already in flight */ 5256444f47eSTejun Heo if (atomic_read(&isw_nr_in_flight) > WB_FRN_MAX_IN_FLIGHT) 5267fc5854fSTejun Heo return; 5277fc5854fSTejun Heo 528*f5fbe6b7SRoman Gushchin isw = kzalloc(sizeof(*isw) + 2 * sizeof(struct inode *), GFP_ATOMIC); 529682aa8e1STejun Heo if (!isw) 5306444f47eSTejun Heo return; 531682aa8e1STejun Heo 5328826ee4fSRoman Gushchin atomic_inc(&isw_nr_in_flight); 5338826ee4fSRoman Gushchin 534682aa8e1STejun Heo /* find and pin the new wb */ 535682aa8e1STejun Heo rcu_read_lock(); 536682aa8e1STejun Heo memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys); 537682aa8e1STejun Heo if (memcg_css) 538682aa8e1STejun Heo isw->new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 539682aa8e1STejun Heo rcu_read_unlock(); 540682aa8e1STejun Heo if (!isw->new_wb) 541682aa8e1STejun Heo goto out_free; 542682aa8e1STejun Heo 543682aa8e1STejun Heo /* while holding I_WB_SWITCH, no one else can update the association */ 544682aa8e1STejun Heo spin_lock(&inode->i_lock); 5451751e8a6SLinus Torvalds if (!(inode->i_sb->s_flags & SB_ACTIVE) || 5464ade5867SRoman Gushchin inode->i_state & (I_WB_SWITCH | I_FREEING | I_WILL_FREE) || 547a1a0e23eSTejun Heo inode_to_wb(inode) == isw->new_wb) { 548a1a0e23eSTejun Heo spin_unlock(&inode->i_lock); 549a1a0e23eSTejun Heo goto out_free; 550a1a0e23eSTejun Heo } 551682aa8e1STejun Heo inode->i_state |= I_WB_SWITCH; 55274524955STahsin Erdogan __iget(inode); 553682aa8e1STejun Heo spin_unlock(&inode->i_lock); 554682aa8e1STejun Heo 555*f5fbe6b7SRoman Gushchin isw->inodes[0] = inode; 556682aa8e1STejun Heo 557682aa8e1STejun Heo /* 558682aa8e1STejun Heo * In addition to synchronizing among switchers, I_WB_SWITCH tells 559b93b0163SMatthew Wilcox * the RCU protected stat update paths to grab the i_page 560b93b0163SMatthew Wilcox * lock so that stat transfer can synchronize against them. 561682aa8e1STejun Heo * Let's continue after I_WB_SWITCH is guaranteed to be visible. 562682aa8e1STejun Heo */ 56329264d92SRoman Gushchin INIT_RCU_WORK(&isw->work, inode_switch_wbs_work_fn); 56429264d92SRoman Gushchin queue_rcu_work(isw_wq, &isw->work); 5656444f47eSTejun Heo return; 566682aa8e1STejun Heo 567682aa8e1STejun Heo out_free: 5688826ee4fSRoman Gushchin atomic_dec(&isw_nr_in_flight); 569682aa8e1STejun Heo if (isw->new_wb) 570682aa8e1STejun Heo wb_put(isw->new_wb); 571682aa8e1STejun Heo kfree(isw); 572682aa8e1STejun Heo } 573682aa8e1STejun Heo 57487e1d789STejun Heo /** 575b16b1debSTejun Heo * wbc_attach_and_unlock_inode - associate wbc with target inode and unlock it 576b16b1debSTejun Heo * @wbc: writeback_control of interest 577b16b1debSTejun Heo * @inode: target inode 578b16b1debSTejun Heo * 579b16b1debSTejun Heo * @inode is locked and about to be written back under the control of @wbc. 580b16b1debSTejun Heo * Record @inode's writeback context into @wbc and unlock the i_lock. On 581b16b1debSTejun Heo * writeback completion, wbc_detach_inode() should be called. This is used 582b16b1debSTejun Heo * to track the cgroup writeback context. 583b16b1debSTejun Heo */ 584b16b1debSTejun Heo void wbc_attach_and_unlock_inode(struct writeback_control *wbc, 585b16b1debSTejun Heo struct inode *inode) 586b16b1debSTejun Heo { 587dd73e4b7STejun Heo if (!inode_cgwb_enabled(inode)) { 588dd73e4b7STejun Heo spin_unlock(&inode->i_lock); 589dd73e4b7STejun Heo return; 590dd73e4b7STejun Heo } 591dd73e4b7STejun Heo 592b16b1debSTejun Heo wbc->wb = inode_to_wb(inode); 5932a814908STejun Heo wbc->inode = inode; 5942a814908STejun Heo 5952a814908STejun Heo wbc->wb_id = wbc->wb->memcg_css->id; 5962a814908STejun Heo wbc->wb_lcand_id = inode->i_wb_frn_winner; 5972a814908STejun Heo wbc->wb_tcand_id = 0; 5982a814908STejun Heo wbc->wb_bytes = 0; 5992a814908STejun Heo wbc->wb_lcand_bytes = 0; 6002a814908STejun Heo wbc->wb_tcand_bytes = 0; 6012a814908STejun Heo 602b16b1debSTejun Heo wb_get(wbc->wb); 603b16b1debSTejun Heo spin_unlock(&inode->i_lock); 604e8a7abf5STejun Heo 605e8a7abf5STejun Heo /* 60665de03e2STejun Heo * A dying wb indicates that either the blkcg associated with the 60765de03e2STejun Heo * memcg changed or the associated memcg is dying. In the first 60865de03e2STejun Heo * case, a replacement wb should already be available and we should 60965de03e2STejun Heo * refresh the wb immediately. In the second case, trying to 61065de03e2STejun Heo * refresh will keep failing. 611e8a7abf5STejun Heo */ 61265de03e2STejun Heo if (unlikely(wb_dying(wbc->wb) && !css_is_dying(wbc->wb->memcg_css))) 613e8a7abf5STejun Heo inode_switch_wbs(inode, wbc->wb_id); 614b16b1debSTejun Heo } 6159b0eb69bSTejun Heo EXPORT_SYMBOL_GPL(wbc_attach_and_unlock_inode); 616b16b1debSTejun Heo 617b16b1debSTejun Heo /** 6182a814908STejun Heo * wbc_detach_inode - disassociate wbc from inode and perform foreign detection 6192a814908STejun Heo * @wbc: writeback_control of the just finished writeback 620b16b1debSTejun Heo * 621b16b1debSTejun Heo * To be called after a writeback attempt of an inode finishes and undoes 622b16b1debSTejun Heo * wbc_attach_and_unlock_inode(). Can be called under any context. 6232a814908STejun Heo * 6242a814908STejun Heo * As concurrent write sharing of an inode is expected to be very rare and 6252a814908STejun Heo * memcg only tracks page ownership on first-use basis severely confining 6262a814908STejun Heo * the usefulness of such sharing, cgroup writeback tracks ownership 6272a814908STejun Heo * per-inode. While the support for concurrent write sharing of an inode 6282a814908STejun Heo * is deemed unnecessary, an inode being written to by different cgroups at 6292a814908STejun Heo * different points in time is a lot more common, and, more importantly, 6302a814908STejun Heo * charging only by first-use can too readily lead to grossly incorrect 6312a814908STejun Heo * behaviors (single foreign page can lead to gigabytes of writeback to be 6322a814908STejun Heo * incorrectly attributed). 6332a814908STejun Heo * 6342a814908STejun Heo * To resolve this issue, cgroup writeback detects the majority dirtier of 6352a814908STejun Heo * an inode and transfers the ownership to it. To avoid unnnecessary 6362a814908STejun Heo * oscillation, the detection mechanism keeps track of history and gives 6372a814908STejun Heo * out the switch verdict only if the foreign usage pattern is stable over 6382a814908STejun Heo * a certain amount of time and/or writeback attempts. 6392a814908STejun Heo * 6402a814908STejun Heo * On each writeback attempt, @wbc tries to detect the majority writer 6412a814908STejun Heo * using Boyer-Moore majority vote algorithm. In addition to the byte 6422a814908STejun Heo * count from the majority voting, it also counts the bytes written for the 6432a814908STejun Heo * current wb and the last round's winner wb (max of last round's current 6442a814908STejun Heo * wb, the winner from two rounds ago, and the last round's majority 6452a814908STejun Heo * candidate). Keeping track of the historical winner helps the algorithm 6462a814908STejun Heo * to semi-reliably detect the most active writer even when it's not the 6472a814908STejun Heo * absolute majority. 6482a814908STejun Heo * 6492a814908STejun Heo * Once the winner of the round is determined, whether the winner is 6502a814908STejun Heo * foreign or not and how much IO time the round consumed is recorded in 6512a814908STejun Heo * inode->i_wb_frn_history. If the amount of recorded foreign IO time is 6522a814908STejun Heo * over a certain threshold, the switch verdict is given. 653b16b1debSTejun Heo */ 654b16b1debSTejun Heo void wbc_detach_inode(struct writeback_control *wbc) 655b16b1debSTejun Heo { 6562a814908STejun Heo struct bdi_writeback *wb = wbc->wb; 6572a814908STejun Heo struct inode *inode = wbc->inode; 658dd73e4b7STejun Heo unsigned long avg_time, max_bytes, max_time; 659dd73e4b7STejun Heo u16 history; 6602a814908STejun Heo int max_id; 6612a814908STejun Heo 662dd73e4b7STejun Heo if (!wb) 663dd73e4b7STejun Heo return; 664dd73e4b7STejun Heo 665dd73e4b7STejun Heo history = inode->i_wb_frn_history; 666dd73e4b7STejun Heo avg_time = inode->i_wb_frn_avg_time; 667dd73e4b7STejun Heo 6682a814908STejun Heo /* pick the winner of this round */ 6692a814908STejun Heo if (wbc->wb_bytes >= wbc->wb_lcand_bytes && 6702a814908STejun Heo wbc->wb_bytes >= wbc->wb_tcand_bytes) { 6712a814908STejun Heo max_id = wbc->wb_id; 6722a814908STejun Heo max_bytes = wbc->wb_bytes; 6732a814908STejun Heo } else if (wbc->wb_lcand_bytes >= wbc->wb_tcand_bytes) { 6742a814908STejun Heo max_id = wbc->wb_lcand_id; 6752a814908STejun Heo max_bytes = wbc->wb_lcand_bytes; 6762a814908STejun Heo } else { 6772a814908STejun Heo max_id = wbc->wb_tcand_id; 6782a814908STejun Heo max_bytes = wbc->wb_tcand_bytes; 6792a814908STejun Heo } 6802a814908STejun Heo 6812a814908STejun Heo /* 6822a814908STejun Heo * Calculate the amount of IO time the winner consumed and fold it 6832a814908STejun Heo * into the running average kept per inode. If the consumed IO 6842a814908STejun Heo * time is lower than avag / WB_FRN_TIME_CUT_DIV, ignore it for 6852a814908STejun Heo * deciding whether to switch or not. This is to prevent one-off 6862a814908STejun Heo * small dirtiers from skewing the verdict. 6872a814908STejun Heo */ 6882a814908STejun Heo max_time = DIV_ROUND_UP((max_bytes >> PAGE_SHIFT) << WB_FRN_TIME_SHIFT, 6892a814908STejun Heo wb->avg_write_bandwidth); 6902a814908STejun Heo if (avg_time) 6912a814908STejun Heo avg_time += (max_time >> WB_FRN_TIME_AVG_SHIFT) - 6922a814908STejun Heo (avg_time >> WB_FRN_TIME_AVG_SHIFT); 6932a814908STejun Heo else 6942a814908STejun Heo avg_time = max_time; /* immediate catch up on first run */ 6952a814908STejun Heo 6962a814908STejun Heo if (max_time >= avg_time / WB_FRN_TIME_CUT_DIV) { 6972a814908STejun Heo int slots; 6982a814908STejun Heo 6992a814908STejun Heo /* 7002a814908STejun Heo * The switch verdict is reached if foreign wb's consume 7012a814908STejun Heo * more than a certain proportion of IO time in a 7022a814908STejun Heo * WB_FRN_TIME_PERIOD. This is loosely tracked by 16 slot 7032a814908STejun Heo * history mask where each bit represents one sixteenth of 7042a814908STejun Heo * the period. Determine the number of slots to shift into 7052a814908STejun Heo * history from @max_time. 7062a814908STejun Heo */ 7072a814908STejun Heo slots = min(DIV_ROUND_UP(max_time, WB_FRN_HIST_UNIT), 7082a814908STejun Heo (unsigned long)WB_FRN_HIST_MAX_SLOTS); 7092a814908STejun Heo history <<= slots; 7102a814908STejun Heo if (wbc->wb_id != max_id) 7112a814908STejun Heo history |= (1U << slots) - 1; 7122a814908STejun Heo 7133a8e9ac8STejun Heo if (history) 7143a8e9ac8STejun Heo trace_inode_foreign_history(inode, wbc, history); 7153a8e9ac8STejun Heo 7162a814908STejun Heo /* 7172a814908STejun Heo * Switch if the current wb isn't the consistent winner. 7182a814908STejun Heo * If there are multiple closely competing dirtiers, the 7192a814908STejun Heo * inode may switch across them repeatedly over time, which 7202a814908STejun Heo * is okay. The main goal is avoiding keeping an inode on 7212a814908STejun Heo * the wrong wb for an extended period of time. 7222a814908STejun Heo */ 723682aa8e1STejun Heo if (hweight32(history) > WB_FRN_HIST_THR_SLOTS) 724682aa8e1STejun Heo inode_switch_wbs(inode, max_id); 7252a814908STejun Heo } 7262a814908STejun Heo 7272a814908STejun Heo /* 7282a814908STejun Heo * Multiple instances of this function may race to update the 7292a814908STejun Heo * following fields but we don't mind occassional inaccuracies. 7302a814908STejun Heo */ 7312a814908STejun Heo inode->i_wb_frn_winner = max_id; 7322a814908STejun Heo inode->i_wb_frn_avg_time = min(avg_time, (unsigned long)U16_MAX); 7332a814908STejun Heo inode->i_wb_frn_history = history; 7342a814908STejun Heo 735b16b1debSTejun Heo wb_put(wbc->wb); 736b16b1debSTejun Heo wbc->wb = NULL; 737b16b1debSTejun Heo } 7389b0eb69bSTejun Heo EXPORT_SYMBOL_GPL(wbc_detach_inode); 739b16b1debSTejun Heo 740b16b1debSTejun Heo /** 74134e51a5eSTejun Heo * wbc_account_cgroup_owner - account writeback to update inode cgroup ownership 7422a814908STejun Heo * @wbc: writeback_control of the writeback in progress 7432a814908STejun Heo * @page: page being written out 7442a814908STejun Heo * @bytes: number of bytes being written out 7452a814908STejun Heo * 7462a814908STejun Heo * @bytes from @page are about to written out during the writeback 7472a814908STejun Heo * controlled by @wbc. Keep the book for foreign inode detection. See 7482a814908STejun Heo * wbc_detach_inode(). 7492a814908STejun Heo */ 75034e51a5eSTejun Heo void wbc_account_cgroup_owner(struct writeback_control *wbc, struct page *page, 7512a814908STejun Heo size_t bytes) 7522a814908STejun Heo { 75366311422STejun Heo struct cgroup_subsys_state *css; 7542a814908STejun Heo int id; 7552a814908STejun Heo 7562a814908STejun Heo /* 7572a814908STejun Heo * pageout() path doesn't attach @wbc to the inode being written 7582a814908STejun Heo * out. This is intentional as we don't want the function to block 7592a814908STejun Heo * behind a slow cgroup. Ultimately, we want pageout() to kick off 7602a814908STejun Heo * regular writeback instead of writing things out itself. 7612a814908STejun Heo */ 76227b36d8fSTejun Heo if (!wbc->wb || wbc->no_cgroup_owner) 7632a814908STejun Heo return; 7642a814908STejun Heo 76566311422STejun Heo css = mem_cgroup_css_from_page(page); 76666311422STejun Heo /* dead cgroups shouldn't contribute to inode ownership arbitration */ 76766311422STejun Heo if (!(css->flags & CSS_ONLINE)) 76866311422STejun Heo return; 76966311422STejun Heo 77066311422STejun Heo id = css->id; 7712a814908STejun Heo 7722a814908STejun Heo if (id == wbc->wb_id) { 7732a814908STejun Heo wbc->wb_bytes += bytes; 7742a814908STejun Heo return; 7752a814908STejun Heo } 7762a814908STejun Heo 7772a814908STejun Heo if (id == wbc->wb_lcand_id) 7782a814908STejun Heo wbc->wb_lcand_bytes += bytes; 7792a814908STejun Heo 7802a814908STejun Heo /* Boyer-Moore majority vote algorithm */ 7812a814908STejun Heo if (!wbc->wb_tcand_bytes) 7822a814908STejun Heo wbc->wb_tcand_id = id; 7832a814908STejun Heo if (id == wbc->wb_tcand_id) 7842a814908STejun Heo wbc->wb_tcand_bytes += bytes; 7852a814908STejun Heo else 7862a814908STejun Heo wbc->wb_tcand_bytes -= min(bytes, wbc->wb_tcand_bytes); 7872a814908STejun Heo } 78834e51a5eSTejun Heo EXPORT_SYMBOL_GPL(wbc_account_cgroup_owner); 7892a814908STejun Heo 7902a814908STejun Heo /** 791703c2708STejun Heo * inode_congested - test whether an inode is congested 79260292bccSTejun Heo * @inode: inode to test for congestion (may be NULL) 793703c2708STejun Heo * @cong_bits: mask of WB_[a]sync_congested bits to test 794703c2708STejun Heo * 795703c2708STejun Heo * Tests whether @inode is congested. @cong_bits is the mask of congestion 796703c2708STejun Heo * bits to test and the return value is the mask of set bits. 797703c2708STejun Heo * 798703c2708STejun Heo * If cgroup writeback is enabled for @inode, the congestion state is 799703c2708STejun Heo * determined by whether the cgwb (cgroup bdi_writeback) for the blkcg 800703c2708STejun Heo * associated with @inode is congested; otherwise, the root wb's congestion 801703c2708STejun Heo * state is used. 80260292bccSTejun Heo * 80360292bccSTejun Heo * @inode is allowed to be NULL as this function is often called on 80460292bccSTejun Heo * mapping->host which is NULL for the swapper space. 805703c2708STejun Heo */ 806703c2708STejun Heo int inode_congested(struct inode *inode, int cong_bits) 807703c2708STejun Heo { 8085cb8b824STejun Heo /* 8095cb8b824STejun Heo * Once set, ->i_wb never becomes NULL while the inode is alive. 8105cb8b824STejun Heo * Start transaction iff ->i_wb is visible. 8115cb8b824STejun Heo */ 812aaa2cacfSTejun Heo if (inode && inode_to_wb_is_valid(inode)) { 8135cb8b824STejun Heo struct bdi_writeback *wb; 8142e898e4cSGreg Thelen struct wb_lock_cookie lock_cookie = {}; 8152e898e4cSGreg Thelen bool congested; 8165cb8b824STejun Heo 8172e898e4cSGreg Thelen wb = unlocked_inode_to_wb_begin(inode, &lock_cookie); 8185cb8b824STejun Heo congested = wb_congested(wb, cong_bits); 8192e898e4cSGreg Thelen unlocked_inode_to_wb_end(inode, &lock_cookie); 8205cb8b824STejun Heo return congested; 821703c2708STejun Heo } 822703c2708STejun Heo 823703c2708STejun Heo return wb_congested(&inode_to_bdi(inode)->wb, cong_bits); 824703c2708STejun Heo } 825703c2708STejun Heo EXPORT_SYMBOL_GPL(inode_congested); 826703c2708STejun Heo 827f2b65121STejun Heo /** 828f2b65121STejun Heo * wb_split_bdi_pages - split nr_pages to write according to bandwidth 829f2b65121STejun Heo * @wb: target bdi_writeback to split @nr_pages to 830f2b65121STejun Heo * @nr_pages: number of pages to write for the whole bdi 831f2b65121STejun Heo * 832f2b65121STejun Heo * Split @wb's portion of @nr_pages according to @wb's write bandwidth in 833f2b65121STejun Heo * relation to the total write bandwidth of all wb's w/ dirty inodes on 834f2b65121STejun Heo * @wb->bdi. 835f2b65121STejun Heo */ 836f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) 837f2b65121STejun Heo { 838f2b65121STejun Heo unsigned long this_bw = wb->avg_write_bandwidth; 839f2b65121STejun Heo unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth); 840f2b65121STejun Heo 841f2b65121STejun Heo if (nr_pages == LONG_MAX) 842f2b65121STejun Heo return LONG_MAX; 843f2b65121STejun Heo 844f2b65121STejun Heo /* 845f2b65121STejun Heo * This may be called on clean wb's and proportional distribution 846f2b65121STejun Heo * may not make sense, just use the original @nr_pages in those 847f2b65121STejun Heo * cases. In general, we wanna err on the side of writing more. 848f2b65121STejun Heo */ 849f2b65121STejun Heo if (!tot_bw || this_bw >= tot_bw) 850f2b65121STejun Heo return nr_pages; 851f2b65121STejun Heo else 852f2b65121STejun Heo return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw); 853f2b65121STejun Heo } 854f2b65121STejun Heo 855db125360STejun Heo /** 856db125360STejun Heo * bdi_split_work_to_wbs - split a wb_writeback_work to all wb's of a bdi 857db125360STejun Heo * @bdi: target backing_dev_info 858db125360STejun Heo * @base_work: wb_writeback_work to issue 859db125360STejun Heo * @skip_if_busy: skip wb's which already have writeback in progress 860db125360STejun Heo * 861db125360STejun Heo * Split and issue @base_work to all wb's (bdi_writeback's) of @bdi which 862db125360STejun Heo * have dirty inodes. If @base_work->nr_page isn't %LONG_MAX, it's 863db125360STejun Heo * distributed to the busy wbs according to each wb's proportion in the 864db125360STejun Heo * total active write bandwidth of @bdi. 865db125360STejun Heo */ 866db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, 867db125360STejun Heo struct wb_writeback_work *base_work, 868db125360STejun Heo bool skip_if_busy) 869db125360STejun Heo { 870b817525aSTejun Heo struct bdi_writeback *last_wb = NULL; 871b33e18f6STejun Heo struct bdi_writeback *wb = list_entry(&bdi->wb_list, 872b817525aSTejun Heo struct bdi_writeback, bdi_node); 873db125360STejun Heo 874db125360STejun Heo might_sleep(); 875db125360STejun Heo restart: 876db125360STejun Heo rcu_read_lock(); 877b817525aSTejun Heo list_for_each_entry_continue_rcu(wb, &bdi->wb_list, bdi_node) { 8785b9cce4cSTejun Heo DEFINE_WB_COMPLETION(fallback_work_done, bdi); 8798a1270cdSTejun Heo struct wb_writeback_work fallback_work; 8808a1270cdSTejun Heo struct wb_writeback_work *work; 8818a1270cdSTejun Heo long nr_pages; 8828a1270cdSTejun Heo 883b817525aSTejun Heo if (last_wb) { 884b817525aSTejun Heo wb_put(last_wb); 885b817525aSTejun Heo last_wb = NULL; 886b817525aSTejun Heo } 887b817525aSTejun Heo 888006a0973STejun Heo /* SYNC_ALL writes out I_DIRTY_TIME too */ 889006a0973STejun Heo if (!wb_has_dirty_io(wb) && 890006a0973STejun Heo (base_work->sync_mode == WB_SYNC_NONE || 891006a0973STejun Heo list_empty(&wb->b_dirty_time))) 892006a0973STejun Heo continue; 893006a0973STejun Heo if (skip_if_busy && writeback_in_progress(wb)) 894db125360STejun Heo continue; 895db125360STejun Heo 8968a1270cdSTejun Heo nr_pages = wb_split_bdi_pages(wb, base_work->nr_pages); 8978a1270cdSTejun Heo 8988a1270cdSTejun Heo work = kmalloc(sizeof(*work), GFP_ATOMIC); 8998a1270cdSTejun Heo if (work) { 9008a1270cdSTejun Heo *work = *base_work; 9018a1270cdSTejun Heo work->nr_pages = nr_pages; 9028a1270cdSTejun Heo work->auto_free = 1; 9038a1270cdSTejun Heo wb_queue_work(wb, work); 9048a1270cdSTejun Heo continue; 905db125360STejun Heo } 9068a1270cdSTejun Heo 9078a1270cdSTejun Heo /* alloc failed, execute synchronously using on-stack fallback */ 9088a1270cdSTejun Heo work = &fallback_work; 9098a1270cdSTejun Heo *work = *base_work; 9108a1270cdSTejun Heo work->nr_pages = nr_pages; 9118a1270cdSTejun Heo work->auto_free = 0; 9128a1270cdSTejun Heo work->done = &fallback_work_done; 9138a1270cdSTejun Heo 9148a1270cdSTejun Heo wb_queue_work(wb, work); 9158a1270cdSTejun Heo 916b817525aSTejun Heo /* 917b817525aSTejun Heo * Pin @wb so that it stays on @bdi->wb_list. This allows 918b817525aSTejun Heo * continuing iteration from @wb after dropping and 919b817525aSTejun Heo * regrabbing rcu read lock. 920b817525aSTejun Heo */ 921b817525aSTejun Heo wb_get(wb); 922b817525aSTejun Heo last_wb = wb; 923b817525aSTejun Heo 924db125360STejun Heo rcu_read_unlock(); 9255b9cce4cSTejun Heo wb_wait_for_completion(&fallback_work_done); 926db125360STejun Heo goto restart; 927db125360STejun Heo } 928db125360STejun Heo rcu_read_unlock(); 929b817525aSTejun Heo 930b817525aSTejun Heo if (last_wb) 931b817525aSTejun Heo wb_put(last_wb); 932db125360STejun Heo } 933db125360STejun Heo 934a1a0e23eSTejun Heo /** 935d62241c7STejun Heo * cgroup_writeback_by_id - initiate cgroup writeback from bdi and memcg IDs 936d62241c7STejun Heo * @bdi_id: target bdi id 937d62241c7STejun Heo * @memcg_id: target memcg css id 938b46ec1daSRandy Dunlap * @nr: number of pages to write, 0 for best-effort dirty flushing 939d62241c7STejun Heo * @reason: reason why some writeback work initiated 940d62241c7STejun Heo * @done: target wb_completion 941d62241c7STejun Heo * 942d62241c7STejun Heo * Initiate flush of the bdi_writeback identified by @bdi_id and @memcg_id 943d62241c7STejun Heo * with the specified parameters. 944d62241c7STejun Heo */ 945d62241c7STejun Heo int cgroup_writeback_by_id(u64 bdi_id, int memcg_id, unsigned long nr, 946d62241c7STejun Heo enum wb_reason reason, struct wb_completion *done) 947d62241c7STejun Heo { 948d62241c7STejun Heo struct backing_dev_info *bdi; 949d62241c7STejun Heo struct cgroup_subsys_state *memcg_css; 950d62241c7STejun Heo struct bdi_writeback *wb; 951d62241c7STejun Heo struct wb_writeback_work *work; 952d62241c7STejun Heo int ret; 953d62241c7STejun Heo 954d62241c7STejun Heo /* lookup bdi and memcg */ 955d62241c7STejun Heo bdi = bdi_get_by_id(bdi_id); 956d62241c7STejun Heo if (!bdi) 957d62241c7STejun Heo return -ENOENT; 958d62241c7STejun Heo 959d62241c7STejun Heo rcu_read_lock(); 960d62241c7STejun Heo memcg_css = css_from_id(memcg_id, &memory_cgrp_subsys); 961d62241c7STejun Heo if (memcg_css && !css_tryget(memcg_css)) 962d62241c7STejun Heo memcg_css = NULL; 963d62241c7STejun Heo rcu_read_unlock(); 964d62241c7STejun Heo if (!memcg_css) { 965d62241c7STejun Heo ret = -ENOENT; 966d62241c7STejun Heo goto out_bdi_put; 967d62241c7STejun Heo } 968d62241c7STejun Heo 969d62241c7STejun Heo /* 970d62241c7STejun Heo * And find the associated wb. If the wb isn't there already 971d62241c7STejun Heo * there's nothing to flush, don't create one. 972d62241c7STejun Heo */ 973d62241c7STejun Heo wb = wb_get_lookup(bdi, memcg_css); 974d62241c7STejun Heo if (!wb) { 975d62241c7STejun Heo ret = -ENOENT; 976d62241c7STejun Heo goto out_css_put; 977d62241c7STejun Heo } 978d62241c7STejun Heo 979d62241c7STejun Heo /* 980d62241c7STejun Heo * If @nr is zero, the caller is attempting to write out most of 981d62241c7STejun Heo * the currently dirty pages. Let's take the current dirty page 982d62241c7STejun Heo * count and inflate it by 25% which should be large enough to 983d62241c7STejun Heo * flush out most dirty pages while avoiding getting livelocked by 984d62241c7STejun Heo * concurrent dirtiers. 985d62241c7STejun Heo */ 986d62241c7STejun Heo if (!nr) { 987d62241c7STejun Heo unsigned long filepages, headroom, dirty, writeback; 988d62241c7STejun Heo 989d62241c7STejun Heo mem_cgroup_wb_stats(wb, &filepages, &headroom, &dirty, 990d62241c7STejun Heo &writeback); 991d62241c7STejun Heo nr = dirty * 10 / 8; 992d62241c7STejun Heo } 993d62241c7STejun Heo 994d62241c7STejun Heo /* issue the writeback work */ 995d62241c7STejun Heo work = kzalloc(sizeof(*work), GFP_NOWAIT | __GFP_NOWARN); 996d62241c7STejun Heo if (work) { 997d62241c7STejun Heo work->nr_pages = nr; 998d62241c7STejun Heo work->sync_mode = WB_SYNC_NONE; 999d62241c7STejun Heo work->range_cyclic = 1; 1000d62241c7STejun Heo work->reason = reason; 1001d62241c7STejun Heo work->done = done; 1002d62241c7STejun Heo work->auto_free = 1; 1003d62241c7STejun Heo wb_queue_work(wb, work); 1004d62241c7STejun Heo ret = 0; 1005d62241c7STejun Heo } else { 1006d62241c7STejun Heo ret = -ENOMEM; 1007d62241c7STejun Heo } 1008d62241c7STejun Heo 1009d62241c7STejun Heo wb_put(wb); 1010d62241c7STejun Heo out_css_put: 1011d62241c7STejun Heo css_put(memcg_css); 1012d62241c7STejun Heo out_bdi_put: 1013d62241c7STejun Heo bdi_put(bdi); 1014d62241c7STejun Heo return ret; 1015d62241c7STejun Heo } 1016d62241c7STejun Heo 1017d62241c7STejun Heo /** 1018a1a0e23eSTejun Heo * cgroup_writeback_umount - flush inode wb switches for umount 1019a1a0e23eSTejun Heo * 1020a1a0e23eSTejun Heo * This function is called when a super_block is about to be destroyed and 1021a1a0e23eSTejun Heo * flushes in-flight inode wb switches. An inode wb switch goes through 1022a1a0e23eSTejun Heo * RCU and then workqueue, so the two need to be flushed in order to ensure 1023a1a0e23eSTejun Heo * that all previously scheduled switches are finished. As wb switches are 1024a1a0e23eSTejun Heo * rare occurrences and synchronize_rcu() can take a while, perform 1025a1a0e23eSTejun Heo * flushing iff wb switches are in flight. 1026a1a0e23eSTejun Heo */ 1027a1a0e23eSTejun Heo void cgroup_writeback_umount(void) 1028a1a0e23eSTejun Heo { 1029592fa002SRoman Gushchin /* 1030592fa002SRoman Gushchin * SB_ACTIVE should be reliably cleared before checking 1031592fa002SRoman Gushchin * isw_nr_in_flight, see generic_shutdown_super(). 1032592fa002SRoman Gushchin */ 1033592fa002SRoman Gushchin smp_mb(); 1034592fa002SRoman Gushchin 1035a1a0e23eSTejun Heo if (atomic_read(&isw_nr_in_flight)) { 1036ec084de9SJiufei Xue /* 1037ec084de9SJiufei Xue * Use rcu_barrier() to wait for all pending callbacks to 1038ec084de9SJiufei Xue * ensure that all in-flight wb switches are in the workqueue. 1039ec084de9SJiufei Xue */ 1040ec084de9SJiufei Xue rcu_barrier(); 1041a1a0e23eSTejun Heo flush_workqueue(isw_wq); 1042a1a0e23eSTejun Heo } 1043a1a0e23eSTejun Heo } 1044a1a0e23eSTejun Heo 1045a1a0e23eSTejun Heo static int __init cgroup_writeback_init(void) 1046a1a0e23eSTejun Heo { 1047a1a0e23eSTejun Heo isw_wq = alloc_workqueue("inode_switch_wbs", 0, 0); 1048a1a0e23eSTejun Heo if (!isw_wq) 1049a1a0e23eSTejun Heo return -ENOMEM; 1050a1a0e23eSTejun Heo return 0; 1051a1a0e23eSTejun Heo } 1052a1a0e23eSTejun Heo fs_initcall(cgroup_writeback_init); 1053a1a0e23eSTejun Heo 1054f2b65121STejun Heo #else /* CONFIG_CGROUP_WRITEBACK */ 1055f2b65121STejun Heo 10567fc5854fSTejun Heo static void bdi_down_write_wb_switch_rwsem(struct backing_dev_info *bdi) { } 10577fc5854fSTejun Heo static void bdi_up_write_wb_switch_rwsem(struct backing_dev_info *bdi) { } 10587fc5854fSTejun Heo 1059f3b6a6dfSRoman Gushchin static void inode_cgwb_move_to_attached(struct inode *inode, 1060f3b6a6dfSRoman Gushchin struct bdi_writeback *wb) 1061f3b6a6dfSRoman Gushchin { 1062f3b6a6dfSRoman Gushchin assert_spin_locked(&wb->list_lock); 1063f3b6a6dfSRoman Gushchin assert_spin_locked(&inode->i_lock); 1064f3b6a6dfSRoman Gushchin 1065f3b6a6dfSRoman Gushchin inode->i_state &= ~I_SYNC_QUEUED; 1066f3b6a6dfSRoman Gushchin list_del_init(&inode->i_io_list); 1067f3b6a6dfSRoman Gushchin wb_io_lists_depopulated(wb); 1068f3b6a6dfSRoman Gushchin } 1069f3b6a6dfSRoman Gushchin 107087e1d789STejun Heo static struct bdi_writeback * 107187e1d789STejun Heo locked_inode_to_wb_and_lock_list(struct inode *inode) 107287e1d789STejun Heo __releases(&inode->i_lock) 107387e1d789STejun Heo __acquires(&wb->list_lock) 107487e1d789STejun Heo { 107587e1d789STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 107687e1d789STejun Heo 107787e1d789STejun Heo spin_unlock(&inode->i_lock); 107887e1d789STejun Heo spin_lock(&wb->list_lock); 107987e1d789STejun Heo return wb; 108087e1d789STejun Heo } 108187e1d789STejun Heo 108287e1d789STejun Heo static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode) 108387e1d789STejun Heo __acquires(&wb->list_lock) 108487e1d789STejun Heo { 108587e1d789STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 108687e1d789STejun Heo 108787e1d789STejun Heo spin_lock(&wb->list_lock); 108887e1d789STejun Heo return wb; 108987e1d789STejun Heo } 109087e1d789STejun Heo 1091f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) 1092f2b65121STejun Heo { 1093f2b65121STejun Heo return nr_pages; 1094f2b65121STejun Heo } 1095f2b65121STejun Heo 1096db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, 1097db125360STejun Heo struct wb_writeback_work *base_work, 1098db125360STejun Heo bool skip_if_busy) 1099db125360STejun Heo { 1100db125360STejun Heo might_sleep(); 1101db125360STejun Heo 1102006a0973STejun Heo if (!skip_if_busy || !writeback_in_progress(&bdi->wb)) { 1103db125360STejun Heo base_work->auto_free = 0; 1104db125360STejun Heo wb_queue_work(&bdi->wb, base_work); 1105db125360STejun Heo } 1106db125360STejun Heo } 1107db125360STejun Heo 1108703c2708STejun Heo #endif /* CONFIG_CGROUP_WRITEBACK */ 1109703c2708STejun Heo 1110e8e8a0c6SJens Axboe /* 1111e8e8a0c6SJens Axboe * Add in the number of potentially dirty inodes, because each inode 1112e8e8a0c6SJens Axboe * write can dirty pagecache in the underlying blockdev. 1113e8e8a0c6SJens Axboe */ 1114e8e8a0c6SJens Axboe static unsigned long get_nr_dirty_pages(void) 1115e8e8a0c6SJens Axboe { 1116e8e8a0c6SJens Axboe return global_node_page_state(NR_FILE_DIRTY) + 1117e8e8a0c6SJens Axboe get_nr_dirty_inodes(); 1118e8e8a0c6SJens Axboe } 1119e8e8a0c6SJens Axboe 1120e8e8a0c6SJens Axboe static void wb_start_writeback(struct bdi_writeback *wb, enum wb_reason reason) 1121b6e51316SJens Axboe { 1122c00ddad3STejun Heo if (!wb_has_dirty_io(wb)) 1123c00ddad3STejun Heo return; 1124c00ddad3STejun Heo 1125c00ddad3STejun Heo /* 1126aac8d41cSJens Axboe * All callers of this function want to start writeback of all 1127aac8d41cSJens Axboe * dirty pages. Places like vmscan can call this at a very 1128aac8d41cSJens Axboe * high frequency, causing pointless allocations of tons of 1129aac8d41cSJens Axboe * work items and keeping the flusher threads busy retrieving 1130aac8d41cSJens Axboe * that work. Ensure that we only allow one of them pending and 113185009b4fSJens Axboe * inflight at the time. 1132aac8d41cSJens Axboe */ 113385009b4fSJens Axboe if (test_bit(WB_start_all, &wb->state) || 113485009b4fSJens Axboe test_and_set_bit(WB_start_all, &wb->state)) 1135aac8d41cSJens Axboe return; 1136aac8d41cSJens Axboe 113785009b4fSJens Axboe wb->start_all_reason = reason; 1138c00ddad3STejun Heo wb_wakeup(wb); 1139d3ddec76SWu Fengguang } 1140d3ddec76SWu Fengguang 1141c5444198SChristoph Hellwig /** 11429ecf4866STejun Heo * wb_start_background_writeback - start background writeback 11439ecf4866STejun Heo * @wb: bdi_writback to write from 1144c5444198SChristoph Hellwig * 1145c5444198SChristoph Hellwig * Description: 11466585027aSJan Kara * This makes sure WB_SYNC_NONE background writeback happens. When 11479ecf4866STejun Heo * this function returns, it is only guaranteed that for given wb 11486585027aSJan Kara * some IO is happening if we are over background dirty threshold. 11496585027aSJan Kara * Caller need not hold sb s_umount semaphore. 1150c5444198SChristoph Hellwig */ 11519ecf4866STejun Heo void wb_start_background_writeback(struct bdi_writeback *wb) 1152c5444198SChristoph Hellwig { 11536585027aSJan Kara /* 11546585027aSJan Kara * We just wake up the flusher thread. It will perform background 11556585027aSJan Kara * writeback as soon as there is no other work to do. 11566585027aSJan Kara */ 11575634cc2aSTejun Heo trace_writeback_wake_background(wb); 11589ecf4866STejun Heo wb_wakeup(wb); 11591da177e4SLinus Torvalds } 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds /* 1162a66979abSDave Chinner * Remove the inode from the writeback list it is on. 1163a66979abSDave Chinner */ 1164c7f54084SDave Chinner void inode_io_list_del(struct inode *inode) 1165a66979abSDave Chinner { 116687e1d789STejun Heo struct bdi_writeback *wb; 1167a66979abSDave Chinner 116887e1d789STejun Heo wb = inode_to_wb_and_lock_list(inode); 1169b35250c0SJan Kara spin_lock(&inode->i_lock); 1170f3b6a6dfSRoman Gushchin 1171f3b6a6dfSRoman Gushchin inode->i_state &= ~I_SYNC_QUEUED; 1172f3b6a6dfSRoman Gushchin list_del_init(&inode->i_io_list); 1173f3b6a6dfSRoman Gushchin wb_io_lists_depopulated(wb); 1174f3b6a6dfSRoman Gushchin 1175b35250c0SJan Kara spin_unlock(&inode->i_lock); 117652ebea74STejun Heo spin_unlock(&wb->list_lock); 1177f758eeabSChristoph Hellwig } 11784301efa4SJan Kara EXPORT_SYMBOL(inode_io_list_del); 1179a66979abSDave Chinner 1180a66979abSDave Chinner /* 11816c60d2b5SDave Chinner * mark an inode as under writeback on the sb 11826c60d2b5SDave Chinner */ 11836c60d2b5SDave Chinner void sb_mark_inode_writeback(struct inode *inode) 11846c60d2b5SDave Chinner { 11856c60d2b5SDave Chinner struct super_block *sb = inode->i_sb; 11866c60d2b5SDave Chinner unsigned long flags; 11876c60d2b5SDave Chinner 11886c60d2b5SDave Chinner if (list_empty(&inode->i_wb_list)) { 11896c60d2b5SDave Chinner spin_lock_irqsave(&sb->s_inode_wblist_lock, flags); 11909a46b04fSBrian Foster if (list_empty(&inode->i_wb_list)) { 11916c60d2b5SDave Chinner list_add_tail(&inode->i_wb_list, &sb->s_inodes_wb); 11929a46b04fSBrian Foster trace_sb_mark_inode_writeback(inode); 11939a46b04fSBrian Foster } 11946c60d2b5SDave Chinner spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags); 11956c60d2b5SDave Chinner } 11966c60d2b5SDave Chinner } 11976c60d2b5SDave Chinner 11986c60d2b5SDave Chinner /* 11996c60d2b5SDave Chinner * clear an inode as under writeback on the sb 12006c60d2b5SDave Chinner */ 12016c60d2b5SDave Chinner void sb_clear_inode_writeback(struct inode *inode) 12026c60d2b5SDave Chinner { 12036c60d2b5SDave Chinner struct super_block *sb = inode->i_sb; 12046c60d2b5SDave Chinner unsigned long flags; 12056c60d2b5SDave Chinner 12066c60d2b5SDave Chinner if (!list_empty(&inode->i_wb_list)) { 12076c60d2b5SDave Chinner spin_lock_irqsave(&sb->s_inode_wblist_lock, flags); 12089a46b04fSBrian Foster if (!list_empty(&inode->i_wb_list)) { 12096c60d2b5SDave Chinner list_del_init(&inode->i_wb_list); 12109a46b04fSBrian Foster trace_sb_clear_inode_writeback(inode); 12119a46b04fSBrian Foster } 12126c60d2b5SDave Chinner spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags); 12136c60d2b5SDave Chinner } 12146c60d2b5SDave Chinner } 12156c60d2b5SDave Chinner 12166c60d2b5SDave Chinner /* 12176610a0bcSAndrew Morton * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 12186610a0bcSAndrew Morton * furthest end of its superblock's dirty-inode list. 12196610a0bcSAndrew Morton * 12206610a0bcSAndrew Morton * Before stamping the inode's ->dirtied_when, we check to see whether it is 122166f3b8e2SJens Axboe * already the most-recently-dirtied inode on the b_dirty list. If that is 12226610a0bcSAndrew Morton * the case then the inode must have been redirtied while it was being written 12236610a0bcSAndrew Morton * out and we don't reset its dirtied_when. 12246610a0bcSAndrew Morton */ 1225b35250c0SJan Kara static void redirty_tail_locked(struct inode *inode, struct bdi_writeback *wb) 12266610a0bcSAndrew Morton { 1227b35250c0SJan Kara assert_spin_locked(&inode->i_lock); 1228b35250c0SJan Kara 122903ba3782SJens Axboe if (!list_empty(&wb->b_dirty)) { 123066f3b8e2SJens Axboe struct inode *tail; 12316610a0bcSAndrew Morton 12327ccf19a8SNick Piggin tail = wb_inode(wb->b_dirty.next); 123366f3b8e2SJens Axboe if (time_before(inode->dirtied_when, tail->dirtied_when)) 12346610a0bcSAndrew Morton inode->dirtied_when = jiffies; 12356610a0bcSAndrew Morton } 1236c7f54084SDave Chinner inode_io_list_move_locked(inode, wb, &wb->b_dirty); 12375afced3bSJan Kara inode->i_state &= ~I_SYNC_QUEUED; 12386610a0bcSAndrew Morton } 12396610a0bcSAndrew Morton 1240b35250c0SJan Kara static void redirty_tail(struct inode *inode, struct bdi_writeback *wb) 1241b35250c0SJan Kara { 1242b35250c0SJan Kara spin_lock(&inode->i_lock); 1243b35250c0SJan Kara redirty_tail_locked(inode, wb); 1244b35250c0SJan Kara spin_unlock(&inode->i_lock); 1245b35250c0SJan Kara } 1246b35250c0SJan Kara 12476610a0bcSAndrew Morton /* 124866f3b8e2SJens Axboe * requeue inode for re-scanning after bdi->b_io list is exhausted. 1249c986d1e2SAndrew Morton */ 1250f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb) 1251c986d1e2SAndrew Morton { 1252c7f54084SDave Chinner inode_io_list_move_locked(inode, wb, &wb->b_more_io); 1253c986d1e2SAndrew Morton } 1254c986d1e2SAndrew Morton 12551c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode) 12561c0eeaf5SJoern Engel { 1257365b94aeSJan Kara inode->i_state &= ~I_SYNC; 12584eff96ddSJan Kara /* If inode is clean an unused, put it into LRU now... */ 12594eff96ddSJan Kara inode_add_lru(inode); 1260365b94aeSJan Kara /* Waiters must see I_SYNC cleared before being woken up */ 12611c0eeaf5SJoern Engel smp_mb(); 12621c0eeaf5SJoern Engel wake_up_bit(&inode->i_state, __I_SYNC); 12631c0eeaf5SJoern Engel } 12641c0eeaf5SJoern Engel 1265d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t) 1266d2caa3c5SJeff Layton { 1267d2caa3c5SJeff Layton bool ret = time_after(inode->dirtied_when, t); 1268d2caa3c5SJeff Layton #ifndef CONFIG_64BIT 1269d2caa3c5SJeff Layton /* 1270d2caa3c5SJeff Layton * For inodes being constantly redirtied, dirtied_when can get stuck. 1271d2caa3c5SJeff Layton * It _appears_ to be in the future, but is actually in distant past. 1272d2caa3c5SJeff Layton * This test is necessary to prevent such wrapped-around relative times 12735b0830cbSJens Axboe * from permanently stopping the whole bdi writeback. 1274d2caa3c5SJeff Layton */ 1275d2caa3c5SJeff Layton ret = ret && time_before_eq(inode->dirtied_when, jiffies); 1276d2caa3c5SJeff Layton #endif 1277d2caa3c5SJeff Layton return ret; 1278d2caa3c5SJeff Layton } 1279d2caa3c5SJeff Layton 12800ae45f63STheodore Ts'o #define EXPIRE_DIRTY_ATIME 0x0001 12810ae45f63STheodore Ts'o 1282c986d1e2SAndrew Morton /* 1283f9cae926SJan Kara * Move expired (dirtied before dirtied_before) dirty inodes from 1284697e6fedSJan Kara * @delaying_queue to @dispatch_queue. 12852c136579SFengguang Wu */ 1286e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue, 12872c136579SFengguang Wu struct list_head *dispatch_queue, 12885fcd5750SJan Kara unsigned long dirtied_before) 12892c136579SFengguang Wu { 12905c03449dSShaohua Li LIST_HEAD(tmp); 12915c03449dSShaohua Li struct list_head *pos, *node; 1292cf137307SJens Axboe struct super_block *sb = NULL; 12935c03449dSShaohua Li struct inode *inode; 1294cf137307SJens Axboe int do_sb_sort = 0; 1295e84d0a4fSWu Fengguang int moved = 0; 12965c03449dSShaohua Li 12972c136579SFengguang Wu while (!list_empty(delaying_queue)) { 12987ccf19a8SNick Piggin inode = wb_inode(delaying_queue->prev); 1299f9cae926SJan Kara if (inode_dirtied_after(inode, dirtied_before)) 13002c136579SFengguang Wu break; 1301c7f54084SDave Chinner list_move(&inode->i_io_list, &tmp); 1302a8855990SJan Kara moved++; 13035afced3bSJan Kara spin_lock(&inode->i_lock); 13045afced3bSJan Kara inode->i_state |= I_SYNC_QUEUED; 13055afced3bSJan Kara spin_unlock(&inode->i_lock); 1306a8855990SJan Kara if (sb_is_blkdev_sb(inode->i_sb)) 1307a8855990SJan Kara continue; 1308cf137307SJens Axboe if (sb && sb != inode->i_sb) 1309cf137307SJens Axboe do_sb_sort = 1; 1310cf137307SJens Axboe sb = inode->i_sb; 13115c03449dSShaohua Li } 13125c03449dSShaohua Li 1313cf137307SJens Axboe /* just one sb in list, splice to dispatch_queue and we're done */ 1314cf137307SJens Axboe if (!do_sb_sort) { 1315cf137307SJens Axboe list_splice(&tmp, dispatch_queue); 1316e84d0a4fSWu Fengguang goto out; 1317cf137307SJens Axboe } 1318cf137307SJens Axboe 13195c03449dSShaohua Li /* Move inodes from one superblock together */ 13205c03449dSShaohua Li while (!list_empty(&tmp)) { 13217ccf19a8SNick Piggin sb = wb_inode(tmp.prev)->i_sb; 13225c03449dSShaohua Li list_for_each_prev_safe(pos, node, &tmp) { 13237ccf19a8SNick Piggin inode = wb_inode(pos); 13245c03449dSShaohua Li if (inode->i_sb == sb) 1325c7f54084SDave Chinner list_move(&inode->i_io_list, dispatch_queue); 13262c136579SFengguang Wu } 13272c136579SFengguang Wu } 1328e84d0a4fSWu Fengguang out: 1329e84d0a4fSWu Fengguang return moved; 13305c03449dSShaohua Li } 13312c136579SFengguang Wu 13322c136579SFengguang Wu /* 13332c136579SFengguang Wu * Queue all expired dirty inodes for io, eldest first. 13344ea879b9SWu Fengguang * Before 13354ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 13364ea879b9SWu Fengguang * =============> gf edc BA 13374ea879b9SWu Fengguang * After 13384ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 13394ea879b9SWu Fengguang * =============> g fBAedc 13404ea879b9SWu Fengguang * | 13414ea879b9SWu Fengguang * +--> dequeue for IO 13422c136579SFengguang Wu */ 1343f9cae926SJan Kara static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work, 1344f9cae926SJan Kara unsigned long dirtied_before) 13452c136579SFengguang Wu { 1346e84d0a4fSWu Fengguang int moved; 1347f9cae926SJan Kara unsigned long time_expire_jif = dirtied_before; 13480ae45f63STheodore Ts'o 1349f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 13504ea879b9SWu Fengguang list_splice_init(&wb->b_more_io, &wb->b_io); 13515fcd5750SJan Kara moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, dirtied_before); 1352f9cae926SJan Kara if (!work->for_sync) 1353f9cae926SJan Kara time_expire_jif = jiffies - dirtytime_expire_interval * HZ; 13540ae45f63STheodore Ts'o moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io, 13555fcd5750SJan Kara time_expire_jif); 1356d6c10f1fSTejun Heo if (moved) 1357d6c10f1fSTejun Heo wb_io_lists_populated(wb); 1358f9cae926SJan Kara trace_writeback_queue_io(wb, work, dirtied_before, moved); 135966f3b8e2SJens Axboe } 136066f3b8e2SJens Axboe 1361a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc) 136266f3b8e2SJens Axboe { 13639fb0a7daSTejun Heo int ret; 13649fb0a7daSTejun Heo 13659fb0a7daSTejun Heo if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) { 13669fb0a7daSTejun Heo trace_writeback_write_inode_start(inode, wbc); 13679fb0a7daSTejun Heo ret = inode->i_sb->s_op->write_inode(inode, wbc); 13689fb0a7daSTejun Heo trace_writeback_write_inode(inode, wbc); 13699fb0a7daSTejun Heo return ret; 13709fb0a7daSTejun Heo } 137103ba3782SJens Axboe return 0; 137266f3b8e2SJens Axboe } 137308d8e974SFengguang Wu 13742c136579SFengguang Wu /* 1375169ebd90SJan Kara * Wait for writeback on an inode to complete. Called with i_lock held. 1376169ebd90SJan Kara * Caller must make sure inode cannot go away when we drop i_lock. 137701c03194SChristoph Hellwig */ 1378169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode) 1379169ebd90SJan Kara __releases(inode->i_lock) 1380169ebd90SJan Kara __acquires(inode->i_lock) 138101c03194SChristoph Hellwig { 138201c03194SChristoph Hellwig DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); 138301c03194SChristoph Hellwig wait_queue_head_t *wqh; 138401c03194SChristoph Hellwig 138501c03194SChristoph Hellwig wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 138658a9d3d8SRichard Kennedy while (inode->i_state & I_SYNC) { 1387250df6edSDave Chinner spin_unlock(&inode->i_lock); 138874316201SNeilBrown __wait_on_bit(wqh, &wq, bit_wait, 138974316201SNeilBrown TASK_UNINTERRUPTIBLE); 1390250df6edSDave Chinner spin_lock(&inode->i_lock); 139158a9d3d8SRichard Kennedy } 139201c03194SChristoph Hellwig } 139301c03194SChristoph Hellwig 139401c03194SChristoph Hellwig /* 1395169ebd90SJan Kara * Wait for writeback on an inode to complete. Caller must have inode pinned. 1396169ebd90SJan Kara */ 1397169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode) 1398169ebd90SJan Kara { 1399169ebd90SJan Kara spin_lock(&inode->i_lock); 1400169ebd90SJan Kara __inode_wait_for_writeback(inode); 1401169ebd90SJan Kara spin_unlock(&inode->i_lock); 1402169ebd90SJan Kara } 1403169ebd90SJan Kara 1404169ebd90SJan Kara /* 1405169ebd90SJan Kara * Sleep until I_SYNC is cleared. This function must be called with i_lock 1406169ebd90SJan Kara * held and drops it. It is aimed for callers not holding any inode reference 1407169ebd90SJan Kara * so once i_lock is dropped, inode can go away. 1408169ebd90SJan Kara */ 1409169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode) 1410169ebd90SJan Kara __releases(inode->i_lock) 1411169ebd90SJan Kara { 1412169ebd90SJan Kara DEFINE_WAIT(wait); 1413169ebd90SJan Kara wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 1414169ebd90SJan Kara int sleep; 1415169ebd90SJan Kara 1416169ebd90SJan Kara prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); 1417169ebd90SJan Kara sleep = inode->i_state & I_SYNC; 1418169ebd90SJan Kara spin_unlock(&inode->i_lock); 1419169ebd90SJan Kara if (sleep) 1420169ebd90SJan Kara schedule(); 1421169ebd90SJan Kara finish_wait(wqh, &wait); 1422169ebd90SJan Kara } 1423169ebd90SJan Kara 1424169ebd90SJan Kara /* 1425ccb26b5aSJan Kara * Find proper writeback list for the inode depending on its current state and 1426ccb26b5aSJan Kara * possibly also change of its state while we were doing writeback. Here we 1427ccb26b5aSJan Kara * handle things such as livelock prevention or fairness of writeback among 1428ccb26b5aSJan Kara * inodes. This function can be called only by flusher thread - noone else 1429ccb26b5aSJan Kara * processes all inodes in writeback lists and requeueing inodes behind flusher 1430ccb26b5aSJan Kara * thread's back can have unexpected consequences. 1431ccb26b5aSJan Kara */ 1432ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb, 1433ccb26b5aSJan Kara struct writeback_control *wbc) 1434ccb26b5aSJan Kara { 1435ccb26b5aSJan Kara if (inode->i_state & I_FREEING) 1436ccb26b5aSJan Kara return; 1437ccb26b5aSJan Kara 1438ccb26b5aSJan Kara /* 1439ccb26b5aSJan Kara * Sync livelock prevention. Each inode is tagged and synced in one 1440ccb26b5aSJan Kara * shot. If still dirty, it will be redirty_tail()'ed below. Update 1441ccb26b5aSJan Kara * the dirty time to prevent enqueue and sync it again. 1442ccb26b5aSJan Kara */ 1443ccb26b5aSJan Kara if ((inode->i_state & I_DIRTY) && 1444ccb26b5aSJan Kara (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)) 1445ccb26b5aSJan Kara inode->dirtied_when = jiffies; 1446ccb26b5aSJan Kara 14474f8ad655SJan Kara if (wbc->pages_skipped) { 14484f8ad655SJan Kara /* 14494f8ad655SJan Kara * writeback is not making progress due to locked 14504f8ad655SJan Kara * buffers. Skip this inode for now. 14514f8ad655SJan Kara */ 1452b35250c0SJan Kara redirty_tail_locked(inode, wb); 14534f8ad655SJan Kara return; 14544f8ad655SJan Kara } 14554f8ad655SJan Kara 1456ccb26b5aSJan Kara if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) { 1457ccb26b5aSJan Kara /* 1458ccb26b5aSJan Kara * We didn't write back all the pages. nfs_writepages() 1459ccb26b5aSJan Kara * sometimes bales out without doing anything. 1460ccb26b5aSJan Kara */ 1461ccb26b5aSJan Kara if (wbc->nr_to_write <= 0) { 1462ccb26b5aSJan Kara /* Slice used up. Queue for next turn. */ 1463ccb26b5aSJan Kara requeue_io(inode, wb); 1464ccb26b5aSJan Kara } else { 1465ccb26b5aSJan Kara /* 1466ccb26b5aSJan Kara * Writeback blocked by something other than 1467ccb26b5aSJan Kara * congestion. Delay the inode for some time to 1468ccb26b5aSJan Kara * avoid spinning on the CPU (100% iowait) 1469ccb26b5aSJan Kara * retrying writeback of the dirty page/inode 1470ccb26b5aSJan Kara * that cannot be performed immediately. 1471ccb26b5aSJan Kara */ 1472b35250c0SJan Kara redirty_tail_locked(inode, wb); 1473ccb26b5aSJan Kara } 1474ccb26b5aSJan Kara } else if (inode->i_state & I_DIRTY) { 1475ccb26b5aSJan Kara /* 1476ccb26b5aSJan Kara * Filesystems can dirty the inode during writeback operations, 1477ccb26b5aSJan Kara * such as delayed allocation during submission or metadata 1478ccb26b5aSJan Kara * updates after data IO completion. 1479ccb26b5aSJan Kara */ 1480b35250c0SJan Kara redirty_tail_locked(inode, wb); 14810ae45f63STheodore Ts'o } else if (inode->i_state & I_DIRTY_TIME) { 1482a2f48706STheodore Ts'o inode->dirtied_when = jiffies; 1483c7f54084SDave Chinner inode_io_list_move_locked(inode, wb, &wb->b_dirty_time); 14845afced3bSJan Kara inode->i_state &= ~I_SYNC_QUEUED; 1485ccb26b5aSJan Kara } else { 1486ccb26b5aSJan Kara /* The inode is clean. Remove from writeback lists. */ 1487f3b6a6dfSRoman Gushchin inode_cgwb_move_to_attached(inode, wb); 1488ccb26b5aSJan Kara } 1489ccb26b5aSJan Kara } 1490ccb26b5aSJan Kara 1491ccb26b5aSJan Kara /* 1492da0c4c60SEric Biggers * Write out an inode and its dirty pages (or some of its dirty pages, depending 1493da0c4c60SEric Biggers * on @wbc->nr_to_write), and clear the relevant dirty flags from i_state. 1494da0c4c60SEric Biggers * 1495da0c4c60SEric Biggers * This doesn't remove the inode from the writeback list it is on, except 1496da0c4c60SEric Biggers * potentially to move it from b_dirty_time to b_dirty due to timestamp 1497da0c4c60SEric Biggers * expiration. The caller is otherwise responsible for writeback list handling. 1498da0c4c60SEric Biggers * 1499da0c4c60SEric Biggers * The caller is also responsible for setting the I_SYNC flag beforehand and 1500da0c4c60SEric Biggers * calling inode_sync_complete() to clear it afterwards. 15011da177e4SLinus Torvalds */ 15021da177e4SLinus Torvalds static int 1503cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) 15041da177e4SLinus Torvalds { 15051da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 1506251d6a47SWu Fengguang long nr_to_write = wbc->nr_to_write; 150701c03194SChristoph Hellwig unsigned dirty; 15081da177e4SLinus Torvalds int ret; 15091da177e4SLinus Torvalds 15104f8ad655SJan Kara WARN_ON(!(inode->i_state & I_SYNC)); 15111da177e4SLinus Torvalds 15129fb0a7daSTejun Heo trace_writeback_single_inode_start(inode, wbc, nr_to_write); 15139fb0a7daSTejun Heo 15141da177e4SLinus Torvalds ret = do_writepages(mapping, wbc); 15151da177e4SLinus Torvalds 151626821ed4SChristoph Hellwig /* 151726821ed4SChristoph Hellwig * Make sure to wait on the data before writing out the metadata. 151826821ed4SChristoph Hellwig * This is important for filesystems that modify metadata on data 15197747bd4bSDave Chinner * I/O completion. We don't do it for sync(2) writeback because it has a 15207747bd4bSDave Chinner * separate, external IO completion path and ->sync_fs for guaranteeing 15217747bd4bSDave Chinner * inode metadata is written back correctly. 152226821ed4SChristoph Hellwig */ 15237747bd4bSDave Chinner if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) { 152426821ed4SChristoph Hellwig int err = filemap_fdatawait(mapping); 15251da177e4SLinus Torvalds if (ret == 0) 15261da177e4SLinus Torvalds ret = err; 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds 15295547e8aaSDmitry Monakhov /* 15301e249cb5SEric Biggers * If the inode has dirty timestamps and we need to write them, call 15311e249cb5SEric Biggers * mark_inode_dirty_sync() to notify the filesystem about it and to 15321e249cb5SEric Biggers * change I_DIRTY_TIME into I_DIRTY_SYNC. 15331e249cb5SEric Biggers */ 15341e249cb5SEric Biggers if ((inode->i_state & I_DIRTY_TIME) && 153583dc881dSEric Biggers (wbc->sync_mode == WB_SYNC_ALL || 15361e249cb5SEric Biggers time_after(jiffies, inode->dirtied_time_when + 15371e249cb5SEric Biggers dirtytime_expire_interval * HZ))) { 15381e249cb5SEric Biggers trace_writeback_lazytime(inode); 15391e249cb5SEric Biggers mark_inode_dirty_sync(inode); 15401e249cb5SEric Biggers } 15411e249cb5SEric Biggers 15421e249cb5SEric Biggers /* 1543da0c4c60SEric Biggers * Get and clear the dirty flags from i_state. This needs to be done 1544da0c4c60SEric Biggers * after calling writepages because some filesystems may redirty the 1545da0c4c60SEric Biggers * inode during writepages due to delalloc. It also needs to be done 1546da0c4c60SEric Biggers * after handling timestamp expiration, as that may dirty the inode too. 15475547e8aaSDmitry Monakhov */ 1548250df6edSDave Chinner spin_lock(&inode->i_lock); 15495547e8aaSDmitry Monakhov dirty = inode->i_state & I_DIRTY; 15500ae45f63STheodore Ts'o inode->i_state &= ~dirty; 15519c6ac78eSTejun Heo 15529c6ac78eSTejun Heo /* 15539c6ac78eSTejun Heo * Paired with smp_mb() in __mark_inode_dirty(). This allows 15549c6ac78eSTejun Heo * __mark_inode_dirty() to test i_state without grabbing i_lock - 15559c6ac78eSTejun Heo * either they see the I_DIRTY bits cleared or we see the dirtied 15569c6ac78eSTejun Heo * inode. 15579c6ac78eSTejun Heo * 15589c6ac78eSTejun Heo * I_DIRTY_PAGES is always cleared together above even if @mapping 15599c6ac78eSTejun Heo * still has dirty pages. The flag is reinstated after smp_mb() if 15609c6ac78eSTejun Heo * necessary. This guarantees that either __mark_inode_dirty() 15619c6ac78eSTejun Heo * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY. 15629c6ac78eSTejun Heo */ 15639c6ac78eSTejun Heo smp_mb(); 15649c6ac78eSTejun Heo 15659c6ac78eSTejun Heo if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 15669c6ac78eSTejun Heo inode->i_state |= I_DIRTY_PAGES; 15679c6ac78eSTejun Heo 1568250df6edSDave Chinner spin_unlock(&inode->i_lock); 15699c6ac78eSTejun Heo 157026821ed4SChristoph Hellwig /* Don't write the inode if only I_DIRTY_PAGES was set */ 15710ae45f63STheodore Ts'o if (dirty & ~I_DIRTY_PAGES) { 1572a9185b41SChristoph Hellwig int err = write_inode(inode, wbc); 15731da177e4SLinus Torvalds if (ret == 0) 15741da177e4SLinus Torvalds ret = err; 15751da177e4SLinus Torvalds } 15764f8ad655SJan Kara trace_writeback_single_inode(inode, wbc, nr_to_write); 15774f8ad655SJan Kara return ret; 15784f8ad655SJan Kara } 15794f8ad655SJan Kara 15804f8ad655SJan Kara /* 1581da0c4c60SEric Biggers * Write out an inode's dirty data and metadata on-demand, i.e. separately from 1582da0c4c60SEric Biggers * the regular batched writeback done by the flusher threads in 1583da0c4c60SEric Biggers * writeback_sb_inodes(). @wbc controls various aspects of the write, such as 1584da0c4c60SEric Biggers * whether it is a data-integrity sync (%WB_SYNC_ALL) or not (%WB_SYNC_NONE). 15854f8ad655SJan Kara * 1586da0c4c60SEric Biggers * To prevent the inode from going away, either the caller must have a reference 1587da0c4c60SEric Biggers * to the inode, or the inode must have I_WILL_FREE or I_FREEING set. 15884f8ad655SJan Kara */ 1589aaf25593STejun Heo static int writeback_single_inode(struct inode *inode, 15904f8ad655SJan Kara struct writeback_control *wbc) 15914f8ad655SJan Kara { 1592aaf25593STejun Heo struct bdi_writeback *wb; 15934f8ad655SJan Kara int ret = 0; 15944f8ad655SJan Kara 15954f8ad655SJan Kara spin_lock(&inode->i_lock); 15964f8ad655SJan Kara if (!atomic_read(&inode->i_count)) 15974f8ad655SJan Kara WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); 15984f8ad655SJan Kara else 15994f8ad655SJan Kara WARN_ON(inode->i_state & I_WILL_FREE); 16004f8ad655SJan Kara 16014f8ad655SJan Kara if (inode->i_state & I_SYNC) { 1602da0c4c60SEric Biggers /* 1603da0c4c60SEric Biggers * Writeback is already running on the inode. For WB_SYNC_NONE, 1604da0c4c60SEric Biggers * that's enough and we can just return. For WB_SYNC_ALL, we 1605da0c4c60SEric Biggers * must wait for the existing writeback to complete, then do 1606da0c4c60SEric Biggers * writeback again if there's anything left. 1607da0c4c60SEric Biggers */ 16084f8ad655SJan Kara if (wbc->sync_mode != WB_SYNC_ALL) 16094f8ad655SJan Kara goto out; 1610169ebd90SJan Kara __inode_wait_for_writeback(inode); 16114f8ad655SJan Kara } 16124f8ad655SJan Kara WARN_ON(inode->i_state & I_SYNC); 16134f8ad655SJan Kara /* 1614da0c4c60SEric Biggers * If the inode is already fully clean, then there's nothing to do. 1615da0c4c60SEric Biggers * 1616da0c4c60SEric Biggers * For data-integrity syncs we also need to check whether any pages are 1617da0c4c60SEric Biggers * still under writeback, e.g. due to prior WB_SYNC_NONE writeback. If 1618da0c4c60SEric Biggers * there are any such pages, we'll need to wait for them. 16194f8ad655SJan Kara */ 16200ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL) && 1621f9b0e058SJan Kara (wbc->sync_mode != WB_SYNC_ALL || 1622f9b0e058SJan Kara !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))) 16234f8ad655SJan Kara goto out; 16244f8ad655SJan Kara inode->i_state |= I_SYNC; 1625b16b1debSTejun Heo wbc_attach_and_unlock_inode(wbc, inode); 16264f8ad655SJan Kara 1627cd8ed2a4SYan Hong ret = __writeback_single_inode(inode, wbc); 16281da177e4SLinus Torvalds 1629b16b1debSTejun Heo wbc_detach_inode(wbc); 1630aaf25593STejun Heo 1631aaf25593STejun Heo wb = inode_to_wb_and_lock_list(inode); 1632250df6edSDave Chinner spin_lock(&inode->i_lock); 16334f8ad655SJan Kara /* 1634da0c4c60SEric Biggers * If the inode is now fully clean, then it can be safely removed from 1635da0c4c60SEric Biggers * its writeback list (if any). Otherwise the flusher threads are 1636da0c4c60SEric Biggers * responsible for the writeback lists. 16374f8ad655SJan Kara */ 16380ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 1639f3b6a6dfSRoman Gushchin inode_cgwb_move_to_attached(inode, wb); 16404f8ad655SJan Kara spin_unlock(&wb->list_lock); 16411c0eeaf5SJoern Engel inode_sync_complete(inode); 16424f8ad655SJan Kara out: 16434f8ad655SJan Kara spin_unlock(&inode->i_lock); 16441da177e4SLinus Torvalds return ret; 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 1647a88a341aSTejun Heo static long writeback_chunk_size(struct bdi_writeback *wb, 16481a12d8bdSWu Fengguang struct wb_writeback_work *work) 1649d46db3d5SWu Fengguang { 1650d46db3d5SWu Fengguang long pages; 1651d46db3d5SWu Fengguang 1652d46db3d5SWu Fengguang /* 1653d46db3d5SWu Fengguang * WB_SYNC_ALL mode does livelock avoidance by syncing dirty 1654d46db3d5SWu Fengguang * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX 1655d46db3d5SWu Fengguang * here avoids calling into writeback_inodes_wb() more than once. 1656d46db3d5SWu Fengguang * 1657d46db3d5SWu Fengguang * The intended call sequence for WB_SYNC_ALL writeback is: 1658d46db3d5SWu Fengguang * 1659d46db3d5SWu Fengguang * wb_writeback() 1660d46db3d5SWu Fengguang * writeback_sb_inodes() <== called only once 1661d46db3d5SWu Fengguang * write_cache_pages() <== called once for each inode 1662d46db3d5SWu Fengguang * (quickly) tag currently dirty pages 1663d46db3d5SWu Fengguang * (maybe slowly) sync all tagged pages 1664d46db3d5SWu Fengguang */ 1665d46db3d5SWu Fengguang if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages) 1666d46db3d5SWu Fengguang pages = LONG_MAX; 16671a12d8bdSWu Fengguang else { 1668a88a341aSTejun Heo pages = min(wb->avg_write_bandwidth / 2, 1669dcc25ae7STejun Heo global_wb_domain.dirty_limit / DIRTY_SCOPE); 16701a12d8bdSWu Fengguang pages = min(pages, work->nr_pages); 16711a12d8bdSWu Fengguang pages = round_down(pages + MIN_WRITEBACK_PAGES, 16721a12d8bdSWu Fengguang MIN_WRITEBACK_PAGES); 16731a12d8bdSWu Fengguang } 1674d46db3d5SWu Fengguang 1675d46db3d5SWu Fengguang return pages; 1676d46db3d5SWu Fengguang } 1677d46db3d5SWu Fengguang 167803ba3782SJens Axboe /* 1679f11c9c5cSEdward Shishkin * Write a portion of b_io inodes which belong to @sb. 1680edadfb10SChristoph Hellwig * 1681d46db3d5SWu Fengguang * Return the number of pages and/or inodes written. 16820ba13fd1SLinus Torvalds * 16830ba13fd1SLinus Torvalds * NOTE! This is called with wb->list_lock held, and will 16840ba13fd1SLinus Torvalds * unlock and relock that for each inode it ends up doing 16850ba13fd1SLinus Torvalds * IO for. 1686f11c9c5cSEdward Shishkin */ 1687d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb, 1688d46db3d5SWu Fengguang struct bdi_writeback *wb, 1689d46db3d5SWu Fengguang struct wb_writeback_work *work) 169003ba3782SJens Axboe { 1691d46db3d5SWu Fengguang struct writeback_control wbc = { 1692d46db3d5SWu Fengguang .sync_mode = work->sync_mode, 1693d46db3d5SWu Fengguang .tagged_writepages = work->tagged_writepages, 1694d46db3d5SWu Fengguang .for_kupdate = work->for_kupdate, 1695d46db3d5SWu Fengguang .for_background = work->for_background, 16967747bd4bSDave Chinner .for_sync = work->for_sync, 1697d46db3d5SWu Fengguang .range_cyclic = work->range_cyclic, 1698d46db3d5SWu Fengguang .range_start = 0, 1699d46db3d5SWu Fengguang .range_end = LLONG_MAX, 1700d46db3d5SWu Fengguang }; 1701d46db3d5SWu Fengguang unsigned long start_time = jiffies; 1702d46db3d5SWu Fengguang long write_chunk; 1703d46db3d5SWu Fengguang long wrote = 0; /* count both pages and inodes */ 1704d46db3d5SWu Fengguang 170503ba3782SJens Axboe while (!list_empty(&wb->b_io)) { 17067ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 1707aaf25593STejun Heo struct bdi_writeback *tmp_wb; 1708edadfb10SChristoph Hellwig 1709edadfb10SChristoph Hellwig if (inode->i_sb != sb) { 1710d46db3d5SWu Fengguang if (work->sb) { 1711edadfb10SChristoph Hellwig /* 1712edadfb10SChristoph Hellwig * We only want to write back data for this 1713edadfb10SChristoph Hellwig * superblock, move all inodes not belonging 1714edadfb10SChristoph Hellwig * to it back onto the dirty list. 1715edadfb10SChristoph Hellwig */ 1716f758eeabSChristoph Hellwig redirty_tail(inode, wb); 171766f3b8e2SJens Axboe continue; 171866f3b8e2SJens Axboe } 1719edadfb10SChristoph Hellwig 1720edadfb10SChristoph Hellwig /* 1721edadfb10SChristoph Hellwig * The inode belongs to a different superblock. 1722edadfb10SChristoph Hellwig * Bounce back to the caller to unpin this and 1723edadfb10SChristoph Hellwig * pin the next superblock. 1724edadfb10SChristoph Hellwig */ 1725d46db3d5SWu Fengguang break; 1726edadfb10SChristoph Hellwig } 1727edadfb10SChristoph Hellwig 17289843b76aSChristoph Hellwig /* 1729331cbdeeSWanpeng Li * Don't bother with new inodes or inodes being freed, first 1730331cbdeeSWanpeng Li * kind does not need periodic writeout yet, and for the latter 17319843b76aSChristoph Hellwig * kind writeout is handled by the freer. 17329843b76aSChristoph Hellwig */ 1733250df6edSDave Chinner spin_lock(&inode->i_lock); 17349843b76aSChristoph Hellwig if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { 1735b35250c0SJan Kara redirty_tail_locked(inode, wb); 1736250df6edSDave Chinner spin_unlock(&inode->i_lock); 17377ef0d737SNick Piggin continue; 17387ef0d737SNick Piggin } 1739cc1676d9SJan Kara if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) { 1740cc1676d9SJan Kara /* 1741cc1676d9SJan Kara * If this inode is locked for writeback and we are not 1742cc1676d9SJan Kara * doing writeback-for-data-integrity, move it to 1743cc1676d9SJan Kara * b_more_io so that writeback can proceed with the 1744cc1676d9SJan Kara * other inodes on s_io. 1745cc1676d9SJan Kara * 1746cc1676d9SJan Kara * We'll have another go at writing back this inode 1747cc1676d9SJan Kara * when we completed a full scan of b_io. 1748cc1676d9SJan Kara */ 1749cc1676d9SJan Kara spin_unlock(&inode->i_lock); 1750cc1676d9SJan Kara requeue_io(inode, wb); 1751cc1676d9SJan Kara trace_writeback_sb_inodes_requeue(inode); 1752cc1676d9SJan Kara continue; 1753cc1676d9SJan Kara } 1754f0d07b7fSJan Kara spin_unlock(&wb->list_lock); 1755f0d07b7fSJan Kara 17564f8ad655SJan Kara /* 17574f8ad655SJan Kara * We already requeued the inode if it had I_SYNC set and we 17584f8ad655SJan Kara * are doing WB_SYNC_NONE writeback. So this catches only the 17594f8ad655SJan Kara * WB_SYNC_ALL case. 17604f8ad655SJan Kara */ 1761169ebd90SJan Kara if (inode->i_state & I_SYNC) { 1762169ebd90SJan Kara /* Wait for I_SYNC. This function drops i_lock... */ 1763169ebd90SJan Kara inode_sleep_on_writeback(inode); 1764169ebd90SJan Kara /* Inode may be gone, start again */ 1765ead188f9SJan Kara spin_lock(&wb->list_lock); 1766169ebd90SJan Kara continue; 1767169ebd90SJan Kara } 17684f8ad655SJan Kara inode->i_state |= I_SYNC; 1769b16b1debSTejun Heo wbc_attach_and_unlock_inode(&wbc, inode); 1770169ebd90SJan Kara 1771a88a341aSTejun Heo write_chunk = writeback_chunk_size(wb, work); 1772d46db3d5SWu Fengguang wbc.nr_to_write = write_chunk; 1773d46db3d5SWu Fengguang wbc.pages_skipped = 0; 1774250df6edSDave Chinner 1775169ebd90SJan Kara /* 1776169ebd90SJan Kara * We use I_SYNC to pin the inode in memory. While it is set 1777169ebd90SJan Kara * evict_inode() will wait so the inode cannot be freed. 1778169ebd90SJan Kara */ 1779cd8ed2a4SYan Hong __writeback_single_inode(inode, &wbc); 1780d46db3d5SWu Fengguang 1781b16b1debSTejun Heo wbc_detach_inode(&wbc); 1782d46db3d5SWu Fengguang work->nr_pages -= write_chunk - wbc.nr_to_write; 1783d46db3d5SWu Fengguang wrote += write_chunk - wbc.nr_to_write; 1784590dca3aSChris Mason 1785590dca3aSChris Mason if (need_resched()) { 1786590dca3aSChris Mason /* 1787590dca3aSChris Mason * We're trying to balance between building up a nice 1788590dca3aSChris Mason * long list of IOs to improve our merge rate, and 1789590dca3aSChris Mason * getting those IOs out quickly for anyone throttling 1790590dca3aSChris Mason * in balance_dirty_pages(). cond_resched() doesn't 1791590dca3aSChris Mason * unplug, so get our IOs out the door before we 1792590dca3aSChris Mason * give up the CPU. 1793590dca3aSChris Mason */ 1794590dca3aSChris Mason blk_flush_plug(current); 1795590dca3aSChris Mason cond_resched(); 1796590dca3aSChris Mason } 1797590dca3aSChris Mason 1798aaf25593STejun Heo /* 1799aaf25593STejun Heo * Requeue @inode if still dirty. Be careful as @inode may 1800aaf25593STejun Heo * have been switched to another wb in the meantime. 1801aaf25593STejun Heo */ 1802aaf25593STejun Heo tmp_wb = inode_to_wb_and_lock_list(inode); 18034f8ad655SJan Kara spin_lock(&inode->i_lock); 18040ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 1805d46db3d5SWu Fengguang wrote++; 1806aaf25593STejun Heo requeue_inode(inode, tmp_wb, &wbc); 18074f8ad655SJan Kara inode_sync_complete(inode); 18080f1b1fd8SDave Chinner spin_unlock(&inode->i_lock); 1809590dca3aSChris Mason 1810aaf25593STejun Heo if (unlikely(tmp_wb != wb)) { 1811aaf25593STejun Heo spin_unlock(&tmp_wb->list_lock); 1812aaf25593STejun Heo spin_lock(&wb->list_lock); 1813aaf25593STejun Heo } 1814aaf25593STejun Heo 1815d46db3d5SWu Fengguang /* 1816d46db3d5SWu Fengguang * bail out to wb_writeback() often enough to check 1817d46db3d5SWu Fengguang * background threshold and other termination conditions. 1818d46db3d5SWu Fengguang */ 1819d46db3d5SWu Fengguang if (wrote) { 1820d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 1821d46db3d5SWu Fengguang break; 1822d46db3d5SWu Fengguang if (work->nr_pages <= 0) 1823d46db3d5SWu Fengguang break; 18241da177e4SLinus Torvalds } 18258bc3be27SFengguang Wu } 1826d46db3d5SWu Fengguang return wrote; 1827f11c9c5cSEdward Shishkin } 182838f21977SNick Piggin 1829d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb, 1830d46db3d5SWu Fengguang struct wb_writeback_work *work) 1831f11c9c5cSEdward Shishkin { 1832d46db3d5SWu Fengguang unsigned long start_time = jiffies; 1833d46db3d5SWu Fengguang long wrote = 0; 1834f11c9c5cSEdward Shishkin 1835f11c9c5cSEdward Shishkin while (!list_empty(&wb->b_io)) { 18367ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 1837f11c9c5cSEdward Shishkin struct super_block *sb = inode->i_sb; 1838f11c9c5cSEdward Shishkin 1839eb6ef3dfSKonstantin Khlebnikov if (!trylock_super(sb)) { 18400e995816SWu Fengguang /* 1841eb6ef3dfSKonstantin Khlebnikov * trylock_super() may fail consistently due to 18420e995816SWu Fengguang * s_umount being grabbed by someone else. Don't use 18430e995816SWu Fengguang * requeue_io() to avoid busy retrying the inode/sb. 18440e995816SWu Fengguang */ 18450e995816SWu Fengguang redirty_tail(inode, wb); 1846d19de7edSChristoph Hellwig continue; 1847334132aeSChristoph Hellwig } 1848d46db3d5SWu Fengguang wrote += writeback_sb_inodes(sb, wb, work); 1849eb6ef3dfSKonstantin Khlebnikov up_read(&sb->s_umount); 1850f11c9c5cSEdward Shishkin 1851d46db3d5SWu Fengguang /* refer to the same tests at the end of writeback_sb_inodes */ 1852d46db3d5SWu Fengguang if (wrote) { 1853d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 1854d46db3d5SWu Fengguang break; 1855d46db3d5SWu Fengguang if (work->nr_pages <= 0) 1856f11c9c5cSEdward Shishkin break; 1857f11c9c5cSEdward Shishkin } 1858d46db3d5SWu Fengguang } 185966f3b8e2SJens Axboe /* Leave any unwritten inodes on b_io */ 1860d46db3d5SWu Fengguang return wrote; 186166f3b8e2SJens Axboe } 186266f3b8e2SJens Axboe 18637d9f073bSWanpeng Li static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages, 18640e175a18SCurt Wohlgemuth enum wb_reason reason) 1865edadfb10SChristoph Hellwig { 1866d46db3d5SWu Fengguang struct wb_writeback_work work = { 1867d46db3d5SWu Fengguang .nr_pages = nr_pages, 1868d46db3d5SWu Fengguang .sync_mode = WB_SYNC_NONE, 1869d46db3d5SWu Fengguang .range_cyclic = 1, 18700e175a18SCurt Wohlgemuth .reason = reason, 1871d46db3d5SWu Fengguang }; 1872505a666eSLinus Torvalds struct blk_plug plug; 1873edadfb10SChristoph Hellwig 1874505a666eSLinus Torvalds blk_start_plug(&plug); 1875f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 1876424b351fSWu Fengguang if (list_empty(&wb->b_io)) 1877f9cae926SJan Kara queue_io(wb, &work, jiffies); 1878d46db3d5SWu Fengguang __writeback_inodes_wb(wb, &work); 1879f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 1880505a666eSLinus Torvalds blk_finish_plug(&plug); 1881edadfb10SChristoph Hellwig 1882d46db3d5SWu Fengguang return nr_pages - work.nr_pages; 188366f3b8e2SJens Axboe } 188466f3b8e2SJens Axboe 188503ba3782SJens Axboe /* 188603ba3782SJens Axboe * Explicit flushing or periodic writeback of "old" data. 188703ba3782SJens Axboe * 188803ba3782SJens Axboe * Define "old": the first time one of an inode's pages is dirtied, we mark the 188903ba3782SJens Axboe * dirtying-time in the inode's address_space. So this periodic writeback code 189003ba3782SJens Axboe * just walks the superblock inode list, writing back any inodes which are 189103ba3782SJens Axboe * older than a specific point in time. 189203ba3782SJens Axboe * 189303ba3782SJens Axboe * Try to run once per dirty_writeback_interval. But if a writeback event 189403ba3782SJens Axboe * takes longer than a dirty_writeback_interval interval, then leave a 189503ba3782SJens Axboe * one-second gap. 189603ba3782SJens Axboe * 1897f9cae926SJan Kara * dirtied_before takes precedence over nr_to_write. So we'll only write back 189803ba3782SJens Axboe * all dirty pages if they are all attached to "old" mappings. 189903ba3782SJens Axboe */ 1900c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb, 190183ba7b07SChristoph Hellwig struct wb_writeback_work *work) 190203ba3782SJens Axboe { 1903e98be2d5SWu Fengguang unsigned long wb_start = jiffies; 1904d46db3d5SWu Fengguang long nr_pages = work->nr_pages; 1905f9cae926SJan Kara unsigned long dirtied_before = jiffies; 1906a5989bdcSJan Kara struct inode *inode; 1907d46db3d5SWu Fengguang long progress; 1908505a666eSLinus Torvalds struct blk_plug plug; 190903ba3782SJens Axboe 1910505a666eSLinus Torvalds blk_start_plug(&plug); 1911e8dfc305SWu Fengguang spin_lock(&wb->list_lock); 191203ba3782SJens Axboe for (;;) { 191303ba3782SJens Axboe /* 1914d3ddec76SWu Fengguang * Stop writeback when nr_pages has been consumed 191503ba3782SJens Axboe */ 191683ba7b07SChristoph Hellwig if (work->nr_pages <= 0) 191703ba3782SJens Axboe break; 191803ba3782SJens Axboe 191903ba3782SJens Axboe /* 1920aa373cf5SJan Kara * Background writeout and kupdate-style writeback may 1921aa373cf5SJan Kara * run forever. Stop them if there is other work to do 1922aa373cf5SJan Kara * so that e.g. sync can proceed. They'll be restarted 1923aa373cf5SJan Kara * after the other works are all done. 1924aa373cf5SJan Kara */ 1925aa373cf5SJan Kara if ((work->for_background || work->for_kupdate) && 1926f0054bb1STejun Heo !list_empty(&wb->work_list)) 1927aa373cf5SJan Kara break; 1928aa373cf5SJan Kara 1929aa373cf5SJan Kara /* 1930d3ddec76SWu Fengguang * For background writeout, stop when we are below the 1931d3ddec76SWu Fengguang * background dirty threshold 193203ba3782SJens Axboe */ 1933aa661bbeSTejun Heo if (work->for_background && !wb_over_bg_thresh(wb)) 193403ba3782SJens Axboe break; 193503ba3782SJens Axboe 19361bc36b64SJan Kara /* 19371bc36b64SJan Kara * Kupdate and background works are special and we want to 19381bc36b64SJan Kara * include all inodes that need writing. Livelock avoidance is 19391bc36b64SJan Kara * handled by these works yielding to any other work so we are 19401bc36b64SJan Kara * safe. 19411bc36b64SJan Kara */ 1942ba9aa839SWu Fengguang if (work->for_kupdate) { 1943f9cae926SJan Kara dirtied_before = jiffies - 1944ba9aa839SWu Fengguang msecs_to_jiffies(dirty_expire_interval * 10); 19451bc36b64SJan Kara } else if (work->for_background) 1946f9cae926SJan Kara dirtied_before = jiffies; 1947028c2dd1SDave Chinner 19485634cc2aSTejun Heo trace_writeback_start(wb, work); 1949e8dfc305SWu Fengguang if (list_empty(&wb->b_io)) 1950f9cae926SJan Kara queue_io(wb, work, dirtied_before); 195183ba7b07SChristoph Hellwig if (work->sb) 1952d46db3d5SWu Fengguang progress = writeback_sb_inodes(work->sb, wb, work); 1953edadfb10SChristoph Hellwig else 1954d46db3d5SWu Fengguang progress = __writeback_inodes_wb(wb, work); 19555634cc2aSTejun Heo trace_writeback_written(wb, work); 1956028c2dd1SDave Chinner 1957e98be2d5SWu Fengguang wb_update_bandwidth(wb, wb_start); 195803ba3782SJens Axboe 195903ba3782SJens Axboe /* 196071fd05a8SJens Axboe * Did we write something? Try for more 1961e6fb6da2SWu Fengguang * 1962e6fb6da2SWu Fengguang * Dirty inodes are moved to b_io for writeback in batches. 1963e6fb6da2SWu Fengguang * The completion of the current batch does not necessarily 1964e6fb6da2SWu Fengguang * mean the overall work is done. So we keep looping as long 1965e6fb6da2SWu Fengguang * as made some progress on cleaning pages or inodes. 196671fd05a8SJens Axboe */ 1967d46db3d5SWu Fengguang if (progress) 196803ba3782SJens Axboe continue; 1969a5989bdcSJan Kara /* 1970e6fb6da2SWu Fengguang * No more inodes for IO, bail 1971a5989bdcSJan Kara */ 1972b7a2441fSWu Fengguang if (list_empty(&wb->b_more_io)) 197303ba3782SJens Axboe break; 197403ba3782SJens Axboe /* 19758010c3b6SJens Axboe * Nothing written. Wait for some inode to 19768010c3b6SJens Axboe * become available for writeback. Otherwise 19778010c3b6SJens Axboe * we'll just busyloop. 197803ba3782SJens Axboe */ 19795634cc2aSTejun Heo trace_writeback_wait(wb, work); 198003ba3782SJens Axboe inode = wb_inode(wb->b_more_io.prev); 1981250df6edSDave Chinner spin_lock(&inode->i_lock); 1982f0d07b7fSJan Kara spin_unlock(&wb->list_lock); 1983169ebd90SJan Kara /* This function drops i_lock... */ 1984169ebd90SJan Kara inode_sleep_on_writeback(inode); 1985f0d07b7fSJan Kara spin_lock(&wb->list_lock); 198603ba3782SJens Axboe } 1987e8dfc305SWu Fengguang spin_unlock(&wb->list_lock); 1988505a666eSLinus Torvalds blk_finish_plug(&plug); 198903ba3782SJens Axboe 1990d46db3d5SWu Fengguang return nr_pages - work->nr_pages; 199103ba3782SJens Axboe } 199203ba3782SJens Axboe 199303ba3782SJens Axboe /* 199483ba7b07SChristoph Hellwig * Return the next wb_writeback_work struct that hasn't been processed yet. 199503ba3782SJens Axboe */ 1996f0054bb1STejun Heo static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb) 199703ba3782SJens Axboe { 199883ba7b07SChristoph Hellwig struct wb_writeback_work *work = NULL; 199903ba3782SJens Axboe 2000f0054bb1STejun Heo spin_lock_bh(&wb->work_lock); 2001f0054bb1STejun Heo if (!list_empty(&wb->work_list)) { 2002f0054bb1STejun Heo work = list_entry(wb->work_list.next, 200383ba7b07SChristoph Hellwig struct wb_writeback_work, list); 200483ba7b07SChristoph Hellwig list_del_init(&work->list); 200503ba3782SJens Axboe } 2006f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 200783ba7b07SChristoph Hellwig return work; 200803ba3782SJens Axboe } 200903ba3782SJens Axboe 20106585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb) 20116585027aSJan Kara { 2012aa661bbeSTejun Heo if (wb_over_bg_thresh(wb)) { 20136585027aSJan Kara 20146585027aSJan Kara struct wb_writeback_work work = { 20156585027aSJan Kara .nr_pages = LONG_MAX, 20166585027aSJan Kara .sync_mode = WB_SYNC_NONE, 20176585027aSJan Kara .for_background = 1, 20186585027aSJan Kara .range_cyclic = 1, 20190e175a18SCurt Wohlgemuth .reason = WB_REASON_BACKGROUND, 20206585027aSJan Kara }; 20216585027aSJan Kara 20226585027aSJan Kara return wb_writeback(wb, &work); 20236585027aSJan Kara } 20246585027aSJan Kara 20256585027aSJan Kara return 0; 20266585027aSJan Kara } 20276585027aSJan Kara 202803ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb) 202903ba3782SJens Axboe { 203003ba3782SJens Axboe unsigned long expired; 203103ba3782SJens Axboe long nr_pages; 203203ba3782SJens Axboe 203369b62d01SJens Axboe /* 203469b62d01SJens Axboe * When set to zero, disable periodic writeback 203569b62d01SJens Axboe */ 203669b62d01SJens Axboe if (!dirty_writeback_interval) 203769b62d01SJens Axboe return 0; 203869b62d01SJens Axboe 203903ba3782SJens Axboe expired = wb->last_old_flush + 204003ba3782SJens Axboe msecs_to_jiffies(dirty_writeback_interval * 10); 204103ba3782SJens Axboe if (time_before(jiffies, expired)) 204203ba3782SJens Axboe return 0; 204303ba3782SJens Axboe 204403ba3782SJens Axboe wb->last_old_flush = jiffies; 2045cdf01dd5SLinus Torvalds nr_pages = get_nr_dirty_pages(); 204603ba3782SJens Axboe 2047c4a77a6cSJens Axboe if (nr_pages) { 204883ba7b07SChristoph Hellwig struct wb_writeback_work work = { 2049c4a77a6cSJens Axboe .nr_pages = nr_pages, 2050c4a77a6cSJens Axboe .sync_mode = WB_SYNC_NONE, 2051c4a77a6cSJens Axboe .for_kupdate = 1, 2052c4a77a6cSJens Axboe .range_cyclic = 1, 20530e175a18SCurt Wohlgemuth .reason = WB_REASON_PERIODIC, 2054c4a77a6cSJens Axboe }; 2055c4a77a6cSJens Axboe 205683ba7b07SChristoph Hellwig return wb_writeback(wb, &work); 2057c4a77a6cSJens Axboe } 205803ba3782SJens Axboe 205903ba3782SJens Axboe return 0; 206003ba3782SJens Axboe } 206103ba3782SJens Axboe 206285009b4fSJens Axboe static long wb_check_start_all(struct bdi_writeback *wb) 206385009b4fSJens Axboe { 206485009b4fSJens Axboe long nr_pages; 206585009b4fSJens Axboe 206685009b4fSJens Axboe if (!test_bit(WB_start_all, &wb->state)) 206785009b4fSJens Axboe return 0; 206885009b4fSJens Axboe 206985009b4fSJens Axboe nr_pages = get_nr_dirty_pages(); 207085009b4fSJens Axboe if (nr_pages) { 207185009b4fSJens Axboe struct wb_writeback_work work = { 207285009b4fSJens Axboe .nr_pages = wb_split_bdi_pages(wb, nr_pages), 207385009b4fSJens Axboe .sync_mode = WB_SYNC_NONE, 207485009b4fSJens Axboe .range_cyclic = 1, 207585009b4fSJens Axboe .reason = wb->start_all_reason, 207685009b4fSJens Axboe }; 207785009b4fSJens Axboe 207885009b4fSJens Axboe nr_pages = wb_writeback(wb, &work); 207985009b4fSJens Axboe } 208085009b4fSJens Axboe 208185009b4fSJens Axboe clear_bit(WB_start_all, &wb->state); 208285009b4fSJens Axboe return nr_pages; 208385009b4fSJens Axboe } 208485009b4fSJens Axboe 208585009b4fSJens Axboe 208603ba3782SJens Axboe /* 208703ba3782SJens Axboe * Retrieve work items and do the writeback they describe 208803ba3782SJens Axboe */ 208925d130baSWanpeng Li static long wb_do_writeback(struct bdi_writeback *wb) 209003ba3782SJens Axboe { 209183ba7b07SChristoph Hellwig struct wb_writeback_work *work; 2092c4a77a6cSJens Axboe long wrote = 0; 209303ba3782SJens Axboe 20944452226eSTejun Heo set_bit(WB_writeback_running, &wb->state); 2095f0054bb1STejun Heo while ((work = get_next_work_item(wb)) != NULL) { 20965634cc2aSTejun Heo trace_writeback_exec(wb, work); 209783ba7b07SChristoph Hellwig wrote += wb_writeback(wb, work); 20984a3a485bSTahsin Erdogan finish_writeback_work(wb, work); 209903ba3782SJens Axboe } 210003ba3782SJens Axboe 210103ba3782SJens Axboe /* 210285009b4fSJens Axboe * Check for a flush-everything request 210385009b4fSJens Axboe */ 210485009b4fSJens Axboe wrote += wb_check_start_all(wb); 210585009b4fSJens Axboe 210685009b4fSJens Axboe /* 210703ba3782SJens Axboe * Check for periodic writeback, kupdated() style 210803ba3782SJens Axboe */ 210903ba3782SJens Axboe wrote += wb_check_old_data_flush(wb); 21106585027aSJan Kara wrote += wb_check_background_flush(wb); 21114452226eSTejun Heo clear_bit(WB_writeback_running, &wb->state); 211203ba3782SJens Axboe 211303ba3782SJens Axboe return wrote; 211403ba3782SJens Axboe } 211503ba3782SJens Axboe 211603ba3782SJens Axboe /* 211703ba3782SJens Axboe * Handle writeback of dirty data for the device backed by this bdi. Also 2118839a8e86STejun Heo * reschedules periodically and does kupdated style flushing. 211903ba3782SJens Axboe */ 2120f0054bb1STejun Heo void wb_workfn(struct work_struct *work) 212103ba3782SJens Axboe { 2122839a8e86STejun Heo struct bdi_writeback *wb = container_of(to_delayed_work(work), 2123839a8e86STejun Heo struct bdi_writeback, dwork); 212403ba3782SJens Axboe long pages_written; 212503ba3782SJens Axboe 212668f23b89STheodore Ts'o set_worker_desc("flush-%s", bdi_dev_name(wb->bdi)); 2127766f9164SPeter Zijlstra current->flags |= PF_SWAPWRITE; 212803ba3782SJens Axboe 2129839a8e86STejun Heo if (likely(!current_is_workqueue_rescuer() || 21304452226eSTejun Heo !test_bit(WB_registered, &wb->state))) { 213103ba3782SJens Axboe /* 2132f0054bb1STejun Heo * The normal path. Keep writing back @wb until its 2133839a8e86STejun Heo * work_list is empty. Note that this path is also taken 2134f0054bb1STejun Heo * if @wb is shutting down even when we're running off the 2135839a8e86STejun Heo * rescuer as work_list needs to be drained. 213603ba3782SJens Axboe */ 2137839a8e86STejun Heo do { 213825d130baSWanpeng Li pages_written = wb_do_writeback(wb); 2139455b2864SDave Chinner trace_writeback_pages_written(pages_written); 2140f0054bb1STejun Heo } while (!list_empty(&wb->work_list)); 2141839a8e86STejun Heo } else { 2142253c34e9SArtem Bityutskiy /* 2143839a8e86STejun Heo * bdi_wq can't get enough workers and we're running off 2144839a8e86STejun Heo * the emergency worker. Don't hog it. Hopefully, 1024 is 2145839a8e86STejun Heo * enough for efficient IO. 2146253c34e9SArtem Bityutskiy */ 2147f0054bb1STejun Heo pages_written = writeback_inodes_wb(wb, 1024, 2148839a8e86STejun Heo WB_REASON_FORKER_THREAD); 2149839a8e86STejun Heo trace_writeback_pages_written(pages_written); 215003ba3782SJens Axboe } 215103ba3782SJens Axboe 2152f0054bb1STejun Heo if (!list_empty(&wb->work_list)) 2153b8b78495SJan Kara wb_wakeup(wb); 21546ca738d6SDerek Basehore else if (wb_has_dirty_io(wb) && dirty_writeback_interval) 2155f0054bb1STejun Heo wb_wakeup_delayed(wb); 2156455b2864SDave Chinner 2157839a8e86STejun Heo current->flags &= ~PF_SWAPWRITE; 215803ba3782SJens Axboe } 215903ba3782SJens Axboe 216003ba3782SJens Axboe /* 2161595043e5SJens Axboe * Start writeback of `nr_pages' pages on this bdi. If `nr_pages' is zero, 2162595043e5SJens Axboe * write back the whole world. 2163595043e5SJens Axboe */ 2164595043e5SJens Axboe static void __wakeup_flusher_threads_bdi(struct backing_dev_info *bdi, 2165e8e8a0c6SJens Axboe enum wb_reason reason) 2166595043e5SJens Axboe { 2167595043e5SJens Axboe struct bdi_writeback *wb; 2168595043e5SJens Axboe 2169595043e5SJens Axboe if (!bdi_has_dirty_io(bdi)) 2170595043e5SJens Axboe return; 2171595043e5SJens Axboe 2172595043e5SJens Axboe list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node) 2173e8e8a0c6SJens Axboe wb_start_writeback(wb, reason); 2174595043e5SJens Axboe } 2175595043e5SJens Axboe 2176595043e5SJens Axboe void wakeup_flusher_threads_bdi(struct backing_dev_info *bdi, 2177595043e5SJens Axboe enum wb_reason reason) 2178595043e5SJens Axboe { 2179595043e5SJens Axboe rcu_read_lock(); 2180e8e8a0c6SJens Axboe __wakeup_flusher_threads_bdi(bdi, reason); 2181595043e5SJens Axboe rcu_read_unlock(); 2182595043e5SJens Axboe } 2183595043e5SJens Axboe 2184595043e5SJens Axboe /* 21859ba4b2dfSJens Axboe * Wakeup the flusher threads to start writeback of all currently dirty pages 218603ba3782SJens Axboe */ 21879ba4b2dfSJens Axboe void wakeup_flusher_threads(enum wb_reason reason) 218803ba3782SJens Axboe { 2189b8c2f347SChristoph Hellwig struct backing_dev_info *bdi; 2190b8c2f347SChristoph Hellwig 219151350ea0SKonstantin Khlebnikov /* 219251350ea0SKonstantin Khlebnikov * If we are expecting writeback progress we must submit plugged IO. 219351350ea0SKonstantin Khlebnikov */ 219451350ea0SKonstantin Khlebnikov if (blk_needs_flush_plug(current)) 219551350ea0SKonstantin Khlebnikov blk_schedule_flush_plug(current); 219651350ea0SKonstantin Khlebnikov 2197b8c2f347SChristoph Hellwig rcu_read_lock(); 2198595043e5SJens Axboe list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) 2199e8e8a0c6SJens Axboe __wakeup_flusher_threads_bdi(bdi, reason); 2200b8c2f347SChristoph Hellwig rcu_read_unlock(); 220103ba3782SJens Axboe } 220203ba3782SJens Axboe 2203a2f48706STheodore Ts'o /* 2204a2f48706STheodore Ts'o * Wake up bdi's periodically to make sure dirtytime inodes gets 2205a2f48706STheodore Ts'o * written back periodically. We deliberately do *not* check the 2206a2f48706STheodore Ts'o * b_dirtytime list in wb_has_dirty_io(), since this would cause the 2207a2f48706STheodore Ts'o * kernel to be constantly waking up once there are any dirtytime 2208a2f48706STheodore Ts'o * inodes on the system. So instead we define a separate delayed work 2209a2f48706STheodore Ts'o * function which gets called much more rarely. (By default, only 2210a2f48706STheodore Ts'o * once every 12 hours.) 2211a2f48706STheodore Ts'o * 2212a2f48706STheodore Ts'o * If there is any other write activity going on in the file system, 2213a2f48706STheodore Ts'o * this function won't be necessary. But if the only thing that has 2214a2f48706STheodore Ts'o * happened on the file system is a dirtytime inode caused by an atime 2215a2f48706STheodore Ts'o * update, we need this infrastructure below to make sure that inode 2216a2f48706STheodore Ts'o * eventually gets pushed out to disk. 2217a2f48706STheodore Ts'o */ 2218a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w); 2219a2f48706STheodore Ts'o static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback); 2220a2f48706STheodore Ts'o 2221a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w) 2222a2f48706STheodore Ts'o { 2223a2f48706STheodore Ts'o struct backing_dev_info *bdi; 2224a2f48706STheodore Ts'o 2225a2f48706STheodore Ts'o rcu_read_lock(); 2226a2f48706STheodore Ts'o list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 2227001fe6f6STejun Heo struct bdi_writeback *wb; 2228001fe6f6STejun Heo 2229b817525aSTejun Heo list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node) 22306fdf860fSTejun Heo if (!list_empty(&wb->b_dirty_time)) 22316fdf860fSTejun Heo wb_wakeup(wb); 2232a2f48706STheodore Ts'o } 2233a2f48706STheodore Ts'o rcu_read_unlock(); 2234a2f48706STheodore Ts'o schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ); 2235a2f48706STheodore Ts'o } 2236a2f48706STheodore Ts'o 2237a2f48706STheodore Ts'o static int __init start_dirtytime_writeback(void) 2238a2f48706STheodore Ts'o { 2239a2f48706STheodore Ts'o schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ); 2240a2f48706STheodore Ts'o return 0; 2241a2f48706STheodore Ts'o } 2242a2f48706STheodore Ts'o __initcall(start_dirtytime_writeback); 2243a2f48706STheodore Ts'o 22441efff914STheodore Ts'o int dirtytime_interval_handler(struct ctl_table *table, int write, 22459ca48e20STobias Klauser void *buffer, size_t *lenp, loff_t *ppos) 22461efff914STheodore Ts'o { 22471efff914STheodore Ts'o int ret; 22481efff914STheodore Ts'o 22491efff914STheodore Ts'o ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 22501efff914STheodore Ts'o if (ret == 0 && write) 22511efff914STheodore Ts'o mod_delayed_work(system_wq, &dirtytime_work, 0); 22521efff914STheodore Ts'o return ret; 22531efff914STheodore Ts'o } 22541efff914STheodore Ts'o 225503ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode) 225603ba3782SJens Axboe { 225703ba3782SJens Axboe if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { 225803ba3782SJens Axboe struct dentry *dentry; 225903ba3782SJens Axboe const char *name = "?"; 226003ba3782SJens Axboe 226103ba3782SJens Axboe dentry = d_find_alias(inode); 226203ba3782SJens Axboe if (dentry) { 226303ba3782SJens Axboe spin_lock(&dentry->d_lock); 226403ba3782SJens Axboe name = (const char *) dentry->d_name.name; 226503ba3782SJens Axboe } 226603ba3782SJens Axboe printk(KERN_DEBUG 226703ba3782SJens Axboe "%s(%d): dirtied inode %lu (%s) on %s\n", 226803ba3782SJens Axboe current->comm, task_pid_nr(current), inode->i_ino, 226903ba3782SJens Axboe name, inode->i_sb->s_id); 227003ba3782SJens Axboe if (dentry) { 227103ba3782SJens Axboe spin_unlock(&dentry->d_lock); 227203ba3782SJens Axboe dput(dentry); 227303ba3782SJens Axboe } 227403ba3782SJens Axboe } 227503ba3782SJens Axboe } 227603ba3782SJens Axboe 227703ba3782SJens Axboe /** 227835d14f27SEric Biggers * __mark_inode_dirty - internal function to mark an inode dirty 22790117d427SMauro Carvalho Chehab * 228003ba3782SJens Axboe * @inode: inode to mark 228135d14f27SEric Biggers * @flags: what kind of dirty, e.g. I_DIRTY_SYNC. This can be a combination of 228235d14f27SEric Biggers * multiple I_DIRTY_* flags, except that I_DIRTY_TIME can't be combined 228335d14f27SEric Biggers * with I_DIRTY_PAGES. 22840117d427SMauro Carvalho Chehab * 228535d14f27SEric Biggers * Mark an inode as dirty. We notify the filesystem, then update the inode's 228635d14f27SEric Biggers * dirty flags. Then, if needed we add the inode to the appropriate dirty list. 228703ba3782SJens Axboe * 228835d14f27SEric Biggers * Most callers should use mark_inode_dirty() or mark_inode_dirty_sync() 228935d14f27SEric Biggers * instead of calling this directly. 229003ba3782SJens Axboe * 229135d14f27SEric Biggers * CAREFUL! We only add the inode to the dirty list if it is hashed or if it 229235d14f27SEric Biggers * refers to a blockdev. Unhashed inodes will never be added to the dirty list 229335d14f27SEric Biggers * even if they are later hashed, as they will have been marked dirty already. 229403ba3782SJens Axboe * 229535d14f27SEric Biggers * In short, ensure you hash any inodes _before_ you start marking them dirty. 229603ba3782SJens Axboe * 229703ba3782SJens Axboe * Note that for blockdevs, inode->dirtied_when represents the dirtying time of 229803ba3782SJens Axboe * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of 229903ba3782SJens Axboe * the kernel-internal blockdev inode represents the dirtying time of the 230003ba3782SJens Axboe * blockdev's pages. This is why for I_DIRTY_PAGES we always use 230103ba3782SJens Axboe * page->mapping->host, so the page-dirtying time is recorded in the internal 230203ba3782SJens Axboe * blockdev inode. 230303ba3782SJens Axboe */ 230403ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags) 230503ba3782SJens Axboe { 230603ba3782SJens Axboe struct super_block *sb = inode->i_sb; 230735d14f27SEric Biggers int dirtytime = 0; 23080ae45f63STheodore Ts'o 23090ae45f63STheodore Ts'o trace_writeback_mark_inode_dirty(inode, flags); 231003ba3782SJens Axboe 2311e2728c56SEric Biggers if (flags & I_DIRTY_INODE) { 231235d14f27SEric Biggers /* 231335d14f27SEric Biggers * Notify the filesystem about the inode being dirtied, so that 231435d14f27SEric Biggers * (if needed) it can update on-disk fields and journal the 231535d14f27SEric Biggers * inode. This is only needed when the inode itself is being 231635d14f27SEric Biggers * dirtied now. I.e. it's only needed for I_DIRTY_INODE, not 231735d14f27SEric Biggers * for just I_DIRTY_PAGES or I_DIRTY_TIME. 231835d14f27SEric Biggers */ 23199fb0a7daSTejun Heo trace_writeback_dirty_inode_start(inode, flags); 232003ba3782SJens Axboe if (sb->s_op->dirty_inode) 2321a38ed483SEric Biggers sb->s_op->dirty_inode(inode, flags & I_DIRTY_INODE); 23229fb0a7daSTejun Heo trace_writeback_dirty_inode(inode, flags); 2323e2728c56SEric Biggers 232435d14f27SEric Biggers /* I_DIRTY_INODE supersedes I_DIRTY_TIME. */ 23250ae45f63STheodore Ts'o flags &= ~I_DIRTY_TIME; 232635d14f27SEric Biggers } else { 232735d14f27SEric Biggers /* 232835d14f27SEric Biggers * Else it's either I_DIRTY_PAGES, I_DIRTY_TIME, or nothing. 232935d14f27SEric Biggers * (We don't support setting both I_DIRTY_PAGES and I_DIRTY_TIME 233035d14f27SEric Biggers * in one call to __mark_inode_dirty().) 233135d14f27SEric Biggers */ 23320ae45f63STheodore Ts'o dirtytime = flags & I_DIRTY_TIME; 233335d14f27SEric Biggers WARN_ON_ONCE(dirtytime && flags != I_DIRTY_TIME); 233435d14f27SEric Biggers } 233503ba3782SJens Axboe 233603ba3782SJens Axboe /* 23379c6ac78eSTejun Heo * Paired with smp_mb() in __writeback_single_inode() for the 23389c6ac78eSTejun Heo * following lockless i_state test. See there for details. 233903ba3782SJens Axboe */ 234003ba3782SJens Axboe smp_mb(); 234103ba3782SJens Axboe 23420ae45f63STheodore Ts'o if (((inode->i_state & flags) == flags) || 23430ae45f63STheodore Ts'o (dirtytime && (inode->i_state & I_DIRTY_INODE))) 234403ba3782SJens Axboe return; 234503ba3782SJens Axboe 234603ba3782SJens Axboe if (unlikely(block_dump)) 234703ba3782SJens Axboe block_dump___mark_inode_dirty(inode); 234803ba3782SJens Axboe 2349250df6edSDave Chinner spin_lock(&inode->i_lock); 23500ae45f63STheodore Ts'o if (dirtytime && (inode->i_state & I_DIRTY_INODE)) 23510ae45f63STheodore Ts'o goto out_unlock_inode; 235203ba3782SJens Axboe if ((inode->i_state & flags) != flags) { 235303ba3782SJens Axboe const int was_dirty = inode->i_state & I_DIRTY; 235403ba3782SJens Axboe 235552ebea74STejun Heo inode_attach_wb(inode, NULL); 235652ebea74STejun Heo 235735d14f27SEric Biggers /* I_DIRTY_INODE supersedes I_DIRTY_TIME. */ 23580ae45f63STheodore Ts'o if (flags & I_DIRTY_INODE) 23590ae45f63STheodore Ts'o inode->i_state &= ~I_DIRTY_TIME; 236003ba3782SJens Axboe inode->i_state |= flags; 236103ba3782SJens Axboe 236203ba3782SJens Axboe /* 23635afced3bSJan Kara * If the inode is queued for writeback by flush worker, just 23645afced3bSJan Kara * update its dirty state. Once the flush worker is done with 23655afced3bSJan Kara * the inode it will place it on the appropriate superblock 23665afced3bSJan Kara * list, based upon its state. 236703ba3782SJens Axboe */ 23685afced3bSJan Kara if (inode->i_state & I_SYNC_QUEUED) 2369250df6edSDave Chinner goto out_unlock_inode; 237003ba3782SJens Axboe 237103ba3782SJens Axboe /* 237203ba3782SJens Axboe * Only add valid (hashed) inodes to the superblock's 237303ba3782SJens Axboe * dirty list. Add blockdev inodes as well. 237403ba3782SJens Axboe */ 237503ba3782SJens Axboe if (!S_ISBLK(inode->i_mode)) { 23761d3382cbSAl Viro if (inode_unhashed(inode)) 2377250df6edSDave Chinner goto out_unlock_inode; 237803ba3782SJens Axboe } 2379a4ffdde6SAl Viro if (inode->i_state & I_FREEING) 2380250df6edSDave Chinner goto out_unlock_inode; 238103ba3782SJens Axboe 238203ba3782SJens Axboe /* 238303ba3782SJens Axboe * If the inode was already on b_dirty/b_io/b_more_io, don't 238403ba3782SJens Axboe * reposition it (that would break b_dirty time-ordering). 238503ba3782SJens Axboe */ 238603ba3782SJens Axboe if (!was_dirty) { 238787e1d789STejun Heo struct bdi_writeback *wb; 2388d6c10f1fSTejun Heo struct list_head *dirty_list; 2389a66979abSDave Chinner bool wakeup_bdi = false; 2390500b067cSJens Axboe 239187e1d789STejun Heo wb = locked_inode_to_wb_and_lock_list(inode); 2392253c34e9SArtem Bityutskiy 239303ba3782SJens Axboe inode->dirtied_when = jiffies; 2394a2f48706STheodore Ts'o if (dirtytime) 2395a2f48706STheodore Ts'o inode->dirtied_time_when = jiffies; 2396d6c10f1fSTejun Heo 23970e11f644SChristoph Hellwig if (inode->i_state & I_DIRTY) 23980747259dSTejun Heo dirty_list = &wb->b_dirty; 2399a2f48706STheodore Ts'o else 24000747259dSTejun Heo dirty_list = &wb->b_dirty_time; 2401d6c10f1fSTejun Heo 2402c7f54084SDave Chinner wakeup_bdi = inode_io_list_move_locked(inode, wb, 2403d6c10f1fSTejun Heo dirty_list); 2404d6c10f1fSTejun Heo 24050747259dSTejun Heo spin_unlock(&wb->list_lock); 24060ae45f63STheodore Ts'o trace_writeback_dirty_inode_enqueue(inode); 2407253c34e9SArtem Bityutskiy 2408d6c10f1fSTejun Heo /* 2409d6c10f1fSTejun Heo * If this is the first dirty inode for this bdi, 2410d6c10f1fSTejun Heo * we have to wake-up the corresponding bdi thread 2411d6c10f1fSTejun Heo * to make sure background write-back happens 2412d6c10f1fSTejun Heo * later. 2413d6c10f1fSTejun Heo */ 2414f56753acSChristoph Hellwig if (wakeup_bdi && 2415f56753acSChristoph Hellwig (wb->bdi->capabilities & BDI_CAP_WRITEBACK)) 24160747259dSTejun Heo wb_wakeup_delayed(wb); 2417a66979abSDave Chinner return; 2418a66979abSDave Chinner } 2419a66979abSDave Chinner } 2420a66979abSDave Chinner out_unlock_inode: 2421a66979abSDave Chinner spin_unlock(&inode->i_lock); 242203ba3782SJens Axboe } 242303ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty); 242403ba3782SJens Axboe 2425e97fedb9SDave Chinner /* 2426e97fedb9SDave Chinner * The @s_sync_lock is used to serialise concurrent sync operations 2427e97fedb9SDave Chinner * to avoid lock contention problems with concurrent wait_sb_inodes() calls. 2428e97fedb9SDave Chinner * Concurrent callers will block on the s_sync_lock rather than doing contending 2429e97fedb9SDave Chinner * walks. The queueing maintains sync(2) required behaviour as all the IO that 2430e97fedb9SDave Chinner * has been issued up to the time this function is enter is guaranteed to be 2431e97fedb9SDave Chinner * completed by the time we have gained the lock and waited for all IO that is 2432e97fedb9SDave Chinner * in progress regardless of the order callers are granted the lock. 2433e97fedb9SDave Chinner */ 2434b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb) 243566f3b8e2SJens Axboe { 24366c60d2b5SDave Chinner LIST_HEAD(sync_list); 243738f21977SNick Piggin 243803ba3782SJens Axboe /* 243903ba3782SJens Axboe * We need to be protected against the filesystem going from 244003ba3782SJens Axboe * r/o to r/w or vice versa. 244103ba3782SJens Axboe */ 2442b6e51316SJens Axboe WARN_ON(!rwsem_is_locked(&sb->s_umount)); 244303ba3782SJens Axboe 2444e97fedb9SDave Chinner mutex_lock(&sb->s_sync_lock); 244566f3b8e2SJens Axboe 244638f21977SNick Piggin /* 24476c60d2b5SDave Chinner * Splice the writeback list onto a temporary list to avoid waiting on 24486c60d2b5SDave Chinner * inodes that have started writeback after this point. 24496c60d2b5SDave Chinner * 24506c60d2b5SDave Chinner * Use rcu_read_lock() to keep the inodes around until we have a 24516c60d2b5SDave Chinner * reference. s_inode_wblist_lock protects sb->s_inodes_wb as well as 24526c60d2b5SDave Chinner * the local list because inodes can be dropped from either by writeback 24536c60d2b5SDave Chinner * completion. 245438f21977SNick Piggin */ 24556c60d2b5SDave Chinner rcu_read_lock(); 24566c60d2b5SDave Chinner spin_lock_irq(&sb->s_inode_wblist_lock); 24576c60d2b5SDave Chinner list_splice_init(&sb->s_inodes_wb, &sync_list); 24586c60d2b5SDave Chinner 24596c60d2b5SDave Chinner /* 24606c60d2b5SDave Chinner * Data integrity sync. Must wait for all pages under writeback, because 24616c60d2b5SDave Chinner * there may have been pages dirtied before our sync call, but which had 24626c60d2b5SDave Chinner * writeout started before we write it out. In which case, the inode 24636c60d2b5SDave Chinner * may not be on the dirty list, but we still have to wait for that 24646c60d2b5SDave Chinner * writeout. 24656c60d2b5SDave Chinner */ 24666c60d2b5SDave Chinner while (!list_empty(&sync_list)) { 24676c60d2b5SDave Chinner struct inode *inode = list_first_entry(&sync_list, struct inode, 24686c60d2b5SDave Chinner i_wb_list); 2469250df6edSDave Chinner struct address_space *mapping = inode->i_mapping; 247038f21977SNick Piggin 24716c60d2b5SDave Chinner /* 24726c60d2b5SDave Chinner * Move each inode back to the wb list before we drop the lock 24736c60d2b5SDave Chinner * to preserve consistency between i_wb_list and the mapping 24746c60d2b5SDave Chinner * writeback tag. Writeback completion is responsible to remove 24756c60d2b5SDave Chinner * the inode from either list once the writeback tag is cleared. 24766c60d2b5SDave Chinner */ 24776c60d2b5SDave Chinner list_move_tail(&inode->i_wb_list, &sb->s_inodes_wb); 24786c60d2b5SDave Chinner 24796c60d2b5SDave Chinner /* 24806c60d2b5SDave Chinner * The mapping can appear untagged while still on-list since we 24816c60d2b5SDave Chinner * do not have the mapping lock. Skip it here, wb completion 24826c60d2b5SDave Chinner * will remove it. 24836c60d2b5SDave Chinner */ 24846c60d2b5SDave Chinner if (!mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK)) 24856c60d2b5SDave Chinner continue; 24866c60d2b5SDave Chinner 24876c60d2b5SDave Chinner spin_unlock_irq(&sb->s_inode_wblist_lock); 24886c60d2b5SDave Chinner 2489250df6edSDave Chinner spin_lock(&inode->i_lock); 24906c60d2b5SDave Chinner if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) { 2491250df6edSDave Chinner spin_unlock(&inode->i_lock); 24926c60d2b5SDave Chinner 24936c60d2b5SDave Chinner spin_lock_irq(&sb->s_inode_wblist_lock); 249438f21977SNick Piggin continue; 2495250df6edSDave Chinner } 249638f21977SNick Piggin __iget(inode); 2497250df6edSDave Chinner spin_unlock(&inode->i_lock); 24986c60d2b5SDave Chinner rcu_read_unlock(); 249938f21977SNick Piggin 2500aa750fd7SJunichi Nomura /* 2501aa750fd7SJunichi Nomura * We keep the error status of individual mapping so that 2502aa750fd7SJunichi Nomura * applications can catch the writeback error using fsync(2). 2503aa750fd7SJunichi Nomura * See filemap_fdatawait_keep_errors() for details. 2504aa750fd7SJunichi Nomura */ 2505aa750fd7SJunichi Nomura filemap_fdatawait_keep_errors(mapping); 250638f21977SNick Piggin 250738f21977SNick Piggin cond_resched(); 250838f21977SNick Piggin 25096c60d2b5SDave Chinner iput(inode); 25106c60d2b5SDave Chinner 25116c60d2b5SDave Chinner rcu_read_lock(); 25126c60d2b5SDave Chinner spin_lock_irq(&sb->s_inode_wblist_lock); 251338f21977SNick Piggin } 25146c60d2b5SDave Chinner spin_unlock_irq(&sb->s_inode_wblist_lock); 25156c60d2b5SDave Chinner rcu_read_unlock(); 2516e97fedb9SDave Chinner mutex_unlock(&sb->s_sync_lock); 251766f3b8e2SJens Axboe } 25181da177e4SLinus Torvalds 2519f30a7d0cSTejun Heo static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr, 2520f30a7d0cSTejun Heo enum wb_reason reason, bool skip_if_busy) 25211da177e4SLinus Torvalds { 25225b9cce4cSTejun Heo struct backing_dev_info *bdi = sb->s_bdi; 25235b9cce4cSTejun Heo DEFINE_WB_COMPLETION(done, bdi); 252483ba7b07SChristoph Hellwig struct wb_writeback_work work = { 25253c4d7165SChristoph Hellwig .sb = sb, 25263c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_NONE, 25276e6938b6SWu Fengguang .tagged_writepages = 1, 252883ba7b07SChristoph Hellwig .done = &done, 25293259f8beSChris Mason .nr_pages = nr, 25300e175a18SCurt Wohlgemuth .reason = reason, 25313c4d7165SChristoph Hellwig }; 25320e3c9a22SJens Axboe 2533e7972912STejun Heo if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info) 25346eedc701SJan Kara return; 2535cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 2536f30a7d0cSTejun Heo 2537db125360STejun Heo bdi_split_work_to_wbs(sb->s_bdi, &work, skip_if_busy); 25385b9cce4cSTejun Heo wb_wait_for_completion(&done); 25391da177e4SLinus Torvalds } 2540f30a7d0cSTejun Heo 2541f30a7d0cSTejun Heo /** 2542f30a7d0cSTejun Heo * writeback_inodes_sb_nr - writeback dirty inodes from given super_block 2543f30a7d0cSTejun Heo * @sb: the superblock 2544f30a7d0cSTejun Heo * @nr: the number of pages to write 2545f30a7d0cSTejun Heo * @reason: reason why some writeback work initiated 2546f30a7d0cSTejun Heo * 2547f30a7d0cSTejun Heo * Start writeback on some inodes on this super_block. No guarantees are made 2548f30a7d0cSTejun Heo * on how many (if any) will be written, and this function does not wait 2549f30a7d0cSTejun Heo * for IO completion of submitted IO. 2550f30a7d0cSTejun Heo */ 2551f30a7d0cSTejun Heo void writeback_inodes_sb_nr(struct super_block *sb, 2552f30a7d0cSTejun Heo unsigned long nr, 2553f30a7d0cSTejun Heo enum wb_reason reason) 2554f30a7d0cSTejun Heo { 2555f30a7d0cSTejun Heo __writeback_inodes_sb_nr(sb, nr, reason, false); 2556f30a7d0cSTejun Heo } 25573259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr); 25583259f8beSChris Mason 25593259f8beSChris Mason /** 25603259f8beSChris Mason * writeback_inodes_sb - writeback dirty inodes from given super_block 25613259f8beSChris Mason * @sb: the superblock 2562786228abSMarcos Paulo de Souza * @reason: reason why some writeback work was initiated 25633259f8beSChris Mason * 25643259f8beSChris Mason * Start writeback on some inodes on this super_block. No guarantees are made 25653259f8beSChris Mason * on how many (if any) will be written, and this function does not wait 25663259f8beSChris Mason * for IO completion of submitted IO. 25673259f8beSChris Mason */ 25680e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 25693259f8beSChris Mason { 25700e175a18SCurt Wohlgemuth return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); 25713259f8beSChris Mason } 2572d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb); 2573d8a8559cSJens Axboe 2574d8a8559cSJens Axboe /** 257510ee27a0SMiao Xie * try_to_writeback_inodes_sb - try to start writeback if none underway 257610ee27a0SMiao Xie * @sb: the superblock 257710ee27a0SMiao Xie * @reason: reason why some writeback work was initiated 257810ee27a0SMiao Xie * 25798264c321SRakesh Pandit * Invoke __writeback_inodes_sb_nr if no writeback is currently underway. 258010ee27a0SMiao Xie */ 25818264c321SRakesh Pandit void try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 258210ee27a0SMiao Xie { 25838264c321SRakesh Pandit if (!down_read_trylock(&sb->s_umount)) 25848264c321SRakesh Pandit return; 25858264c321SRakesh Pandit 25868264c321SRakesh Pandit __writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason, true); 25878264c321SRakesh Pandit up_read(&sb->s_umount); 258810ee27a0SMiao Xie } 258910ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb); 25903259f8beSChris Mason 25913259f8beSChris Mason /** 2592d8a8559cSJens Axboe * sync_inodes_sb - sync sb inode pages 2593d8a8559cSJens Axboe * @sb: the superblock 2594d8a8559cSJens Axboe * 2595d8a8559cSJens Axboe * This function writes and waits on any dirty inode belonging to this 25960dc83bd3SJan Kara * super_block. 2597d8a8559cSJens Axboe */ 25980dc83bd3SJan Kara void sync_inodes_sb(struct super_block *sb) 2599d8a8559cSJens Axboe { 26005b9cce4cSTejun Heo struct backing_dev_info *bdi = sb->s_bdi; 26015b9cce4cSTejun Heo DEFINE_WB_COMPLETION(done, bdi); 260283ba7b07SChristoph Hellwig struct wb_writeback_work work = { 26033c4d7165SChristoph Hellwig .sb = sb, 26043c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_ALL, 26053c4d7165SChristoph Hellwig .nr_pages = LONG_MAX, 26063c4d7165SChristoph Hellwig .range_cyclic = 0, 260783ba7b07SChristoph Hellwig .done = &done, 26080e175a18SCurt Wohlgemuth .reason = WB_REASON_SYNC, 26097747bd4bSDave Chinner .for_sync = 1, 26103c4d7165SChristoph Hellwig }; 26113c4d7165SChristoph Hellwig 2612006a0973STejun Heo /* 2613006a0973STejun Heo * Can't skip on !bdi_has_dirty() because we should wait for !dirty 2614006a0973STejun Heo * inodes under writeback and I_DIRTY_TIME inodes ignored by 2615006a0973STejun Heo * bdi_has_dirty() need to be written out too. 2616006a0973STejun Heo */ 2617006a0973STejun Heo if (bdi == &noop_backing_dev_info) 26186eedc701SJan Kara return; 2619cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 2620cf37e972SChristoph Hellwig 26217fc5854fSTejun Heo /* protect against inode wb switch, see inode_switch_wbs_work_fn() */ 26227fc5854fSTejun Heo bdi_down_write_wb_switch_rwsem(bdi); 2623db125360STejun Heo bdi_split_work_to_wbs(bdi, &work, false); 26245b9cce4cSTejun Heo wb_wait_for_completion(&done); 26257fc5854fSTejun Heo bdi_up_write_wb_switch_rwsem(bdi); 262683ba7b07SChristoph Hellwig 2627b6e51316SJens Axboe wait_sb_inodes(sb); 2628d8a8559cSJens Axboe } 2629d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb); 26301da177e4SLinus Torvalds 26311da177e4SLinus Torvalds /** 26321da177e4SLinus Torvalds * write_inode_now - write an inode to disk 26331da177e4SLinus Torvalds * @inode: inode to write to disk 26341da177e4SLinus Torvalds * @sync: whether the write should be synchronous or not 26351da177e4SLinus Torvalds * 26367f04c26dSAndrea Arcangeli * This function commits an inode to disk immediately if it is dirty. This is 26377f04c26dSAndrea Arcangeli * primarily needed by knfsd. 26387f04c26dSAndrea Arcangeli * 26397f04c26dSAndrea Arcangeli * The caller must either have a ref on the inode or must have set I_WILL_FREE. 26401da177e4SLinus Torvalds */ 26411da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync) 26421da177e4SLinus Torvalds { 26431da177e4SLinus Torvalds struct writeback_control wbc = { 26441da177e4SLinus Torvalds .nr_to_write = LONG_MAX, 264518914b18SMike Galbraith .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 2646111ebb6eSOGAWA Hirofumi .range_start = 0, 2647111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 26481da177e4SLinus Torvalds }; 26491da177e4SLinus Torvalds 2650f56753acSChristoph Hellwig if (!mapping_can_writeback(inode->i_mapping)) 265149364ce2SAndrew Morton wbc.nr_to_write = 0; 26521da177e4SLinus Torvalds 26531da177e4SLinus Torvalds might_sleep(); 2654aaf25593STejun Heo return writeback_single_inode(inode, &wbc); 26551da177e4SLinus Torvalds } 26561da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now); 26571da177e4SLinus Torvalds 26581da177e4SLinus Torvalds /** 26591da177e4SLinus Torvalds * sync_inode - write an inode and its pages to disk. 26601da177e4SLinus Torvalds * @inode: the inode to sync 26611da177e4SLinus Torvalds * @wbc: controls the writeback mode 26621da177e4SLinus Torvalds * 26631da177e4SLinus Torvalds * sync_inode() will write an inode and its pages to disk. It will also 26641da177e4SLinus Torvalds * correctly update the inode on its superblock's dirty inode lists and will 26651da177e4SLinus Torvalds * update inode->i_state. 26661da177e4SLinus Torvalds * 26671da177e4SLinus Torvalds * The caller must have a ref on the inode. 26681da177e4SLinus Torvalds */ 26691da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc) 26701da177e4SLinus Torvalds { 2671aaf25593STejun Heo return writeback_single_inode(inode, wbc); 26721da177e4SLinus Torvalds } 26731da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode); 2674c3765016SChristoph Hellwig 2675c3765016SChristoph Hellwig /** 2676c691b9d9SAndrew Morton * sync_inode_metadata - write an inode to disk 2677c3765016SChristoph Hellwig * @inode: the inode to sync 2678c3765016SChristoph Hellwig * @wait: wait for I/O to complete. 2679c3765016SChristoph Hellwig * 2680c691b9d9SAndrew Morton * Write an inode to disk and adjust its dirty state after completion. 2681c3765016SChristoph Hellwig * 2682c3765016SChristoph Hellwig * Note: only writes the actual inode, no associated data or other metadata. 2683c3765016SChristoph Hellwig */ 2684c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait) 2685c3765016SChristoph Hellwig { 2686c3765016SChristoph Hellwig struct writeback_control wbc = { 2687c3765016SChristoph Hellwig .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, 2688c3765016SChristoph Hellwig .nr_to_write = 0, /* metadata-only */ 2689c3765016SChristoph Hellwig }; 2690c3765016SChristoph Hellwig 2691c3765016SChristoph Hellwig return sync_inode(inode, &wbc); 2692c3765016SChristoph Hellwig } 2693c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata); 2694