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; 450dc83bd3SJan Kara unsigned long *older_than_this; 46c4a77a6cSJens Axboe enum writeback_sync_modes sync_mode; 476e6938b6SWu Fengguang unsigned int tagged_writepages:1; 4852957fe1SH Hartley Sweeten unsigned int for_kupdate:1; 4952957fe1SH Hartley Sweeten unsigned int range_cyclic:1; 5052957fe1SH Hartley Sweeten unsigned int for_background:1; 517747bd4bSDave Chinner unsigned int for_sync:1; /* sync(2) WB_SYNC_ALL writeback */ 52ac7b19a3STejun Heo unsigned int auto_free:1; /* free on completion */ 530e175a18SCurt Wohlgemuth enum wb_reason reason; /* why was writeback initiated? */ 54c4a77a6cSJens Axboe 558010c3b6SJens Axboe struct list_head list; /* pending work list */ 56cc395d7fSTejun Heo struct wb_completion *done; /* set if the caller waits */ 5703ba3782SJens Axboe }; 5803ba3782SJens Axboe 59a2f48706STheodore Ts'o /* 60a2f48706STheodore Ts'o * If an inode is constantly having its pages dirtied, but then the 61a2f48706STheodore Ts'o * updates stop dirtytime_expire_interval seconds in the past, it's 62a2f48706STheodore Ts'o * possible for the worst case time between when an inode has its 63a2f48706STheodore Ts'o * timestamps updated and when they finally get written out to be two 64a2f48706STheodore Ts'o * dirtytime_expire_intervals. We set the default to 12 hours (in 65a2f48706STheodore Ts'o * seconds), which means most of the time inodes will have their 66a2f48706STheodore Ts'o * timestamps written to disk after 12 hours, but in the worst case a 67a2f48706STheodore Ts'o * few inodes might not their timestamps updated for 24 hours. 68a2f48706STheodore Ts'o */ 69a2f48706STheodore Ts'o unsigned int dirtytime_expire_interval = 12 * 60 * 60; 70a2f48706STheodore Ts'o 717ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head) 727ccf19a8SNick Piggin { 73c7f54084SDave Chinner return list_entry(head, struct inode, i_io_list); 747ccf19a8SNick Piggin } 757ccf19a8SNick Piggin 7615eb77a0SWu Fengguang /* 7715eb77a0SWu Fengguang * Include the creation of the trace points after defining the 7815eb77a0SWu Fengguang * wb_writeback_work structure and inline functions so that the definition 7915eb77a0SWu Fengguang * remains local to this file. 8015eb77a0SWu Fengguang */ 8115eb77a0SWu Fengguang #define CREATE_TRACE_POINTS 8215eb77a0SWu Fengguang #include <trace/events/writeback.h> 8315eb77a0SWu Fengguang 84774016b2SSteven Whitehouse EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage); 85774016b2SSteven Whitehouse 86d6c10f1fSTejun Heo static bool wb_io_lists_populated(struct bdi_writeback *wb) 87d6c10f1fSTejun Heo { 88d6c10f1fSTejun Heo if (wb_has_dirty_io(wb)) { 89d6c10f1fSTejun Heo return false; 90d6c10f1fSTejun Heo } else { 91d6c10f1fSTejun Heo set_bit(WB_has_dirty_io, &wb->state); 9295a46c65STejun Heo WARN_ON_ONCE(!wb->avg_write_bandwidth); 93766a9d6eSTejun Heo atomic_long_add(wb->avg_write_bandwidth, 94766a9d6eSTejun Heo &wb->bdi->tot_write_bandwidth); 95d6c10f1fSTejun Heo return true; 96d6c10f1fSTejun Heo } 97d6c10f1fSTejun Heo } 98d6c10f1fSTejun Heo 99d6c10f1fSTejun Heo static void wb_io_lists_depopulated(struct bdi_writeback *wb) 100d6c10f1fSTejun Heo { 101d6c10f1fSTejun Heo if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) && 102766a9d6eSTejun Heo list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) { 103d6c10f1fSTejun Heo clear_bit(WB_has_dirty_io, &wb->state); 10495a46c65STejun Heo WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth, 10595a46c65STejun Heo &wb->bdi->tot_write_bandwidth) < 0); 106766a9d6eSTejun Heo } 107d6c10f1fSTejun Heo } 108d6c10f1fSTejun Heo 109d6c10f1fSTejun Heo /** 110c7f54084SDave Chinner * inode_io_list_move_locked - move an inode onto a bdi_writeback IO list 111d6c10f1fSTejun Heo * @inode: inode to be moved 112d6c10f1fSTejun Heo * @wb: target bdi_writeback 113bbbc3c1cSWang Long * @head: one of @wb->b_{dirty|io|more_io|dirty_time} 114d6c10f1fSTejun Heo * 115c7f54084SDave Chinner * Move @inode->i_io_list to @list of @wb and set %WB_has_dirty_io. 116d6c10f1fSTejun Heo * Returns %true if @inode is the first occupant of the !dirty_time IO 117d6c10f1fSTejun Heo * lists; otherwise, %false. 118d6c10f1fSTejun Heo */ 119c7f54084SDave Chinner static bool inode_io_list_move_locked(struct inode *inode, 120d6c10f1fSTejun Heo struct bdi_writeback *wb, 121d6c10f1fSTejun Heo struct list_head *head) 122d6c10f1fSTejun Heo { 123d6c10f1fSTejun Heo assert_spin_locked(&wb->list_lock); 124d6c10f1fSTejun Heo 125c7f54084SDave Chinner list_move(&inode->i_io_list, head); 126d6c10f1fSTejun Heo 127d6c10f1fSTejun Heo /* dirty_time doesn't count as dirty_io until expiration */ 128d6c10f1fSTejun Heo if (head != &wb->b_dirty_time) 129d6c10f1fSTejun Heo return wb_io_lists_populated(wb); 130d6c10f1fSTejun Heo 131d6c10f1fSTejun Heo wb_io_lists_depopulated(wb); 132d6c10f1fSTejun Heo return false; 133d6c10f1fSTejun Heo } 134d6c10f1fSTejun Heo 135d6c10f1fSTejun Heo /** 136c7f54084SDave Chinner * inode_io_list_del_locked - remove an inode from its bdi_writeback IO list 137d6c10f1fSTejun Heo * @inode: inode to be removed 138d6c10f1fSTejun Heo * @wb: bdi_writeback @inode is being removed from 139d6c10f1fSTejun Heo * 140d6c10f1fSTejun Heo * Remove @inode which may be on one of @wb->b_{dirty|io|more_io} lists and 141d6c10f1fSTejun Heo * clear %WB_has_dirty_io if all are empty afterwards. 142d6c10f1fSTejun Heo */ 143c7f54084SDave Chinner static void inode_io_list_del_locked(struct inode *inode, 144d6c10f1fSTejun Heo struct bdi_writeback *wb) 145d6c10f1fSTejun Heo { 146d6c10f1fSTejun Heo assert_spin_locked(&wb->list_lock); 147d6c10f1fSTejun Heo 148c7f54084SDave Chinner list_del_init(&inode->i_io_list); 149d6c10f1fSTejun Heo wb_io_lists_depopulated(wb); 150d6c10f1fSTejun Heo } 151d6c10f1fSTejun Heo 152f0054bb1STejun Heo static void wb_wakeup(struct bdi_writeback *wb) 1535acda9d1SJan Kara { 154f0054bb1STejun Heo spin_lock_bh(&wb->work_lock); 155f0054bb1STejun Heo if (test_bit(WB_registered, &wb->state)) 156f0054bb1STejun Heo mod_delayed_work(bdi_wq, &wb->dwork, 0); 157f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 1585acda9d1SJan Kara } 1595acda9d1SJan Kara 1604a3a485bSTahsin Erdogan static void finish_writeback_work(struct bdi_writeback *wb, 1614a3a485bSTahsin Erdogan struct wb_writeback_work *work) 1624a3a485bSTahsin Erdogan { 1634a3a485bSTahsin Erdogan struct wb_completion *done = work->done; 1644a3a485bSTahsin Erdogan 1654a3a485bSTahsin Erdogan if (work->auto_free) 1664a3a485bSTahsin Erdogan kfree(work); 1678e00c4e9STejun Heo if (done) { 1688e00c4e9STejun Heo wait_queue_head_t *waitq = done->waitq; 1698e00c4e9STejun Heo 1708e00c4e9STejun Heo /* @done can't be accessed after the following dec */ 1718e00c4e9STejun Heo if (atomic_dec_and_test(&done->cnt)) 1728e00c4e9STejun Heo wake_up_all(waitq); 1738e00c4e9STejun Heo } 1744a3a485bSTahsin Erdogan } 1754a3a485bSTahsin Erdogan 176f0054bb1STejun Heo static void wb_queue_work(struct bdi_writeback *wb, 1776585027aSJan Kara struct wb_writeback_work *work) 1786585027aSJan Kara { 1795634cc2aSTejun Heo trace_writeback_queue(wb, work); 1806585027aSJan Kara 181cc395d7fSTejun Heo if (work->done) 182cc395d7fSTejun Heo atomic_inc(&work->done->cnt); 1834a3a485bSTahsin Erdogan 1844a3a485bSTahsin Erdogan spin_lock_bh(&wb->work_lock); 1854a3a485bSTahsin Erdogan 1864a3a485bSTahsin Erdogan if (test_bit(WB_registered, &wb->state)) { 187f0054bb1STejun Heo list_add_tail(&work->list, &wb->work_list); 188f0054bb1STejun Heo mod_delayed_work(bdi_wq, &wb->dwork, 0); 1894a3a485bSTahsin Erdogan } else 1904a3a485bSTahsin Erdogan finish_writeback_work(wb, work); 1914a3a485bSTahsin Erdogan 192f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 19303ba3782SJens Axboe } 1941da177e4SLinus Torvalds 195cc395d7fSTejun Heo /** 196cc395d7fSTejun Heo * wb_wait_for_completion - wait for completion of bdi_writeback_works 197cc395d7fSTejun Heo * @done: target wb_completion 198cc395d7fSTejun Heo * 199cc395d7fSTejun Heo * Wait for one or more work items issued to @bdi with their ->done field 2005b9cce4cSTejun Heo * set to @done, which should have been initialized with 2015b9cce4cSTejun Heo * DEFINE_WB_COMPLETION(). This function returns after all such work items 2025b9cce4cSTejun Heo * are completed. Work items which are waited upon aren't freed 203cc395d7fSTejun Heo * automatically on completion. 204cc395d7fSTejun Heo */ 2055b9cce4cSTejun Heo void wb_wait_for_completion(struct wb_completion *done) 206cc395d7fSTejun Heo { 207cc395d7fSTejun Heo atomic_dec(&done->cnt); /* put down the initial count */ 2085b9cce4cSTejun Heo wait_event(*done->waitq, !atomic_read(&done->cnt)); 209cc395d7fSTejun Heo } 210cc395d7fSTejun Heo 211703c2708STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 212703c2708STejun Heo 21355a694dfSTejun Heo /* 21455a694dfSTejun Heo * Parameters for foreign inode detection, see wbc_detach_inode() to see 21555a694dfSTejun Heo * how they're used. 21655a694dfSTejun Heo * 21755a694dfSTejun Heo * These paramters are inherently heuristical as the detection target 21855a694dfSTejun Heo * itself is fuzzy. All we want to do is detaching an inode from the 21955a694dfSTejun Heo * current owner if it's being written to by some other cgroups too much. 22055a694dfSTejun Heo * 22155a694dfSTejun Heo * The current cgroup writeback is built on the assumption that multiple 22255a694dfSTejun Heo * cgroups writing to the same inode concurrently is very rare and a mode 22355a694dfSTejun Heo * of operation which isn't well supported. As such, the goal is not 22455a694dfSTejun Heo * taking too long when a different cgroup takes over an inode while 22555a694dfSTejun Heo * avoiding too aggressive flip-flops from occasional foreign writes. 22655a694dfSTejun Heo * 22755a694dfSTejun Heo * We record, very roughly, 2s worth of IO time history and if more than 22855a694dfSTejun Heo * half of that is foreign, trigger the switch. The recording is quantized 22955a694dfSTejun Heo * to 16 slots. To avoid tiny writes from swinging the decision too much, 23055a694dfSTejun Heo * writes smaller than 1/8 of avg size are ignored. 23155a694dfSTejun Heo */ 2322a814908STejun Heo #define WB_FRN_TIME_SHIFT 13 /* 1s = 2^13, upto 8 secs w/ 16bit */ 2332a814908STejun Heo #define WB_FRN_TIME_AVG_SHIFT 3 /* avg = avg * 7/8 + new * 1/8 */ 23455a694dfSTejun Heo #define WB_FRN_TIME_CUT_DIV 8 /* ignore rounds < avg / 8 */ 2352a814908STejun Heo #define WB_FRN_TIME_PERIOD (2 * (1 << WB_FRN_TIME_SHIFT)) /* 2s */ 2362a814908STejun Heo 2372a814908STejun Heo #define WB_FRN_HIST_SLOTS 16 /* inode->i_wb_frn_history is 16bit */ 2382a814908STejun Heo #define WB_FRN_HIST_UNIT (WB_FRN_TIME_PERIOD / WB_FRN_HIST_SLOTS) 2392a814908STejun Heo /* each slot's duration is 2s / 16 */ 2402a814908STejun Heo #define WB_FRN_HIST_THR_SLOTS (WB_FRN_HIST_SLOTS / 2) 2412a814908STejun Heo /* if foreign slots >= 8, switch */ 2422a814908STejun Heo #define WB_FRN_HIST_MAX_SLOTS (WB_FRN_HIST_THR_SLOTS / 2 + 1) 2432a814908STejun Heo /* one round can affect upto 5 slots */ 2446444f47eSTejun Heo #define WB_FRN_MAX_IN_FLIGHT 1024 /* don't queue too many concurrently */ 2452a814908STejun Heo 246a1a0e23eSTejun Heo static atomic_t isw_nr_in_flight = ATOMIC_INIT(0); 247a1a0e23eSTejun Heo static struct workqueue_struct *isw_wq; 248a1a0e23eSTejun Heo 24921c6321fSTejun Heo void __inode_attach_wb(struct inode *inode, struct page *page) 25021c6321fSTejun Heo { 25121c6321fSTejun Heo struct backing_dev_info *bdi = inode_to_bdi(inode); 25221c6321fSTejun Heo struct bdi_writeback *wb = NULL; 25321c6321fSTejun Heo 25421c6321fSTejun Heo if (inode_cgwb_enabled(inode)) { 25521c6321fSTejun Heo struct cgroup_subsys_state *memcg_css; 25621c6321fSTejun Heo 25721c6321fSTejun Heo if (page) { 25821c6321fSTejun Heo memcg_css = mem_cgroup_css_from_page(page); 25921c6321fSTejun Heo wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 26021c6321fSTejun Heo } else { 26121c6321fSTejun Heo /* must pin memcg_css, see wb_get_create() */ 26221c6321fSTejun Heo memcg_css = task_get_css(current, memory_cgrp_id); 26321c6321fSTejun Heo wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 26421c6321fSTejun Heo css_put(memcg_css); 26521c6321fSTejun Heo } 26621c6321fSTejun Heo } 26721c6321fSTejun Heo 26821c6321fSTejun Heo if (!wb) 26921c6321fSTejun Heo wb = &bdi->wb; 27021c6321fSTejun Heo 27121c6321fSTejun Heo /* 27221c6321fSTejun Heo * There may be multiple instances of this function racing to 27321c6321fSTejun Heo * update the same inode. Use cmpxchg() to tell the winner. 27421c6321fSTejun Heo */ 27521c6321fSTejun Heo if (unlikely(cmpxchg(&inode->i_wb, NULL, wb))) 27621c6321fSTejun Heo wb_put(wb); 27721c6321fSTejun Heo } 2789b0eb69bSTejun Heo EXPORT_SYMBOL_GPL(__inode_attach_wb); 27921c6321fSTejun Heo 280703c2708STejun Heo /** 28187e1d789STejun Heo * locked_inode_to_wb_and_lock_list - determine a locked inode's wb and lock it 28287e1d789STejun Heo * @inode: inode of interest with i_lock held 28387e1d789STejun Heo * 28487e1d789STejun Heo * Returns @inode's wb with its list_lock held. @inode->i_lock must be 28587e1d789STejun Heo * held on entry and is released on return. The returned wb is guaranteed 28687e1d789STejun Heo * to stay @inode's associated wb until its list_lock is released. 28787e1d789STejun Heo */ 28887e1d789STejun Heo static struct bdi_writeback * 28987e1d789STejun Heo locked_inode_to_wb_and_lock_list(struct inode *inode) 29087e1d789STejun Heo __releases(&inode->i_lock) 29187e1d789STejun Heo __acquires(&wb->list_lock) 29287e1d789STejun Heo { 29387e1d789STejun Heo while (true) { 29487e1d789STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 29587e1d789STejun Heo 29687e1d789STejun Heo /* 29787e1d789STejun Heo * inode_to_wb() association is protected by both 29887e1d789STejun Heo * @inode->i_lock and @wb->list_lock but list_lock nests 29987e1d789STejun Heo * outside i_lock. Drop i_lock and verify that the 30087e1d789STejun Heo * association hasn't changed after acquiring list_lock. 30187e1d789STejun Heo */ 30287e1d789STejun Heo wb_get(wb); 30387e1d789STejun Heo spin_unlock(&inode->i_lock); 30487e1d789STejun Heo spin_lock(&wb->list_lock); 30587e1d789STejun Heo 306aaa2cacfSTejun Heo /* i_wb may have changed inbetween, can't use inode_to_wb() */ 307614a4e37STejun Heo if (likely(wb == inode->i_wb)) { 308614a4e37STejun Heo wb_put(wb); /* @inode already has ref */ 309614a4e37STejun Heo return wb; 310614a4e37STejun Heo } 31187e1d789STejun Heo 31287e1d789STejun Heo spin_unlock(&wb->list_lock); 313614a4e37STejun Heo wb_put(wb); 31487e1d789STejun Heo cpu_relax(); 31587e1d789STejun Heo spin_lock(&inode->i_lock); 31687e1d789STejun Heo } 31787e1d789STejun Heo } 31887e1d789STejun Heo 31987e1d789STejun Heo /** 32087e1d789STejun Heo * inode_to_wb_and_lock_list - determine an inode's wb and lock it 32187e1d789STejun Heo * @inode: inode of interest 32287e1d789STejun Heo * 32387e1d789STejun Heo * Same as locked_inode_to_wb_and_lock_list() but @inode->i_lock isn't held 32487e1d789STejun Heo * on entry. 32587e1d789STejun Heo */ 32687e1d789STejun Heo static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode) 32787e1d789STejun Heo __acquires(&wb->list_lock) 32887e1d789STejun Heo { 32987e1d789STejun Heo spin_lock(&inode->i_lock); 33087e1d789STejun Heo return locked_inode_to_wb_and_lock_list(inode); 33187e1d789STejun Heo } 33287e1d789STejun Heo 333682aa8e1STejun Heo struct inode_switch_wbs_context { 334682aa8e1STejun Heo struct inode *inode; 335682aa8e1STejun Heo struct bdi_writeback *new_wb; 336682aa8e1STejun Heo 337682aa8e1STejun Heo struct rcu_head rcu_head; 338682aa8e1STejun Heo struct work_struct work; 339682aa8e1STejun Heo }; 340682aa8e1STejun Heo 3417fc5854fSTejun Heo static void bdi_down_write_wb_switch_rwsem(struct backing_dev_info *bdi) 3427fc5854fSTejun Heo { 3437fc5854fSTejun Heo down_write(&bdi->wb_switch_rwsem); 3447fc5854fSTejun Heo } 3457fc5854fSTejun Heo 3467fc5854fSTejun Heo static void bdi_up_write_wb_switch_rwsem(struct backing_dev_info *bdi) 3477fc5854fSTejun Heo { 3487fc5854fSTejun Heo up_write(&bdi->wb_switch_rwsem); 3497fc5854fSTejun Heo } 3507fc5854fSTejun Heo 351682aa8e1STejun Heo static void inode_switch_wbs_work_fn(struct work_struct *work) 352682aa8e1STejun Heo { 353682aa8e1STejun Heo struct inode_switch_wbs_context *isw = 354682aa8e1STejun Heo container_of(work, struct inode_switch_wbs_context, work); 355682aa8e1STejun Heo struct inode *inode = isw->inode; 3567fc5854fSTejun Heo struct backing_dev_info *bdi = inode_to_bdi(inode); 357d10c8095STejun Heo struct address_space *mapping = inode->i_mapping; 358d10c8095STejun Heo struct bdi_writeback *old_wb = inode->i_wb; 359682aa8e1STejun Heo struct bdi_writeback *new_wb = isw->new_wb; 36004edf02cSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, 0); 36104edf02cSMatthew Wilcox struct page *page; 362d10c8095STejun Heo bool switched = false; 363682aa8e1STejun Heo 364682aa8e1STejun Heo /* 3657fc5854fSTejun Heo * If @inode switches cgwb membership while sync_inodes_sb() is 3667fc5854fSTejun Heo * being issued, sync_inodes_sb() might miss it. Synchronize. 3677fc5854fSTejun Heo */ 3687fc5854fSTejun Heo down_read(&bdi->wb_switch_rwsem); 3697fc5854fSTejun Heo 3707fc5854fSTejun Heo /* 371682aa8e1STejun Heo * By the time control reaches here, RCU grace period has passed 372682aa8e1STejun Heo * since I_WB_SWITCH assertion and all wb stat update transactions 373682aa8e1STejun Heo * between unlocked_inode_to_wb_begin/end() are guaranteed to be 374b93b0163SMatthew Wilcox * synchronizing against the i_pages lock. 375d10c8095STejun Heo * 376b93b0163SMatthew Wilcox * Grabbing old_wb->list_lock, inode->i_lock and the i_pages lock 377d10c8095STejun Heo * gives us exclusion against all wb related operations on @inode 378d10c8095STejun Heo * including IO list manipulations and stat updates. 379682aa8e1STejun Heo */ 380d10c8095STejun Heo if (old_wb < new_wb) { 381d10c8095STejun Heo spin_lock(&old_wb->list_lock); 382d10c8095STejun Heo spin_lock_nested(&new_wb->list_lock, SINGLE_DEPTH_NESTING); 383d10c8095STejun Heo } else { 384d10c8095STejun Heo spin_lock(&new_wb->list_lock); 385d10c8095STejun Heo spin_lock_nested(&old_wb->list_lock, SINGLE_DEPTH_NESTING); 386d10c8095STejun Heo } 387682aa8e1STejun Heo spin_lock(&inode->i_lock); 388b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 389682aa8e1STejun Heo 390d10c8095STejun Heo /* 391d10c8095STejun Heo * Once I_FREEING is visible under i_lock, the eviction path owns 392c7f54084SDave Chinner * the inode and we shouldn't modify ->i_io_list. 393d10c8095STejun Heo */ 394d10c8095STejun Heo if (unlikely(inode->i_state & I_FREEING)) 395d10c8095STejun Heo goto skip_switch; 396d10c8095STejun Heo 3973a8e9ac8STejun Heo trace_inode_switch_wbs(inode, old_wb, new_wb); 3983a8e9ac8STejun Heo 399d10c8095STejun Heo /* 400d10c8095STejun Heo * Count and transfer stats. Note that PAGECACHE_TAG_DIRTY points 401d10c8095STejun Heo * to possibly dirty pages while PAGECACHE_TAG_WRITEBACK points to 402b93b0163SMatthew Wilcox * pages actually under writeback. 403d10c8095STejun Heo */ 40404edf02cSMatthew Wilcox xas_for_each_marked(&xas, page, ULONG_MAX, PAGECACHE_TAG_DIRTY) { 40504edf02cSMatthew Wilcox if (PageDirty(page)) { 4063e8f399dSNikolay Borisov dec_wb_stat(old_wb, WB_RECLAIMABLE); 4073e8f399dSNikolay Borisov inc_wb_stat(new_wb, WB_RECLAIMABLE); 408d10c8095STejun Heo } 409d10c8095STejun Heo } 410d10c8095STejun Heo 41104edf02cSMatthew Wilcox xas_set(&xas, 0); 41204edf02cSMatthew Wilcox xas_for_each_marked(&xas, page, ULONG_MAX, PAGECACHE_TAG_WRITEBACK) { 413d10c8095STejun Heo WARN_ON_ONCE(!PageWriteback(page)); 4143e8f399dSNikolay Borisov dec_wb_stat(old_wb, WB_WRITEBACK); 4153e8f399dSNikolay Borisov inc_wb_stat(new_wb, WB_WRITEBACK); 416d10c8095STejun Heo } 417d10c8095STejun Heo 418d10c8095STejun Heo wb_get(new_wb); 419d10c8095STejun Heo 420d10c8095STejun Heo /* 421d10c8095STejun Heo * Transfer to @new_wb's IO list if necessary. The specific list 422d10c8095STejun Heo * @inode was on is ignored and the inode is put on ->b_dirty which 423d10c8095STejun Heo * is always correct including from ->b_dirty_time. The transfer 424d10c8095STejun Heo * preserves @inode->dirtied_when ordering. 425d10c8095STejun Heo */ 426c7f54084SDave Chinner if (!list_empty(&inode->i_io_list)) { 427d10c8095STejun Heo struct inode *pos; 428d10c8095STejun Heo 429c7f54084SDave Chinner inode_io_list_del_locked(inode, old_wb); 430d10c8095STejun Heo inode->i_wb = new_wb; 431c7f54084SDave Chinner list_for_each_entry(pos, &new_wb->b_dirty, i_io_list) 432d10c8095STejun Heo if (time_after_eq(inode->dirtied_when, 433d10c8095STejun Heo pos->dirtied_when)) 434d10c8095STejun Heo break; 435c7f54084SDave Chinner inode_io_list_move_locked(inode, new_wb, pos->i_io_list.prev); 436d10c8095STejun Heo } else { 437d10c8095STejun Heo inode->i_wb = new_wb; 438d10c8095STejun Heo } 439d10c8095STejun Heo 440d10c8095STejun Heo /* ->i_wb_frn updates may race wbc_detach_inode() but doesn't matter */ 441682aa8e1STejun Heo inode->i_wb_frn_winner = 0; 442682aa8e1STejun Heo inode->i_wb_frn_avg_time = 0; 443682aa8e1STejun Heo inode->i_wb_frn_history = 0; 444d10c8095STejun Heo switched = true; 445d10c8095STejun Heo skip_switch: 446682aa8e1STejun Heo /* 447682aa8e1STejun Heo * Paired with load_acquire in unlocked_inode_to_wb_begin() and 448682aa8e1STejun Heo * ensures that the new wb is visible if they see !I_WB_SWITCH. 449682aa8e1STejun Heo */ 450682aa8e1STejun Heo smp_store_release(&inode->i_state, inode->i_state & ~I_WB_SWITCH); 451682aa8e1STejun Heo 452b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 453682aa8e1STejun Heo spin_unlock(&inode->i_lock); 454d10c8095STejun Heo spin_unlock(&new_wb->list_lock); 455d10c8095STejun Heo spin_unlock(&old_wb->list_lock); 456d10c8095STejun Heo 4577fc5854fSTejun Heo up_read(&bdi->wb_switch_rwsem); 4587fc5854fSTejun Heo 459d10c8095STejun Heo if (switched) { 460d10c8095STejun Heo wb_wakeup(new_wb); 461d10c8095STejun Heo wb_put(old_wb); 462d10c8095STejun Heo } 463d10c8095STejun Heo wb_put(new_wb); 464682aa8e1STejun Heo 465682aa8e1STejun Heo iput(inode); 466682aa8e1STejun Heo kfree(isw); 467a1a0e23eSTejun Heo 468a1a0e23eSTejun Heo atomic_dec(&isw_nr_in_flight); 469682aa8e1STejun Heo } 470682aa8e1STejun Heo 471682aa8e1STejun Heo static void inode_switch_wbs_rcu_fn(struct rcu_head *rcu_head) 472682aa8e1STejun Heo { 473682aa8e1STejun Heo struct inode_switch_wbs_context *isw = container_of(rcu_head, 474682aa8e1STejun Heo struct inode_switch_wbs_context, rcu_head); 475682aa8e1STejun Heo 476682aa8e1STejun Heo /* needs to grab bh-unsafe locks, bounce to work item */ 477682aa8e1STejun Heo INIT_WORK(&isw->work, inode_switch_wbs_work_fn); 478a1a0e23eSTejun Heo queue_work(isw_wq, &isw->work); 479682aa8e1STejun Heo } 480682aa8e1STejun Heo 481682aa8e1STejun Heo /** 482682aa8e1STejun Heo * inode_switch_wbs - change the wb association of an inode 483682aa8e1STejun Heo * @inode: target inode 484682aa8e1STejun Heo * @new_wb_id: ID of the new wb 485682aa8e1STejun Heo * 486682aa8e1STejun Heo * Switch @inode's wb association to the wb identified by @new_wb_id. The 487682aa8e1STejun Heo * switching is performed asynchronously and may fail silently. 488682aa8e1STejun Heo */ 489682aa8e1STejun Heo static void inode_switch_wbs(struct inode *inode, int new_wb_id) 490682aa8e1STejun Heo { 491682aa8e1STejun Heo struct backing_dev_info *bdi = inode_to_bdi(inode); 492682aa8e1STejun Heo struct cgroup_subsys_state *memcg_css; 493682aa8e1STejun Heo struct inode_switch_wbs_context *isw; 494682aa8e1STejun Heo 495682aa8e1STejun Heo /* noop if seems to be already in progress */ 496682aa8e1STejun Heo if (inode->i_state & I_WB_SWITCH) 497682aa8e1STejun Heo return; 498682aa8e1STejun Heo 4996444f47eSTejun Heo /* avoid queueing a new switch if too many are already in flight */ 5006444f47eSTejun Heo if (atomic_read(&isw_nr_in_flight) > WB_FRN_MAX_IN_FLIGHT) 5017fc5854fSTejun Heo return; 5027fc5854fSTejun Heo 503682aa8e1STejun Heo isw = kzalloc(sizeof(*isw), GFP_ATOMIC); 504682aa8e1STejun Heo if (!isw) 5056444f47eSTejun Heo return; 506682aa8e1STejun Heo 507682aa8e1STejun Heo /* find and pin the new wb */ 508682aa8e1STejun Heo rcu_read_lock(); 509682aa8e1STejun Heo memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys); 510682aa8e1STejun Heo if (memcg_css) 511682aa8e1STejun Heo isw->new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 512682aa8e1STejun Heo rcu_read_unlock(); 513682aa8e1STejun Heo if (!isw->new_wb) 514682aa8e1STejun Heo goto out_free; 515682aa8e1STejun Heo 516682aa8e1STejun Heo /* while holding I_WB_SWITCH, no one else can update the association */ 517682aa8e1STejun Heo spin_lock(&inode->i_lock); 5181751e8a6SLinus Torvalds if (!(inode->i_sb->s_flags & SB_ACTIVE) || 519a1a0e23eSTejun Heo inode->i_state & (I_WB_SWITCH | I_FREEING) || 520a1a0e23eSTejun Heo inode_to_wb(inode) == isw->new_wb) { 521a1a0e23eSTejun Heo spin_unlock(&inode->i_lock); 522a1a0e23eSTejun Heo goto out_free; 523a1a0e23eSTejun Heo } 524682aa8e1STejun Heo inode->i_state |= I_WB_SWITCH; 52574524955STahsin Erdogan __iget(inode); 526682aa8e1STejun Heo spin_unlock(&inode->i_lock); 527682aa8e1STejun Heo 528682aa8e1STejun Heo isw->inode = inode; 529682aa8e1STejun Heo 530682aa8e1STejun Heo /* 531682aa8e1STejun Heo * In addition to synchronizing among switchers, I_WB_SWITCH tells 532b93b0163SMatthew Wilcox * the RCU protected stat update paths to grab the i_page 533b93b0163SMatthew Wilcox * lock so that stat transfer can synchronize against them. 534682aa8e1STejun Heo * Let's continue after I_WB_SWITCH is guaranteed to be visible. 535682aa8e1STejun Heo */ 536682aa8e1STejun Heo call_rcu(&isw->rcu_head, inode_switch_wbs_rcu_fn); 537ec084de9SJiufei Xue 538ec084de9SJiufei Xue atomic_inc(&isw_nr_in_flight); 5396444f47eSTejun Heo return; 540682aa8e1STejun Heo 541682aa8e1STejun Heo out_free: 542682aa8e1STejun Heo if (isw->new_wb) 543682aa8e1STejun Heo wb_put(isw->new_wb); 544682aa8e1STejun Heo kfree(isw); 545682aa8e1STejun Heo } 546682aa8e1STejun Heo 54787e1d789STejun Heo /** 548b16b1debSTejun Heo * wbc_attach_and_unlock_inode - associate wbc with target inode and unlock it 549b16b1debSTejun Heo * @wbc: writeback_control of interest 550b16b1debSTejun Heo * @inode: target inode 551b16b1debSTejun Heo * 552b16b1debSTejun Heo * @inode is locked and about to be written back under the control of @wbc. 553b16b1debSTejun Heo * Record @inode's writeback context into @wbc and unlock the i_lock. On 554b16b1debSTejun Heo * writeback completion, wbc_detach_inode() should be called. This is used 555b16b1debSTejun Heo * to track the cgroup writeback context. 556b16b1debSTejun Heo */ 557b16b1debSTejun Heo void wbc_attach_and_unlock_inode(struct writeback_control *wbc, 558b16b1debSTejun Heo struct inode *inode) 559b16b1debSTejun Heo { 560dd73e4b7STejun Heo if (!inode_cgwb_enabled(inode)) { 561dd73e4b7STejun Heo spin_unlock(&inode->i_lock); 562dd73e4b7STejun Heo return; 563dd73e4b7STejun Heo } 564dd73e4b7STejun Heo 565b16b1debSTejun Heo wbc->wb = inode_to_wb(inode); 5662a814908STejun Heo wbc->inode = inode; 5672a814908STejun Heo 5682a814908STejun Heo wbc->wb_id = wbc->wb->memcg_css->id; 5692a814908STejun Heo wbc->wb_lcand_id = inode->i_wb_frn_winner; 5702a814908STejun Heo wbc->wb_tcand_id = 0; 5712a814908STejun Heo wbc->wb_bytes = 0; 5722a814908STejun Heo wbc->wb_lcand_bytes = 0; 5732a814908STejun Heo wbc->wb_tcand_bytes = 0; 5742a814908STejun Heo 575b16b1debSTejun Heo wb_get(wbc->wb); 576b16b1debSTejun Heo spin_unlock(&inode->i_lock); 577e8a7abf5STejun Heo 578e8a7abf5STejun Heo /* 57965de03e2STejun Heo * A dying wb indicates that either the blkcg associated with the 58065de03e2STejun Heo * memcg changed or the associated memcg is dying. In the first 58165de03e2STejun Heo * case, a replacement wb should already be available and we should 58265de03e2STejun Heo * refresh the wb immediately. In the second case, trying to 58365de03e2STejun Heo * refresh will keep failing. 584e8a7abf5STejun Heo */ 58565de03e2STejun Heo if (unlikely(wb_dying(wbc->wb) && !css_is_dying(wbc->wb->memcg_css))) 586e8a7abf5STejun Heo inode_switch_wbs(inode, wbc->wb_id); 587b16b1debSTejun Heo } 5889b0eb69bSTejun Heo EXPORT_SYMBOL_GPL(wbc_attach_and_unlock_inode); 589b16b1debSTejun Heo 590b16b1debSTejun Heo /** 5912a814908STejun Heo * wbc_detach_inode - disassociate wbc from inode and perform foreign detection 5922a814908STejun Heo * @wbc: writeback_control of the just finished writeback 593b16b1debSTejun Heo * 594b16b1debSTejun Heo * To be called after a writeback attempt of an inode finishes and undoes 595b16b1debSTejun Heo * wbc_attach_and_unlock_inode(). Can be called under any context. 5962a814908STejun Heo * 5972a814908STejun Heo * As concurrent write sharing of an inode is expected to be very rare and 5982a814908STejun Heo * memcg only tracks page ownership on first-use basis severely confining 5992a814908STejun Heo * the usefulness of such sharing, cgroup writeback tracks ownership 6002a814908STejun Heo * per-inode. While the support for concurrent write sharing of an inode 6012a814908STejun Heo * is deemed unnecessary, an inode being written to by different cgroups at 6022a814908STejun Heo * different points in time is a lot more common, and, more importantly, 6032a814908STejun Heo * charging only by first-use can too readily lead to grossly incorrect 6042a814908STejun Heo * behaviors (single foreign page can lead to gigabytes of writeback to be 6052a814908STejun Heo * incorrectly attributed). 6062a814908STejun Heo * 6072a814908STejun Heo * To resolve this issue, cgroup writeback detects the majority dirtier of 6082a814908STejun Heo * an inode and transfers the ownership to it. To avoid unnnecessary 6092a814908STejun Heo * oscillation, the detection mechanism keeps track of history and gives 6102a814908STejun Heo * out the switch verdict only if the foreign usage pattern is stable over 6112a814908STejun Heo * a certain amount of time and/or writeback attempts. 6122a814908STejun Heo * 6132a814908STejun Heo * On each writeback attempt, @wbc tries to detect the majority writer 6142a814908STejun Heo * using Boyer-Moore majority vote algorithm. In addition to the byte 6152a814908STejun Heo * count from the majority voting, it also counts the bytes written for the 6162a814908STejun Heo * current wb and the last round's winner wb (max of last round's current 6172a814908STejun Heo * wb, the winner from two rounds ago, and the last round's majority 6182a814908STejun Heo * candidate). Keeping track of the historical winner helps the algorithm 6192a814908STejun Heo * to semi-reliably detect the most active writer even when it's not the 6202a814908STejun Heo * absolute majority. 6212a814908STejun Heo * 6222a814908STejun Heo * Once the winner of the round is determined, whether the winner is 6232a814908STejun Heo * foreign or not and how much IO time the round consumed is recorded in 6242a814908STejun Heo * inode->i_wb_frn_history. If the amount of recorded foreign IO time is 6252a814908STejun Heo * over a certain threshold, the switch verdict is given. 626b16b1debSTejun Heo */ 627b16b1debSTejun Heo void wbc_detach_inode(struct writeback_control *wbc) 628b16b1debSTejun Heo { 6292a814908STejun Heo struct bdi_writeback *wb = wbc->wb; 6302a814908STejun Heo struct inode *inode = wbc->inode; 631dd73e4b7STejun Heo unsigned long avg_time, max_bytes, max_time; 632dd73e4b7STejun Heo u16 history; 6332a814908STejun Heo int max_id; 6342a814908STejun Heo 635dd73e4b7STejun Heo if (!wb) 636dd73e4b7STejun Heo return; 637dd73e4b7STejun Heo 638dd73e4b7STejun Heo history = inode->i_wb_frn_history; 639dd73e4b7STejun Heo avg_time = inode->i_wb_frn_avg_time; 640dd73e4b7STejun Heo 6412a814908STejun Heo /* pick the winner of this round */ 6422a814908STejun Heo if (wbc->wb_bytes >= wbc->wb_lcand_bytes && 6432a814908STejun Heo wbc->wb_bytes >= wbc->wb_tcand_bytes) { 6442a814908STejun Heo max_id = wbc->wb_id; 6452a814908STejun Heo max_bytes = wbc->wb_bytes; 6462a814908STejun Heo } else if (wbc->wb_lcand_bytes >= wbc->wb_tcand_bytes) { 6472a814908STejun Heo max_id = wbc->wb_lcand_id; 6482a814908STejun Heo max_bytes = wbc->wb_lcand_bytes; 6492a814908STejun Heo } else { 6502a814908STejun Heo max_id = wbc->wb_tcand_id; 6512a814908STejun Heo max_bytes = wbc->wb_tcand_bytes; 6522a814908STejun Heo } 6532a814908STejun Heo 6542a814908STejun Heo /* 6552a814908STejun Heo * Calculate the amount of IO time the winner consumed and fold it 6562a814908STejun Heo * into the running average kept per inode. If the consumed IO 6572a814908STejun Heo * time is lower than avag / WB_FRN_TIME_CUT_DIV, ignore it for 6582a814908STejun Heo * deciding whether to switch or not. This is to prevent one-off 6592a814908STejun Heo * small dirtiers from skewing the verdict. 6602a814908STejun Heo */ 6612a814908STejun Heo max_time = DIV_ROUND_UP((max_bytes >> PAGE_SHIFT) << WB_FRN_TIME_SHIFT, 6622a814908STejun Heo wb->avg_write_bandwidth); 6632a814908STejun Heo if (avg_time) 6642a814908STejun Heo avg_time += (max_time >> WB_FRN_TIME_AVG_SHIFT) - 6652a814908STejun Heo (avg_time >> WB_FRN_TIME_AVG_SHIFT); 6662a814908STejun Heo else 6672a814908STejun Heo avg_time = max_time; /* immediate catch up on first run */ 6682a814908STejun Heo 6692a814908STejun Heo if (max_time >= avg_time / WB_FRN_TIME_CUT_DIV) { 6702a814908STejun Heo int slots; 6712a814908STejun Heo 6722a814908STejun Heo /* 6732a814908STejun Heo * The switch verdict is reached if foreign wb's consume 6742a814908STejun Heo * more than a certain proportion of IO time in a 6752a814908STejun Heo * WB_FRN_TIME_PERIOD. This is loosely tracked by 16 slot 6762a814908STejun Heo * history mask where each bit represents one sixteenth of 6772a814908STejun Heo * the period. Determine the number of slots to shift into 6782a814908STejun Heo * history from @max_time. 6792a814908STejun Heo */ 6802a814908STejun Heo slots = min(DIV_ROUND_UP(max_time, WB_FRN_HIST_UNIT), 6812a814908STejun Heo (unsigned long)WB_FRN_HIST_MAX_SLOTS); 6822a814908STejun Heo history <<= slots; 6832a814908STejun Heo if (wbc->wb_id != max_id) 6842a814908STejun Heo history |= (1U << slots) - 1; 6852a814908STejun Heo 6863a8e9ac8STejun Heo if (history) 6873a8e9ac8STejun Heo trace_inode_foreign_history(inode, wbc, history); 6883a8e9ac8STejun Heo 6892a814908STejun Heo /* 6902a814908STejun Heo * Switch if the current wb isn't the consistent winner. 6912a814908STejun Heo * If there are multiple closely competing dirtiers, the 6922a814908STejun Heo * inode may switch across them repeatedly over time, which 6932a814908STejun Heo * is okay. The main goal is avoiding keeping an inode on 6942a814908STejun Heo * the wrong wb for an extended period of time. 6952a814908STejun Heo */ 696682aa8e1STejun Heo if (hweight32(history) > WB_FRN_HIST_THR_SLOTS) 697682aa8e1STejun Heo inode_switch_wbs(inode, max_id); 6982a814908STejun Heo } 6992a814908STejun Heo 7002a814908STejun Heo /* 7012a814908STejun Heo * Multiple instances of this function may race to update the 7022a814908STejun Heo * following fields but we don't mind occassional inaccuracies. 7032a814908STejun Heo */ 7042a814908STejun Heo inode->i_wb_frn_winner = max_id; 7052a814908STejun Heo inode->i_wb_frn_avg_time = min(avg_time, (unsigned long)U16_MAX); 7062a814908STejun Heo inode->i_wb_frn_history = history; 7072a814908STejun Heo 708b16b1debSTejun Heo wb_put(wbc->wb); 709b16b1debSTejun Heo wbc->wb = NULL; 710b16b1debSTejun Heo } 7119b0eb69bSTejun Heo EXPORT_SYMBOL_GPL(wbc_detach_inode); 712b16b1debSTejun Heo 713b16b1debSTejun Heo /** 71434e51a5eSTejun Heo * wbc_account_cgroup_owner - account writeback to update inode cgroup ownership 7152a814908STejun Heo * @wbc: writeback_control of the writeback in progress 7162a814908STejun Heo * @page: page being written out 7172a814908STejun Heo * @bytes: number of bytes being written out 7182a814908STejun Heo * 7192a814908STejun Heo * @bytes from @page are about to written out during the writeback 7202a814908STejun Heo * controlled by @wbc. Keep the book for foreign inode detection. See 7212a814908STejun Heo * wbc_detach_inode(). 7222a814908STejun Heo */ 72334e51a5eSTejun Heo void wbc_account_cgroup_owner(struct writeback_control *wbc, struct page *page, 7242a814908STejun Heo size_t bytes) 7252a814908STejun Heo { 72666311422STejun Heo struct cgroup_subsys_state *css; 7272a814908STejun Heo int id; 7282a814908STejun Heo 7292a814908STejun Heo /* 7302a814908STejun Heo * pageout() path doesn't attach @wbc to the inode being written 7312a814908STejun Heo * out. This is intentional as we don't want the function to block 7322a814908STejun Heo * behind a slow cgroup. Ultimately, we want pageout() to kick off 7332a814908STejun Heo * regular writeback instead of writing things out itself. 7342a814908STejun Heo */ 73527b36d8fSTejun Heo if (!wbc->wb || wbc->no_cgroup_owner) 7362a814908STejun Heo return; 7372a814908STejun Heo 73866311422STejun Heo css = mem_cgroup_css_from_page(page); 73966311422STejun Heo /* dead cgroups shouldn't contribute to inode ownership arbitration */ 74066311422STejun Heo if (!(css->flags & CSS_ONLINE)) 74166311422STejun Heo return; 74266311422STejun Heo 74366311422STejun Heo id = css->id; 7442a814908STejun Heo 7452a814908STejun Heo if (id == wbc->wb_id) { 7462a814908STejun Heo wbc->wb_bytes += bytes; 7472a814908STejun Heo return; 7482a814908STejun Heo } 7492a814908STejun Heo 7502a814908STejun Heo if (id == wbc->wb_lcand_id) 7512a814908STejun Heo wbc->wb_lcand_bytes += bytes; 7522a814908STejun Heo 7532a814908STejun Heo /* Boyer-Moore majority vote algorithm */ 7542a814908STejun Heo if (!wbc->wb_tcand_bytes) 7552a814908STejun Heo wbc->wb_tcand_id = id; 7562a814908STejun Heo if (id == wbc->wb_tcand_id) 7572a814908STejun Heo wbc->wb_tcand_bytes += bytes; 7582a814908STejun Heo else 7592a814908STejun Heo wbc->wb_tcand_bytes -= min(bytes, wbc->wb_tcand_bytes); 7602a814908STejun Heo } 76134e51a5eSTejun Heo EXPORT_SYMBOL_GPL(wbc_account_cgroup_owner); 7622a814908STejun Heo 7632a814908STejun Heo /** 764703c2708STejun Heo * inode_congested - test whether an inode is congested 76560292bccSTejun Heo * @inode: inode to test for congestion (may be NULL) 766703c2708STejun Heo * @cong_bits: mask of WB_[a]sync_congested bits to test 767703c2708STejun Heo * 768703c2708STejun Heo * Tests whether @inode is congested. @cong_bits is the mask of congestion 769703c2708STejun Heo * bits to test and the return value is the mask of set bits. 770703c2708STejun Heo * 771703c2708STejun Heo * If cgroup writeback is enabled for @inode, the congestion state is 772703c2708STejun Heo * determined by whether the cgwb (cgroup bdi_writeback) for the blkcg 773703c2708STejun Heo * associated with @inode is congested; otherwise, the root wb's congestion 774703c2708STejun Heo * state is used. 77560292bccSTejun Heo * 77660292bccSTejun Heo * @inode is allowed to be NULL as this function is often called on 77760292bccSTejun Heo * mapping->host which is NULL for the swapper space. 778703c2708STejun Heo */ 779703c2708STejun Heo int inode_congested(struct inode *inode, int cong_bits) 780703c2708STejun Heo { 7815cb8b824STejun Heo /* 7825cb8b824STejun Heo * Once set, ->i_wb never becomes NULL while the inode is alive. 7835cb8b824STejun Heo * Start transaction iff ->i_wb is visible. 7845cb8b824STejun Heo */ 785aaa2cacfSTejun Heo if (inode && inode_to_wb_is_valid(inode)) { 7865cb8b824STejun Heo struct bdi_writeback *wb; 7872e898e4cSGreg Thelen struct wb_lock_cookie lock_cookie = {}; 7882e898e4cSGreg Thelen bool congested; 7895cb8b824STejun Heo 7902e898e4cSGreg Thelen wb = unlocked_inode_to_wb_begin(inode, &lock_cookie); 7915cb8b824STejun Heo congested = wb_congested(wb, cong_bits); 7922e898e4cSGreg Thelen unlocked_inode_to_wb_end(inode, &lock_cookie); 7935cb8b824STejun Heo return congested; 794703c2708STejun Heo } 795703c2708STejun Heo 796703c2708STejun Heo return wb_congested(&inode_to_bdi(inode)->wb, cong_bits); 797703c2708STejun Heo } 798703c2708STejun Heo EXPORT_SYMBOL_GPL(inode_congested); 799703c2708STejun Heo 800f2b65121STejun Heo /** 801f2b65121STejun Heo * wb_split_bdi_pages - split nr_pages to write according to bandwidth 802f2b65121STejun Heo * @wb: target bdi_writeback to split @nr_pages to 803f2b65121STejun Heo * @nr_pages: number of pages to write for the whole bdi 804f2b65121STejun Heo * 805f2b65121STejun Heo * Split @wb's portion of @nr_pages according to @wb's write bandwidth in 806f2b65121STejun Heo * relation to the total write bandwidth of all wb's w/ dirty inodes on 807f2b65121STejun Heo * @wb->bdi. 808f2b65121STejun Heo */ 809f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) 810f2b65121STejun Heo { 811f2b65121STejun Heo unsigned long this_bw = wb->avg_write_bandwidth; 812f2b65121STejun Heo unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth); 813f2b65121STejun Heo 814f2b65121STejun Heo if (nr_pages == LONG_MAX) 815f2b65121STejun Heo return LONG_MAX; 816f2b65121STejun Heo 817f2b65121STejun Heo /* 818f2b65121STejun Heo * This may be called on clean wb's and proportional distribution 819f2b65121STejun Heo * may not make sense, just use the original @nr_pages in those 820f2b65121STejun Heo * cases. In general, we wanna err on the side of writing more. 821f2b65121STejun Heo */ 822f2b65121STejun Heo if (!tot_bw || this_bw >= tot_bw) 823f2b65121STejun Heo return nr_pages; 824f2b65121STejun Heo else 825f2b65121STejun Heo return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw); 826f2b65121STejun Heo } 827f2b65121STejun Heo 828db125360STejun Heo /** 829db125360STejun Heo * bdi_split_work_to_wbs - split a wb_writeback_work to all wb's of a bdi 830db125360STejun Heo * @bdi: target backing_dev_info 831db125360STejun Heo * @base_work: wb_writeback_work to issue 832db125360STejun Heo * @skip_if_busy: skip wb's which already have writeback in progress 833db125360STejun Heo * 834db125360STejun Heo * Split and issue @base_work to all wb's (bdi_writeback's) of @bdi which 835db125360STejun Heo * have dirty inodes. If @base_work->nr_page isn't %LONG_MAX, it's 836db125360STejun Heo * distributed to the busy wbs according to each wb's proportion in the 837db125360STejun Heo * total active write bandwidth of @bdi. 838db125360STejun Heo */ 839db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, 840db125360STejun Heo struct wb_writeback_work *base_work, 841db125360STejun Heo bool skip_if_busy) 842db125360STejun Heo { 843b817525aSTejun Heo struct bdi_writeback *last_wb = NULL; 844b33e18f6STejun Heo struct bdi_writeback *wb = list_entry(&bdi->wb_list, 845b817525aSTejun Heo struct bdi_writeback, bdi_node); 846db125360STejun Heo 847db125360STejun Heo might_sleep(); 848db125360STejun Heo restart: 849db125360STejun Heo rcu_read_lock(); 850b817525aSTejun Heo list_for_each_entry_continue_rcu(wb, &bdi->wb_list, bdi_node) { 8515b9cce4cSTejun Heo DEFINE_WB_COMPLETION(fallback_work_done, bdi); 8528a1270cdSTejun Heo struct wb_writeback_work fallback_work; 8538a1270cdSTejun Heo struct wb_writeback_work *work; 8548a1270cdSTejun Heo long nr_pages; 8558a1270cdSTejun Heo 856b817525aSTejun Heo if (last_wb) { 857b817525aSTejun Heo wb_put(last_wb); 858b817525aSTejun Heo last_wb = NULL; 859b817525aSTejun Heo } 860b817525aSTejun Heo 861006a0973STejun Heo /* SYNC_ALL writes out I_DIRTY_TIME too */ 862006a0973STejun Heo if (!wb_has_dirty_io(wb) && 863006a0973STejun Heo (base_work->sync_mode == WB_SYNC_NONE || 864006a0973STejun Heo list_empty(&wb->b_dirty_time))) 865006a0973STejun Heo continue; 866006a0973STejun Heo if (skip_if_busy && writeback_in_progress(wb)) 867db125360STejun Heo continue; 868db125360STejun Heo 8698a1270cdSTejun Heo nr_pages = wb_split_bdi_pages(wb, base_work->nr_pages); 8708a1270cdSTejun Heo 8718a1270cdSTejun Heo work = kmalloc(sizeof(*work), GFP_ATOMIC); 8728a1270cdSTejun Heo if (work) { 8738a1270cdSTejun Heo *work = *base_work; 8748a1270cdSTejun Heo work->nr_pages = nr_pages; 8758a1270cdSTejun Heo work->auto_free = 1; 8768a1270cdSTejun Heo wb_queue_work(wb, work); 8778a1270cdSTejun Heo continue; 878db125360STejun Heo } 8798a1270cdSTejun Heo 8808a1270cdSTejun Heo /* alloc failed, execute synchronously using on-stack fallback */ 8818a1270cdSTejun Heo work = &fallback_work; 8828a1270cdSTejun Heo *work = *base_work; 8838a1270cdSTejun Heo work->nr_pages = nr_pages; 8848a1270cdSTejun Heo work->auto_free = 0; 8858a1270cdSTejun Heo work->done = &fallback_work_done; 8868a1270cdSTejun Heo 8878a1270cdSTejun Heo wb_queue_work(wb, work); 8888a1270cdSTejun Heo 889b817525aSTejun Heo /* 890b817525aSTejun Heo * Pin @wb so that it stays on @bdi->wb_list. This allows 891b817525aSTejun Heo * continuing iteration from @wb after dropping and 892b817525aSTejun Heo * regrabbing rcu read lock. 893b817525aSTejun Heo */ 894b817525aSTejun Heo wb_get(wb); 895b817525aSTejun Heo last_wb = wb; 896b817525aSTejun Heo 897db125360STejun Heo rcu_read_unlock(); 8985b9cce4cSTejun Heo wb_wait_for_completion(&fallback_work_done); 899db125360STejun Heo goto restart; 900db125360STejun Heo } 901db125360STejun Heo rcu_read_unlock(); 902b817525aSTejun Heo 903b817525aSTejun Heo if (last_wb) 904b817525aSTejun Heo wb_put(last_wb); 905db125360STejun Heo } 906db125360STejun Heo 907a1a0e23eSTejun Heo /** 908d62241c7STejun Heo * cgroup_writeback_by_id - initiate cgroup writeback from bdi and memcg IDs 909d62241c7STejun Heo * @bdi_id: target bdi id 910d62241c7STejun Heo * @memcg_id: target memcg css id 911b46ec1daSRandy Dunlap * @nr: number of pages to write, 0 for best-effort dirty flushing 912d62241c7STejun Heo * @reason: reason why some writeback work initiated 913d62241c7STejun Heo * @done: target wb_completion 914d62241c7STejun Heo * 915d62241c7STejun Heo * Initiate flush of the bdi_writeback identified by @bdi_id and @memcg_id 916d62241c7STejun Heo * with the specified parameters. 917d62241c7STejun Heo */ 918d62241c7STejun Heo int cgroup_writeback_by_id(u64 bdi_id, int memcg_id, unsigned long nr, 919d62241c7STejun Heo enum wb_reason reason, struct wb_completion *done) 920d62241c7STejun Heo { 921d62241c7STejun Heo struct backing_dev_info *bdi; 922d62241c7STejun Heo struct cgroup_subsys_state *memcg_css; 923d62241c7STejun Heo struct bdi_writeback *wb; 924d62241c7STejun Heo struct wb_writeback_work *work; 925d62241c7STejun Heo int ret; 926d62241c7STejun Heo 927d62241c7STejun Heo /* lookup bdi and memcg */ 928d62241c7STejun Heo bdi = bdi_get_by_id(bdi_id); 929d62241c7STejun Heo if (!bdi) 930d62241c7STejun Heo return -ENOENT; 931d62241c7STejun Heo 932d62241c7STejun Heo rcu_read_lock(); 933d62241c7STejun Heo memcg_css = css_from_id(memcg_id, &memory_cgrp_subsys); 934d62241c7STejun Heo if (memcg_css && !css_tryget(memcg_css)) 935d62241c7STejun Heo memcg_css = NULL; 936d62241c7STejun Heo rcu_read_unlock(); 937d62241c7STejun Heo if (!memcg_css) { 938d62241c7STejun Heo ret = -ENOENT; 939d62241c7STejun Heo goto out_bdi_put; 940d62241c7STejun Heo } 941d62241c7STejun Heo 942d62241c7STejun Heo /* 943d62241c7STejun Heo * And find the associated wb. If the wb isn't there already 944d62241c7STejun Heo * there's nothing to flush, don't create one. 945d62241c7STejun Heo */ 946d62241c7STejun Heo wb = wb_get_lookup(bdi, memcg_css); 947d62241c7STejun Heo if (!wb) { 948d62241c7STejun Heo ret = -ENOENT; 949d62241c7STejun Heo goto out_css_put; 950d62241c7STejun Heo } 951d62241c7STejun Heo 952d62241c7STejun Heo /* 953d62241c7STejun Heo * If @nr is zero, the caller is attempting to write out most of 954d62241c7STejun Heo * the currently dirty pages. Let's take the current dirty page 955d62241c7STejun Heo * count and inflate it by 25% which should be large enough to 956d62241c7STejun Heo * flush out most dirty pages while avoiding getting livelocked by 957d62241c7STejun Heo * concurrent dirtiers. 958d62241c7STejun Heo */ 959d62241c7STejun Heo if (!nr) { 960d62241c7STejun Heo unsigned long filepages, headroom, dirty, writeback; 961d62241c7STejun Heo 962d62241c7STejun Heo mem_cgroup_wb_stats(wb, &filepages, &headroom, &dirty, 963d62241c7STejun Heo &writeback); 964d62241c7STejun Heo nr = dirty * 10 / 8; 965d62241c7STejun Heo } 966d62241c7STejun Heo 967d62241c7STejun Heo /* issue the writeback work */ 968d62241c7STejun Heo work = kzalloc(sizeof(*work), GFP_NOWAIT | __GFP_NOWARN); 969d62241c7STejun Heo if (work) { 970d62241c7STejun Heo work->nr_pages = nr; 971d62241c7STejun Heo work->sync_mode = WB_SYNC_NONE; 972d62241c7STejun Heo work->range_cyclic = 1; 973d62241c7STejun Heo work->reason = reason; 974d62241c7STejun Heo work->done = done; 975d62241c7STejun Heo work->auto_free = 1; 976d62241c7STejun Heo wb_queue_work(wb, work); 977d62241c7STejun Heo ret = 0; 978d62241c7STejun Heo } else { 979d62241c7STejun Heo ret = -ENOMEM; 980d62241c7STejun Heo } 981d62241c7STejun Heo 982d62241c7STejun Heo wb_put(wb); 983d62241c7STejun Heo out_css_put: 984d62241c7STejun Heo css_put(memcg_css); 985d62241c7STejun Heo out_bdi_put: 986d62241c7STejun Heo bdi_put(bdi); 987d62241c7STejun Heo return ret; 988d62241c7STejun Heo } 989d62241c7STejun Heo 990d62241c7STejun Heo /** 991a1a0e23eSTejun Heo * cgroup_writeback_umount - flush inode wb switches for umount 992a1a0e23eSTejun Heo * 993a1a0e23eSTejun Heo * This function is called when a super_block is about to be destroyed and 994a1a0e23eSTejun Heo * flushes in-flight inode wb switches. An inode wb switch goes through 995a1a0e23eSTejun Heo * RCU and then workqueue, so the two need to be flushed in order to ensure 996a1a0e23eSTejun Heo * that all previously scheduled switches are finished. As wb switches are 997a1a0e23eSTejun Heo * rare occurrences and synchronize_rcu() can take a while, perform 998a1a0e23eSTejun Heo * flushing iff wb switches are in flight. 999a1a0e23eSTejun Heo */ 1000a1a0e23eSTejun Heo void cgroup_writeback_umount(void) 1001a1a0e23eSTejun Heo { 1002a1a0e23eSTejun Heo if (atomic_read(&isw_nr_in_flight)) { 1003ec084de9SJiufei Xue /* 1004ec084de9SJiufei Xue * Use rcu_barrier() to wait for all pending callbacks to 1005ec084de9SJiufei Xue * ensure that all in-flight wb switches are in the workqueue. 1006ec084de9SJiufei Xue */ 1007ec084de9SJiufei Xue rcu_barrier(); 1008a1a0e23eSTejun Heo flush_workqueue(isw_wq); 1009a1a0e23eSTejun Heo } 1010a1a0e23eSTejun Heo } 1011a1a0e23eSTejun Heo 1012a1a0e23eSTejun Heo static int __init cgroup_writeback_init(void) 1013a1a0e23eSTejun Heo { 1014a1a0e23eSTejun Heo isw_wq = alloc_workqueue("inode_switch_wbs", 0, 0); 1015a1a0e23eSTejun Heo if (!isw_wq) 1016a1a0e23eSTejun Heo return -ENOMEM; 1017a1a0e23eSTejun Heo return 0; 1018a1a0e23eSTejun Heo } 1019a1a0e23eSTejun Heo fs_initcall(cgroup_writeback_init); 1020a1a0e23eSTejun Heo 1021f2b65121STejun Heo #else /* CONFIG_CGROUP_WRITEBACK */ 1022f2b65121STejun Heo 10237fc5854fSTejun Heo static void bdi_down_write_wb_switch_rwsem(struct backing_dev_info *bdi) { } 10247fc5854fSTejun Heo static void bdi_up_write_wb_switch_rwsem(struct backing_dev_info *bdi) { } 10257fc5854fSTejun Heo 102687e1d789STejun Heo static struct bdi_writeback * 102787e1d789STejun Heo locked_inode_to_wb_and_lock_list(struct inode *inode) 102887e1d789STejun Heo __releases(&inode->i_lock) 102987e1d789STejun Heo __acquires(&wb->list_lock) 103087e1d789STejun Heo { 103187e1d789STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 103287e1d789STejun Heo 103387e1d789STejun Heo spin_unlock(&inode->i_lock); 103487e1d789STejun Heo spin_lock(&wb->list_lock); 103587e1d789STejun Heo return wb; 103687e1d789STejun Heo } 103787e1d789STejun Heo 103887e1d789STejun Heo static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode) 103987e1d789STejun Heo __acquires(&wb->list_lock) 104087e1d789STejun Heo { 104187e1d789STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 104287e1d789STejun Heo 104387e1d789STejun Heo spin_lock(&wb->list_lock); 104487e1d789STejun Heo return wb; 104587e1d789STejun Heo } 104687e1d789STejun Heo 1047f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) 1048f2b65121STejun Heo { 1049f2b65121STejun Heo return nr_pages; 1050f2b65121STejun Heo } 1051f2b65121STejun Heo 1052db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, 1053db125360STejun Heo struct wb_writeback_work *base_work, 1054db125360STejun Heo bool skip_if_busy) 1055db125360STejun Heo { 1056db125360STejun Heo might_sleep(); 1057db125360STejun Heo 1058006a0973STejun Heo if (!skip_if_busy || !writeback_in_progress(&bdi->wb)) { 1059db125360STejun Heo base_work->auto_free = 0; 1060db125360STejun Heo wb_queue_work(&bdi->wb, base_work); 1061db125360STejun Heo } 1062db125360STejun Heo } 1063db125360STejun Heo 1064703c2708STejun Heo #endif /* CONFIG_CGROUP_WRITEBACK */ 1065703c2708STejun Heo 1066e8e8a0c6SJens Axboe /* 1067e8e8a0c6SJens Axboe * Add in the number of potentially dirty inodes, because each inode 1068e8e8a0c6SJens Axboe * write can dirty pagecache in the underlying blockdev. 1069e8e8a0c6SJens Axboe */ 1070e8e8a0c6SJens Axboe static unsigned long get_nr_dirty_pages(void) 1071e8e8a0c6SJens Axboe { 1072e8e8a0c6SJens Axboe return global_node_page_state(NR_FILE_DIRTY) + 1073e8e8a0c6SJens Axboe global_node_page_state(NR_UNSTABLE_NFS) + 1074e8e8a0c6SJens Axboe get_nr_dirty_inodes(); 1075e8e8a0c6SJens Axboe } 1076e8e8a0c6SJens Axboe 1077e8e8a0c6SJens Axboe static void wb_start_writeback(struct bdi_writeback *wb, enum wb_reason reason) 1078b6e51316SJens Axboe { 1079c00ddad3STejun Heo if (!wb_has_dirty_io(wb)) 1080c00ddad3STejun Heo return; 1081c00ddad3STejun Heo 1082c00ddad3STejun Heo /* 1083aac8d41cSJens Axboe * All callers of this function want to start writeback of all 1084aac8d41cSJens Axboe * dirty pages. Places like vmscan can call this at a very 1085aac8d41cSJens Axboe * high frequency, causing pointless allocations of tons of 1086aac8d41cSJens Axboe * work items and keeping the flusher threads busy retrieving 1087aac8d41cSJens Axboe * that work. Ensure that we only allow one of them pending and 108885009b4fSJens Axboe * inflight at the time. 1089aac8d41cSJens Axboe */ 109085009b4fSJens Axboe if (test_bit(WB_start_all, &wb->state) || 109185009b4fSJens Axboe test_and_set_bit(WB_start_all, &wb->state)) 1092aac8d41cSJens Axboe return; 1093aac8d41cSJens Axboe 109485009b4fSJens Axboe wb->start_all_reason = reason; 1095c00ddad3STejun Heo wb_wakeup(wb); 1096d3ddec76SWu Fengguang } 1097d3ddec76SWu Fengguang 1098c5444198SChristoph Hellwig /** 10999ecf4866STejun Heo * wb_start_background_writeback - start background writeback 11009ecf4866STejun Heo * @wb: bdi_writback to write from 1101c5444198SChristoph Hellwig * 1102c5444198SChristoph Hellwig * Description: 11036585027aSJan Kara * This makes sure WB_SYNC_NONE background writeback happens. When 11049ecf4866STejun Heo * this function returns, it is only guaranteed that for given wb 11056585027aSJan Kara * some IO is happening if we are over background dirty threshold. 11066585027aSJan Kara * Caller need not hold sb s_umount semaphore. 1107c5444198SChristoph Hellwig */ 11089ecf4866STejun Heo void wb_start_background_writeback(struct bdi_writeback *wb) 1109c5444198SChristoph Hellwig { 11106585027aSJan Kara /* 11116585027aSJan Kara * We just wake up the flusher thread. It will perform background 11126585027aSJan Kara * writeback as soon as there is no other work to do. 11136585027aSJan Kara */ 11145634cc2aSTejun Heo trace_writeback_wake_background(wb); 11159ecf4866STejun Heo wb_wakeup(wb); 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds /* 1119a66979abSDave Chinner * Remove the inode from the writeback list it is on. 1120a66979abSDave Chinner */ 1121c7f54084SDave Chinner void inode_io_list_del(struct inode *inode) 1122a66979abSDave Chinner { 112387e1d789STejun Heo struct bdi_writeback *wb; 1124a66979abSDave Chinner 112587e1d789STejun Heo wb = inode_to_wb_and_lock_list(inode); 1126c7f54084SDave Chinner inode_io_list_del_locked(inode, wb); 112752ebea74STejun Heo spin_unlock(&wb->list_lock); 1128f758eeabSChristoph Hellwig } 11294301efa4SJan Kara EXPORT_SYMBOL(inode_io_list_del); 1130a66979abSDave Chinner 1131a66979abSDave Chinner /* 11326c60d2b5SDave Chinner * mark an inode as under writeback on the sb 11336c60d2b5SDave Chinner */ 11346c60d2b5SDave Chinner void sb_mark_inode_writeback(struct inode *inode) 11356c60d2b5SDave Chinner { 11366c60d2b5SDave Chinner struct super_block *sb = inode->i_sb; 11376c60d2b5SDave Chinner unsigned long flags; 11386c60d2b5SDave Chinner 11396c60d2b5SDave Chinner if (list_empty(&inode->i_wb_list)) { 11406c60d2b5SDave Chinner spin_lock_irqsave(&sb->s_inode_wblist_lock, flags); 11419a46b04fSBrian Foster if (list_empty(&inode->i_wb_list)) { 11426c60d2b5SDave Chinner list_add_tail(&inode->i_wb_list, &sb->s_inodes_wb); 11439a46b04fSBrian Foster trace_sb_mark_inode_writeback(inode); 11449a46b04fSBrian Foster } 11456c60d2b5SDave Chinner spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags); 11466c60d2b5SDave Chinner } 11476c60d2b5SDave Chinner } 11486c60d2b5SDave Chinner 11496c60d2b5SDave Chinner /* 11506c60d2b5SDave Chinner * clear an inode as under writeback on the sb 11516c60d2b5SDave Chinner */ 11526c60d2b5SDave Chinner void sb_clear_inode_writeback(struct inode *inode) 11536c60d2b5SDave Chinner { 11546c60d2b5SDave Chinner struct super_block *sb = inode->i_sb; 11556c60d2b5SDave Chinner unsigned long flags; 11566c60d2b5SDave Chinner 11576c60d2b5SDave Chinner if (!list_empty(&inode->i_wb_list)) { 11586c60d2b5SDave Chinner spin_lock_irqsave(&sb->s_inode_wblist_lock, flags); 11599a46b04fSBrian Foster if (!list_empty(&inode->i_wb_list)) { 11606c60d2b5SDave Chinner list_del_init(&inode->i_wb_list); 11619a46b04fSBrian Foster trace_sb_clear_inode_writeback(inode); 11629a46b04fSBrian Foster } 11636c60d2b5SDave Chinner spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags); 11646c60d2b5SDave Chinner } 11656c60d2b5SDave Chinner } 11666c60d2b5SDave Chinner 11676c60d2b5SDave Chinner /* 11686610a0bcSAndrew Morton * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 11696610a0bcSAndrew Morton * furthest end of its superblock's dirty-inode list. 11706610a0bcSAndrew Morton * 11716610a0bcSAndrew Morton * Before stamping the inode's ->dirtied_when, we check to see whether it is 117266f3b8e2SJens Axboe * already the most-recently-dirtied inode on the b_dirty list. If that is 11736610a0bcSAndrew Morton * the case then the inode must have been redirtied while it was being written 11746610a0bcSAndrew Morton * out and we don't reset its dirtied_when. 11756610a0bcSAndrew Morton */ 1176f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb) 11776610a0bcSAndrew Morton { 117803ba3782SJens Axboe if (!list_empty(&wb->b_dirty)) { 117966f3b8e2SJens Axboe struct inode *tail; 11806610a0bcSAndrew Morton 11817ccf19a8SNick Piggin tail = wb_inode(wb->b_dirty.next); 118266f3b8e2SJens Axboe if (time_before(inode->dirtied_when, tail->dirtied_when)) 11836610a0bcSAndrew Morton inode->dirtied_when = jiffies; 11846610a0bcSAndrew Morton } 1185c7f54084SDave Chinner inode_io_list_move_locked(inode, wb, &wb->b_dirty); 11866610a0bcSAndrew Morton } 11876610a0bcSAndrew Morton 11886610a0bcSAndrew Morton /* 118966f3b8e2SJens Axboe * requeue inode for re-scanning after bdi->b_io list is exhausted. 1190c986d1e2SAndrew Morton */ 1191f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb) 1192c986d1e2SAndrew Morton { 1193c7f54084SDave Chinner inode_io_list_move_locked(inode, wb, &wb->b_more_io); 1194c986d1e2SAndrew Morton } 1195c986d1e2SAndrew Morton 11961c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode) 11971c0eeaf5SJoern Engel { 1198365b94aeSJan Kara inode->i_state &= ~I_SYNC; 11994eff96ddSJan Kara /* If inode is clean an unused, put it into LRU now... */ 12004eff96ddSJan Kara inode_add_lru(inode); 1201365b94aeSJan Kara /* Waiters must see I_SYNC cleared before being woken up */ 12021c0eeaf5SJoern Engel smp_mb(); 12031c0eeaf5SJoern Engel wake_up_bit(&inode->i_state, __I_SYNC); 12041c0eeaf5SJoern Engel } 12051c0eeaf5SJoern Engel 1206d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t) 1207d2caa3c5SJeff Layton { 1208d2caa3c5SJeff Layton bool ret = time_after(inode->dirtied_when, t); 1209d2caa3c5SJeff Layton #ifndef CONFIG_64BIT 1210d2caa3c5SJeff Layton /* 1211d2caa3c5SJeff Layton * For inodes being constantly redirtied, dirtied_when can get stuck. 1212d2caa3c5SJeff Layton * It _appears_ to be in the future, but is actually in distant past. 1213d2caa3c5SJeff Layton * This test is necessary to prevent such wrapped-around relative times 12145b0830cbSJens Axboe * from permanently stopping the whole bdi writeback. 1215d2caa3c5SJeff Layton */ 1216d2caa3c5SJeff Layton ret = ret && time_before_eq(inode->dirtied_when, jiffies); 1217d2caa3c5SJeff Layton #endif 1218d2caa3c5SJeff Layton return ret; 1219d2caa3c5SJeff Layton } 1220d2caa3c5SJeff Layton 12210ae45f63STheodore Ts'o #define EXPIRE_DIRTY_ATIME 0x0001 12220ae45f63STheodore Ts'o 1223c986d1e2SAndrew Morton /* 12240e2f2b23SWang Sheng-Hui * Move expired (dirtied before work->older_than_this) dirty inodes from 1225697e6fedSJan Kara * @delaying_queue to @dispatch_queue. 12262c136579SFengguang Wu */ 1227e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue, 12282c136579SFengguang Wu struct list_head *dispatch_queue, 12290ae45f63STheodore Ts'o int flags, 1230ad4e38ddSCurt Wohlgemuth struct wb_writeback_work *work) 12312c136579SFengguang Wu { 12320ae45f63STheodore Ts'o unsigned long *older_than_this = NULL; 12330ae45f63STheodore Ts'o unsigned long expire_time; 12345c03449dSShaohua Li LIST_HEAD(tmp); 12355c03449dSShaohua Li struct list_head *pos, *node; 1236cf137307SJens Axboe struct super_block *sb = NULL; 12375c03449dSShaohua Li struct inode *inode; 1238cf137307SJens Axboe int do_sb_sort = 0; 1239e84d0a4fSWu Fengguang int moved = 0; 12405c03449dSShaohua Li 12410ae45f63STheodore Ts'o if ((flags & EXPIRE_DIRTY_ATIME) == 0) 12420ae45f63STheodore Ts'o older_than_this = work->older_than_this; 1243a2f48706STheodore Ts'o else if (!work->for_sync) { 1244a2f48706STheodore Ts'o expire_time = jiffies - (dirtytime_expire_interval * HZ); 12450ae45f63STheodore Ts'o older_than_this = &expire_time; 12460ae45f63STheodore Ts'o } 12472c136579SFengguang Wu while (!list_empty(delaying_queue)) { 12487ccf19a8SNick Piggin inode = wb_inode(delaying_queue->prev); 12490ae45f63STheodore Ts'o if (older_than_this && 12500ae45f63STheodore Ts'o inode_dirtied_after(inode, *older_than_this)) 12512c136579SFengguang Wu break; 1252c7f54084SDave Chinner list_move(&inode->i_io_list, &tmp); 1253a8855990SJan Kara moved++; 12540ae45f63STheodore Ts'o if (flags & EXPIRE_DIRTY_ATIME) 12550ae45f63STheodore Ts'o set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state); 1256a8855990SJan Kara if (sb_is_blkdev_sb(inode->i_sb)) 1257a8855990SJan Kara continue; 1258cf137307SJens Axboe if (sb && sb != inode->i_sb) 1259cf137307SJens Axboe do_sb_sort = 1; 1260cf137307SJens Axboe sb = inode->i_sb; 12615c03449dSShaohua Li } 12625c03449dSShaohua Li 1263cf137307SJens Axboe /* just one sb in list, splice to dispatch_queue and we're done */ 1264cf137307SJens Axboe if (!do_sb_sort) { 1265cf137307SJens Axboe list_splice(&tmp, dispatch_queue); 1266e84d0a4fSWu Fengguang goto out; 1267cf137307SJens Axboe } 1268cf137307SJens Axboe 12695c03449dSShaohua Li /* Move inodes from one superblock together */ 12705c03449dSShaohua Li while (!list_empty(&tmp)) { 12717ccf19a8SNick Piggin sb = wb_inode(tmp.prev)->i_sb; 12725c03449dSShaohua Li list_for_each_prev_safe(pos, node, &tmp) { 12737ccf19a8SNick Piggin inode = wb_inode(pos); 12745c03449dSShaohua Li if (inode->i_sb == sb) 1275c7f54084SDave Chinner list_move(&inode->i_io_list, dispatch_queue); 12762c136579SFengguang Wu } 12772c136579SFengguang Wu } 1278e84d0a4fSWu Fengguang out: 1279e84d0a4fSWu Fengguang return moved; 12805c03449dSShaohua Li } 12812c136579SFengguang Wu 12822c136579SFengguang Wu /* 12832c136579SFengguang Wu * Queue all expired dirty inodes for io, eldest first. 12844ea879b9SWu Fengguang * Before 12854ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 12864ea879b9SWu Fengguang * =============> gf edc BA 12874ea879b9SWu Fengguang * After 12884ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 12894ea879b9SWu Fengguang * =============> g fBAedc 12904ea879b9SWu Fengguang * | 12914ea879b9SWu Fengguang * +--> dequeue for IO 12922c136579SFengguang Wu */ 1293ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work) 12942c136579SFengguang Wu { 1295e84d0a4fSWu Fengguang int moved; 12960ae45f63STheodore Ts'o 1297f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 12984ea879b9SWu Fengguang list_splice_init(&wb->b_more_io, &wb->b_io); 12990ae45f63STheodore Ts'o moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work); 13000ae45f63STheodore Ts'o moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io, 13010ae45f63STheodore Ts'o EXPIRE_DIRTY_ATIME, work); 1302d6c10f1fSTejun Heo if (moved) 1303d6c10f1fSTejun Heo wb_io_lists_populated(wb); 1304ad4e38ddSCurt Wohlgemuth trace_writeback_queue_io(wb, work, moved); 130566f3b8e2SJens Axboe } 130666f3b8e2SJens Axboe 1307a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc) 130866f3b8e2SJens Axboe { 13099fb0a7daSTejun Heo int ret; 13109fb0a7daSTejun Heo 13119fb0a7daSTejun Heo if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) { 13129fb0a7daSTejun Heo trace_writeback_write_inode_start(inode, wbc); 13139fb0a7daSTejun Heo ret = inode->i_sb->s_op->write_inode(inode, wbc); 13149fb0a7daSTejun Heo trace_writeback_write_inode(inode, wbc); 13159fb0a7daSTejun Heo return ret; 13169fb0a7daSTejun Heo } 131703ba3782SJens Axboe return 0; 131866f3b8e2SJens Axboe } 131908d8e974SFengguang Wu 13202c136579SFengguang Wu /* 1321169ebd90SJan Kara * Wait for writeback on an inode to complete. Called with i_lock held. 1322169ebd90SJan Kara * Caller must make sure inode cannot go away when we drop i_lock. 132301c03194SChristoph Hellwig */ 1324169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode) 1325169ebd90SJan Kara __releases(inode->i_lock) 1326169ebd90SJan Kara __acquires(inode->i_lock) 132701c03194SChristoph Hellwig { 132801c03194SChristoph Hellwig DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); 132901c03194SChristoph Hellwig wait_queue_head_t *wqh; 133001c03194SChristoph Hellwig 133101c03194SChristoph Hellwig wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 133258a9d3d8SRichard Kennedy while (inode->i_state & I_SYNC) { 1333250df6edSDave Chinner spin_unlock(&inode->i_lock); 133474316201SNeilBrown __wait_on_bit(wqh, &wq, bit_wait, 133574316201SNeilBrown TASK_UNINTERRUPTIBLE); 1336250df6edSDave Chinner spin_lock(&inode->i_lock); 133758a9d3d8SRichard Kennedy } 133801c03194SChristoph Hellwig } 133901c03194SChristoph Hellwig 134001c03194SChristoph Hellwig /* 1341169ebd90SJan Kara * Wait for writeback on an inode to complete. Caller must have inode pinned. 1342169ebd90SJan Kara */ 1343169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode) 1344169ebd90SJan Kara { 1345169ebd90SJan Kara spin_lock(&inode->i_lock); 1346169ebd90SJan Kara __inode_wait_for_writeback(inode); 1347169ebd90SJan Kara spin_unlock(&inode->i_lock); 1348169ebd90SJan Kara } 1349169ebd90SJan Kara 1350169ebd90SJan Kara /* 1351169ebd90SJan Kara * Sleep until I_SYNC is cleared. This function must be called with i_lock 1352169ebd90SJan Kara * held and drops it. It is aimed for callers not holding any inode reference 1353169ebd90SJan Kara * so once i_lock is dropped, inode can go away. 1354169ebd90SJan Kara */ 1355169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode) 1356169ebd90SJan Kara __releases(inode->i_lock) 1357169ebd90SJan Kara { 1358169ebd90SJan Kara DEFINE_WAIT(wait); 1359169ebd90SJan Kara wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 1360169ebd90SJan Kara int sleep; 1361169ebd90SJan Kara 1362169ebd90SJan Kara prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); 1363169ebd90SJan Kara sleep = inode->i_state & I_SYNC; 1364169ebd90SJan Kara spin_unlock(&inode->i_lock); 1365169ebd90SJan Kara if (sleep) 1366169ebd90SJan Kara schedule(); 1367169ebd90SJan Kara finish_wait(wqh, &wait); 1368169ebd90SJan Kara } 1369169ebd90SJan Kara 1370169ebd90SJan Kara /* 1371ccb26b5aSJan Kara * Find proper writeback list for the inode depending on its current state and 1372ccb26b5aSJan Kara * possibly also change of its state while we were doing writeback. Here we 1373ccb26b5aSJan Kara * handle things such as livelock prevention or fairness of writeback among 1374ccb26b5aSJan Kara * inodes. This function can be called only by flusher thread - noone else 1375ccb26b5aSJan Kara * processes all inodes in writeback lists and requeueing inodes behind flusher 1376ccb26b5aSJan Kara * thread's back can have unexpected consequences. 1377ccb26b5aSJan Kara */ 1378ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb, 1379ccb26b5aSJan Kara struct writeback_control *wbc) 1380ccb26b5aSJan Kara { 1381ccb26b5aSJan Kara if (inode->i_state & I_FREEING) 1382ccb26b5aSJan Kara return; 1383ccb26b5aSJan Kara 1384ccb26b5aSJan Kara /* 1385ccb26b5aSJan Kara * Sync livelock prevention. Each inode is tagged and synced in one 1386ccb26b5aSJan Kara * shot. If still dirty, it will be redirty_tail()'ed below. Update 1387ccb26b5aSJan Kara * the dirty time to prevent enqueue and sync it again. 1388ccb26b5aSJan Kara */ 1389ccb26b5aSJan Kara if ((inode->i_state & I_DIRTY) && 1390ccb26b5aSJan Kara (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)) 1391ccb26b5aSJan Kara inode->dirtied_when = jiffies; 1392ccb26b5aSJan Kara 13934f8ad655SJan Kara if (wbc->pages_skipped) { 13944f8ad655SJan Kara /* 13954f8ad655SJan Kara * writeback is not making progress due to locked 13964f8ad655SJan Kara * buffers. Skip this inode for now. 13974f8ad655SJan Kara */ 13984f8ad655SJan Kara redirty_tail(inode, wb); 13994f8ad655SJan Kara return; 14004f8ad655SJan Kara } 14014f8ad655SJan Kara 1402ccb26b5aSJan Kara if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) { 1403ccb26b5aSJan Kara /* 1404ccb26b5aSJan Kara * We didn't write back all the pages. nfs_writepages() 1405ccb26b5aSJan Kara * sometimes bales out without doing anything. 1406ccb26b5aSJan Kara */ 1407ccb26b5aSJan Kara if (wbc->nr_to_write <= 0) { 1408ccb26b5aSJan Kara /* Slice used up. Queue for next turn. */ 1409ccb26b5aSJan Kara requeue_io(inode, wb); 1410ccb26b5aSJan Kara } else { 1411ccb26b5aSJan Kara /* 1412ccb26b5aSJan Kara * Writeback blocked by something other than 1413ccb26b5aSJan Kara * congestion. Delay the inode for some time to 1414ccb26b5aSJan Kara * avoid spinning on the CPU (100% iowait) 1415ccb26b5aSJan Kara * retrying writeback of the dirty page/inode 1416ccb26b5aSJan Kara * that cannot be performed immediately. 1417ccb26b5aSJan Kara */ 1418ccb26b5aSJan Kara redirty_tail(inode, wb); 1419ccb26b5aSJan Kara } 1420ccb26b5aSJan Kara } else if (inode->i_state & I_DIRTY) { 1421ccb26b5aSJan Kara /* 1422ccb26b5aSJan Kara * Filesystems can dirty the inode during writeback operations, 1423ccb26b5aSJan Kara * such as delayed allocation during submission or metadata 1424ccb26b5aSJan Kara * updates after data IO completion. 1425ccb26b5aSJan Kara */ 1426ccb26b5aSJan Kara redirty_tail(inode, wb); 14270ae45f63STheodore Ts'o } else if (inode->i_state & I_DIRTY_TIME) { 1428a2f48706STheodore Ts'o inode->dirtied_when = jiffies; 1429c7f54084SDave Chinner inode_io_list_move_locked(inode, wb, &wb->b_dirty_time); 1430ccb26b5aSJan Kara } else { 1431ccb26b5aSJan Kara /* The inode is clean. Remove from writeback lists. */ 1432c7f54084SDave Chinner inode_io_list_del_locked(inode, wb); 1433ccb26b5aSJan Kara } 1434ccb26b5aSJan Kara } 1435ccb26b5aSJan Kara 1436ccb26b5aSJan Kara /* 14374f8ad655SJan Kara * Write out an inode and its dirty pages. Do not update the writeback list 14384f8ad655SJan Kara * linkage. That is left to the caller. The caller is also responsible for 14394f8ad655SJan Kara * setting I_SYNC flag and calling inode_sync_complete() to clear it. 14401da177e4SLinus Torvalds */ 14411da177e4SLinus Torvalds static int 1442cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) 14431da177e4SLinus Torvalds { 14441da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 1445251d6a47SWu Fengguang long nr_to_write = wbc->nr_to_write; 144601c03194SChristoph Hellwig unsigned dirty; 14471da177e4SLinus Torvalds int ret; 14481da177e4SLinus Torvalds 14494f8ad655SJan Kara WARN_ON(!(inode->i_state & I_SYNC)); 14501da177e4SLinus Torvalds 14519fb0a7daSTejun Heo trace_writeback_single_inode_start(inode, wbc, nr_to_write); 14529fb0a7daSTejun Heo 14531da177e4SLinus Torvalds ret = do_writepages(mapping, wbc); 14541da177e4SLinus Torvalds 145526821ed4SChristoph Hellwig /* 145626821ed4SChristoph Hellwig * Make sure to wait on the data before writing out the metadata. 145726821ed4SChristoph Hellwig * This is important for filesystems that modify metadata on data 14587747bd4bSDave Chinner * I/O completion. We don't do it for sync(2) writeback because it has a 14597747bd4bSDave Chinner * separate, external IO completion path and ->sync_fs for guaranteeing 14607747bd4bSDave Chinner * inode metadata is written back correctly. 146126821ed4SChristoph Hellwig */ 14627747bd4bSDave Chinner if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) { 146326821ed4SChristoph Hellwig int err = filemap_fdatawait(mapping); 14641da177e4SLinus Torvalds if (ret == 0) 14651da177e4SLinus Torvalds ret = err; 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds 14685547e8aaSDmitry Monakhov /* 14695547e8aaSDmitry Monakhov * Some filesystems may redirty the inode during the writeback 14705547e8aaSDmitry Monakhov * due to delalloc, clear dirty metadata flags right before 14715547e8aaSDmitry Monakhov * write_inode() 14725547e8aaSDmitry Monakhov */ 1473250df6edSDave Chinner spin_lock(&inode->i_lock); 14749c6ac78eSTejun Heo 14755547e8aaSDmitry Monakhov dirty = inode->i_state & I_DIRTY; 1476a2f48706STheodore Ts'o if (inode->i_state & I_DIRTY_TIME) { 14770e11f644SChristoph Hellwig if ((dirty & I_DIRTY_INODE) || 1478dc5ff2b1SJan Kara wbc->sync_mode == WB_SYNC_ALL || 1479a2f48706STheodore Ts'o unlikely(inode->i_state & I_DIRTY_TIME_EXPIRED) || 1480a2f48706STheodore Ts'o unlikely(time_after(jiffies, 1481a2f48706STheodore Ts'o (inode->dirtied_time_when + 1482a2f48706STheodore Ts'o dirtytime_expire_interval * HZ)))) { 14830ae45f63STheodore Ts'o dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED; 14840ae45f63STheodore Ts'o trace_writeback_lazytime(inode); 14850ae45f63STheodore Ts'o } 1486a2f48706STheodore Ts'o } else 1487a2f48706STheodore Ts'o inode->i_state &= ~I_DIRTY_TIME_EXPIRED; 14880ae45f63STheodore Ts'o inode->i_state &= ~dirty; 14899c6ac78eSTejun Heo 14909c6ac78eSTejun Heo /* 14919c6ac78eSTejun Heo * Paired with smp_mb() in __mark_inode_dirty(). This allows 14929c6ac78eSTejun Heo * __mark_inode_dirty() to test i_state without grabbing i_lock - 14939c6ac78eSTejun Heo * either they see the I_DIRTY bits cleared or we see the dirtied 14949c6ac78eSTejun Heo * inode. 14959c6ac78eSTejun Heo * 14969c6ac78eSTejun Heo * I_DIRTY_PAGES is always cleared together above even if @mapping 14979c6ac78eSTejun Heo * still has dirty pages. The flag is reinstated after smp_mb() if 14989c6ac78eSTejun Heo * necessary. This guarantees that either __mark_inode_dirty() 14999c6ac78eSTejun Heo * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY. 15009c6ac78eSTejun Heo */ 15019c6ac78eSTejun Heo smp_mb(); 15029c6ac78eSTejun Heo 15039c6ac78eSTejun Heo if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 15049c6ac78eSTejun Heo inode->i_state |= I_DIRTY_PAGES; 15059c6ac78eSTejun Heo 1506250df6edSDave Chinner spin_unlock(&inode->i_lock); 15079c6ac78eSTejun Heo 15080ae45f63STheodore Ts'o if (dirty & I_DIRTY_TIME) 15090ae45f63STheodore Ts'o mark_inode_dirty_sync(inode); 151026821ed4SChristoph Hellwig /* Don't write the inode if only I_DIRTY_PAGES was set */ 15110ae45f63STheodore Ts'o if (dirty & ~I_DIRTY_PAGES) { 1512a9185b41SChristoph Hellwig int err = write_inode(inode, wbc); 15131da177e4SLinus Torvalds if (ret == 0) 15141da177e4SLinus Torvalds ret = err; 15151da177e4SLinus Torvalds } 15164f8ad655SJan Kara trace_writeback_single_inode(inode, wbc, nr_to_write); 15174f8ad655SJan Kara return ret; 15184f8ad655SJan Kara } 15194f8ad655SJan Kara 15204f8ad655SJan Kara /* 15214f8ad655SJan Kara * Write out an inode's dirty pages. Either the caller has an active reference 15224f8ad655SJan Kara * on the inode or the inode has I_WILL_FREE set. 15234f8ad655SJan Kara * 15244f8ad655SJan Kara * This function is designed to be called for writing back one inode which 15254f8ad655SJan Kara * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode() 15264f8ad655SJan Kara * and does more profound writeback list handling in writeback_sb_inodes(). 15274f8ad655SJan Kara */ 1528aaf25593STejun Heo static int writeback_single_inode(struct inode *inode, 15294f8ad655SJan Kara struct writeback_control *wbc) 15304f8ad655SJan Kara { 1531aaf25593STejun Heo struct bdi_writeback *wb; 15324f8ad655SJan Kara int ret = 0; 15334f8ad655SJan Kara 15344f8ad655SJan Kara spin_lock(&inode->i_lock); 15354f8ad655SJan Kara if (!atomic_read(&inode->i_count)) 15364f8ad655SJan Kara WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); 15374f8ad655SJan Kara else 15384f8ad655SJan Kara WARN_ON(inode->i_state & I_WILL_FREE); 15394f8ad655SJan Kara 15404f8ad655SJan Kara if (inode->i_state & I_SYNC) { 15414f8ad655SJan Kara if (wbc->sync_mode != WB_SYNC_ALL) 15424f8ad655SJan Kara goto out; 15434f8ad655SJan Kara /* 1544169ebd90SJan Kara * It's a data-integrity sync. We must wait. Since callers hold 1545169ebd90SJan Kara * inode reference or inode has I_WILL_FREE set, it cannot go 1546169ebd90SJan Kara * away under us. 15474f8ad655SJan Kara */ 1548169ebd90SJan Kara __inode_wait_for_writeback(inode); 15494f8ad655SJan Kara } 15504f8ad655SJan Kara WARN_ON(inode->i_state & I_SYNC); 15514f8ad655SJan Kara /* 1552f9b0e058SJan Kara * Skip inode if it is clean and we have no outstanding writeback in 1553f9b0e058SJan Kara * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this 1554f9b0e058SJan Kara * function since flusher thread may be doing for example sync in 1555f9b0e058SJan Kara * parallel and if we move the inode, it could get skipped. So here we 1556f9b0e058SJan Kara * make sure inode is on some writeback list and leave it there unless 1557f9b0e058SJan Kara * we have completely cleaned the inode. 15584f8ad655SJan Kara */ 15590ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL) && 1560f9b0e058SJan Kara (wbc->sync_mode != WB_SYNC_ALL || 1561f9b0e058SJan Kara !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))) 15624f8ad655SJan Kara goto out; 15634f8ad655SJan Kara inode->i_state |= I_SYNC; 1564b16b1debSTejun Heo wbc_attach_and_unlock_inode(wbc, inode); 15654f8ad655SJan Kara 1566cd8ed2a4SYan Hong ret = __writeback_single_inode(inode, wbc); 15671da177e4SLinus Torvalds 1568b16b1debSTejun Heo wbc_detach_inode(wbc); 1569aaf25593STejun Heo 1570aaf25593STejun Heo wb = inode_to_wb_and_lock_list(inode); 1571250df6edSDave Chinner spin_lock(&inode->i_lock); 15724f8ad655SJan Kara /* 15734f8ad655SJan Kara * If inode is clean, remove it from writeback lists. Otherwise don't 15744f8ad655SJan Kara * touch it. See comment above for explanation. 15754f8ad655SJan Kara */ 15760ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 1577c7f54084SDave Chinner inode_io_list_del_locked(inode, wb); 15784f8ad655SJan Kara spin_unlock(&wb->list_lock); 15791c0eeaf5SJoern Engel inode_sync_complete(inode); 15804f8ad655SJan Kara out: 15814f8ad655SJan Kara spin_unlock(&inode->i_lock); 15821da177e4SLinus Torvalds return ret; 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds 1585a88a341aSTejun Heo static long writeback_chunk_size(struct bdi_writeback *wb, 15861a12d8bdSWu Fengguang struct wb_writeback_work *work) 1587d46db3d5SWu Fengguang { 1588d46db3d5SWu Fengguang long pages; 1589d46db3d5SWu Fengguang 1590d46db3d5SWu Fengguang /* 1591d46db3d5SWu Fengguang * WB_SYNC_ALL mode does livelock avoidance by syncing dirty 1592d46db3d5SWu Fengguang * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX 1593d46db3d5SWu Fengguang * here avoids calling into writeback_inodes_wb() more than once. 1594d46db3d5SWu Fengguang * 1595d46db3d5SWu Fengguang * The intended call sequence for WB_SYNC_ALL writeback is: 1596d46db3d5SWu Fengguang * 1597d46db3d5SWu Fengguang * wb_writeback() 1598d46db3d5SWu Fengguang * writeback_sb_inodes() <== called only once 1599d46db3d5SWu Fengguang * write_cache_pages() <== called once for each inode 1600d46db3d5SWu Fengguang * (quickly) tag currently dirty pages 1601d46db3d5SWu Fengguang * (maybe slowly) sync all tagged pages 1602d46db3d5SWu Fengguang */ 1603d46db3d5SWu Fengguang if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages) 1604d46db3d5SWu Fengguang pages = LONG_MAX; 16051a12d8bdSWu Fengguang else { 1606a88a341aSTejun Heo pages = min(wb->avg_write_bandwidth / 2, 1607dcc25ae7STejun Heo global_wb_domain.dirty_limit / DIRTY_SCOPE); 16081a12d8bdSWu Fengguang pages = min(pages, work->nr_pages); 16091a12d8bdSWu Fengguang pages = round_down(pages + MIN_WRITEBACK_PAGES, 16101a12d8bdSWu Fengguang MIN_WRITEBACK_PAGES); 16111a12d8bdSWu Fengguang } 1612d46db3d5SWu Fengguang 1613d46db3d5SWu Fengguang return pages; 1614d46db3d5SWu Fengguang } 1615d46db3d5SWu Fengguang 161603ba3782SJens Axboe /* 1617f11c9c5cSEdward Shishkin * Write a portion of b_io inodes which belong to @sb. 1618edadfb10SChristoph Hellwig * 1619d46db3d5SWu Fengguang * Return the number of pages and/or inodes written. 16200ba13fd1SLinus Torvalds * 16210ba13fd1SLinus Torvalds * NOTE! This is called with wb->list_lock held, and will 16220ba13fd1SLinus Torvalds * unlock and relock that for each inode it ends up doing 16230ba13fd1SLinus Torvalds * IO for. 1624f11c9c5cSEdward Shishkin */ 1625d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb, 1626d46db3d5SWu Fengguang struct bdi_writeback *wb, 1627d46db3d5SWu Fengguang struct wb_writeback_work *work) 162803ba3782SJens Axboe { 1629d46db3d5SWu Fengguang struct writeback_control wbc = { 1630d46db3d5SWu Fengguang .sync_mode = work->sync_mode, 1631d46db3d5SWu Fengguang .tagged_writepages = work->tagged_writepages, 1632d46db3d5SWu Fengguang .for_kupdate = work->for_kupdate, 1633d46db3d5SWu Fengguang .for_background = work->for_background, 16347747bd4bSDave Chinner .for_sync = work->for_sync, 1635d46db3d5SWu Fengguang .range_cyclic = work->range_cyclic, 1636d46db3d5SWu Fengguang .range_start = 0, 1637d46db3d5SWu Fengguang .range_end = LLONG_MAX, 1638d46db3d5SWu Fengguang }; 1639d46db3d5SWu Fengguang unsigned long start_time = jiffies; 1640d46db3d5SWu Fengguang long write_chunk; 1641d46db3d5SWu Fengguang long wrote = 0; /* count both pages and inodes */ 1642d46db3d5SWu Fengguang 164303ba3782SJens Axboe while (!list_empty(&wb->b_io)) { 16447ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 1645aaf25593STejun Heo struct bdi_writeback *tmp_wb; 1646edadfb10SChristoph Hellwig 1647edadfb10SChristoph Hellwig if (inode->i_sb != sb) { 1648d46db3d5SWu Fengguang if (work->sb) { 1649edadfb10SChristoph Hellwig /* 1650edadfb10SChristoph Hellwig * We only want to write back data for this 1651edadfb10SChristoph Hellwig * superblock, move all inodes not belonging 1652edadfb10SChristoph Hellwig * to it back onto the dirty list. 1653edadfb10SChristoph Hellwig */ 1654f758eeabSChristoph Hellwig redirty_tail(inode, wb); 165566f3b8e2SJens Axboe continue; 165666f3b8e2SJens Axboe } 1657edadfb10SChristoph Hellwig 1658edadfb10SChristoph Hellwig /* 1659edadfb10SChristoph Hellwig * The inode belongs to a different superblock. 1660edadfb10SChristoph Hellwig * Bounce back to the caller to unpin this and 1661edadfb10SChristoph Hellwig * pin the next superblock. 1662edadfb10SChristoph Hellwig */ 1663d46db3d5SWu Fengguang break; 1664edadfb10SChristoph Hellwig } 1665edadfb10SChristoph Hellwig 16669843b76aSChristoph Hellwig /* 1667331cbdeeSWanpeng Li * Don't bother with new inodes or inodes being freed, first 1668331cbdeeSWanpeng Li * kind does not need periodic writeout yet, and for the latter 16699843b76aSChristoph Hellwig * kind writeout is handled by the freer. 16709843b76aSChristoph Hellwig */ 1671250df6edSDave Chinner spin_lock(&inode->i_lock); 16729843b76aSChristoph Hellwig if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { 1673250df6edSDave Chinner spin_unlock(&inode->i_lock); 1674fcc5c222SWu Fengguang redirty_tail(inode, wb); 16757ef0d737SNick Piggin continue; 16767ef0d737SNick Piggin } 1677cc1676d9SJan Kara if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) { 1678cc1676d9SJan Kara /* 1679cc1676d9SJan Kara * If this inode is locked for writeback and we are not 1680cc1676d9SJan Kara * doing writeback-for-data-integrity, move it to 1681cc1676d9SJan Kara * b_more_io so that writeback can proceed with the 1682cc1676d9SJan Kara * other inodes on s_io. 1683cc1676d9SJan Kara * 1684cc1676d9SJan Kara * We'll have another go at writing back this inode 1685cc1676d9SJan Kara * when we completed a full scan of b_io. 1686cc1676d9SJan Kara */ 1687cc1676d9SJan Kara spin_unlock(&inode->i_lock); 1688cc1676d9SJan Kara requeue_io(inode, wb); 1689cc1676d9SJan Kara trace_writeback_sb_inodes_requeue(inode); 1690cc1676d9SJan Kara continue; 1691cc1676d9SJan Kara } 1692f0d07b7fSJan Kara spin_unlock(&wb->list_lock); 1693f0d07b7fSJan Kara 16944f8ad655SJan Kara /* 16954f8ad655SJan Kara * We already requeued the inode if it had I_SYNC set and we 16964f8ad655SJan Kara * are doing WB_SYNC_NONE writeback. So this catches only the 16974f8ad655SJan Kara * WB_SYNC_ALL case. 16984f8ad655SJan Kara */ 1699169ebd90SJan Kara if (inode->i_state & I_SYNC) { 1700169ebd90SJan Kara /* Wait for I_SYNC. This function drops i_lock... */ 1701169ebd90SJan Kara inode_sleep_on_writeback(inode); 1702169ebd90SJan Kara /* Inode may be gone, start again */ 1703ead188f9SJan Kara spin_lock(&wb->list_lock); 1704169ebd90SJan Kara continue; 1705169ebd90SJan Kara } 17064f8ad655SJan Kara inode->i_state |= I_SYNC; 1707b16b1debSTejun Heo wbc_attach_and_unlock_inode(&wbc, inode); 1708169ebd90SJan Kara 1709a88a341aSTejun Heo write_chunk = writeback_chunk_size(wb, work); 1710d46db3d5SWu Fengguang wbc.nr_to_write = write_chunk; 1711d46db3d5SWu Fengguang wbc.pages_skipped = 0; 1712250df6edSDave Chinner 1713169ebd90SJan Kara /* 1714169ebd90SJan Kara * We use I_SYNC to pin the inode in memory. While it is set 1715169ebd90SJan Kara * evict_inode() will wait so the inode cannot be freed. 1716169ebd90SJan Kara */ 1717cd8ed2a4SYan Hong __writeback_single_inode(inode, &wbc); 1718d46db3d5SWu Fengguang 1719b16b1debSTejun Heo wbc_detach_inode(&wbc); 1720d46db3d5SWu Fengguang work->nr_pages -= write_chunk - wbc.nr_to_write; 1721d46db3d5SWu Fengguang wrote += write_chunk - wbc.nr_to_write; 1722590dca3aSChris Mason 1723590dca3aSChris Mason if (need_resched()) { 1724590dca3aSChris Mason /* 1725590dca3aSChris Mason * We're trying to balance between building up a nice 1726590dca3aSChris Mason * long list of IOs to improve our merge rate, and 1727590dca3aSChris Mason * getting those IOs out quickly for anyone throttling 1728590dca3aSChris Mason * in balance_dirty_pages(). cond_resched() doesn't 1729590dca3aSChris Mason * unplug, so get our IOs out the door before we 1730590dca3aSChris Mason * give up the CPU. 1731590dca3aSChris Mason */ 1732590dca3aSChris Mason blk_flush_plug(current); 1733590dca3aSChris Mason cond_resched(); 1734590dca3aSChris Mason } 1735590dca3aSChris Mason 1736aaf25593STejun Heo /* 1737aaf25593STejun Heo * Requeue @inode if still dirty. Be careful as @inode may 1738aaf25593STejun Heo * have been switched to another wb in the meantime. 1739aaf25593STejun Heo */ 1740aaf25593STejun Heo tmp_wb = inode_to_wb_and_lock_list(inode); 17414f8ad655SJan Kara spin_lock(&inode->i_lock); 17420ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 1743d46db3d5SWu Fengguang wrote++; 1744aaf25593STejun Heo requeue_inode(inode, tmp_wb, &wbc); 17454f8ad655SJan Kara inode_sync_complete(inode); 17460f1b1fd8SDave Chinner spin_unlock(&inode->i_lock); 1747590dca3aSChris Mason 1748aaf25593STejun Heo if (unlikely(tmp_wb != wb)) { 1749aaf25593STejun Heo spin_unlock(&tmp_wb->list_lock); 1750aaf25593STejun Heo spin_lock(&wb->list_lock); 1751aaf25593STejun Heo } 1752aaf25593STejun Heo 1753d46db3d5SWu Fengguang /* 1754d46db3d5SWu Fengguang * bail out to wb_writeback() often enough to check 1755d46db3d5SWu Fengguang * background threshold and other termination conditions. 1756d46db3d5SWu Fengguang */ 1757d46db3d5SWu Fengguang if (wrote) { 1758d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 1759d46db3d5SWu Fengguang break; 1760d46db3d5SWu Fengguang if (work->nr_pages <= 0) 1761d46db3d5SWu Fengguang break; 17621da177e4SLinus Torvalds } 17638bc3be27SFengguang Wu } 1764d46db3d5SWu Fengguang return wrote; 1765f11c9c5cSEdward Shishkin } 176638f21977SNick Piggin 1767d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb, 1768d46db3d5SWu Fengguang struct wb_writeback_work *work) 1769f11c9c5cSEdward Shishkin { 1770d46db3d5SWu Fengguang unsigned long start_time = jiffies; 1771d46db3d5SWu Fengguang long wrote = 0; 1772f11c9c5cSEdward Shishkin 1773f11c9c5cSEdward Shishkin while (!list_empty(&wb->b_io)) { 17747ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 1775f11c9c5cSEdward Shishkin struct super_block *sb = inode->i_sb; 1776f11c9c5cSEdward Shishkin 1777eb6ef3dfSKonstantin Khlebnikov if (!trylock_super(sb)) { 17780e995816SWu Fengguang /* 1779eb6ef3dfSKonstantin Khlebnikov * trylock_super() may fail consistently due to 17800e995816SWu Fengguang * s_umount being grabbed by someone else. Don't use 17810e995816SWu Fengguang * requeue_io() to avoid busy retrying the inode/sb. 17820e995816SWu Fengguang */ 17830e995816SWu Fengguang redirty_tail(inode, wb); 1784d19de7edSChristoph Hellwig continue; 1785334132aeSChristoph Hellwig } 1786d46db3d5SWu Fengguang wrote += writeback_sb_inodes(sb, wb, work); 1787eb6ef3dfSKonstantin Khlebnikov up_read(&sb->s_umount); 1788f11c9c5cSEdward Shishkin 1789d46db3d5SWu Fengguang /* refer to the same tests at the end of writeback_sb_inodes */ 1790d46db3d5SWu Fengguang if (wrote) { 1791d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 1792d46db3d5SWu Fengguang break; 1793d46db3d5SWu Fengguang if (work->nr_pages <= 0) 1794f11c9c5cSEdward Shishkin break; 1795f11c9c5cSEdward Shishkin } 1796d46db3d5SWu Fengguang } 179766f3b8e2SJens Axboe /* Leave any unwritten inodes on b_io */ 1798d46db3d5SWu Fengguang return wrote; 179966f3b8e2SJens Axboe } 180066f3b8e2SJens Axboe 18017d9f073bSWanpeng Li static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages, 18020e175a18SCurt Wohlgemuth enum wb_reason reason) 1803edadfb10SChristoph Hellwig { 1804d46db3d5SWu Fengguang struct wb_writeback_work work = { 1805d46db3d5SWu Fengguang .nr_pages = nr_pages, 1806d46db3d5SWu Fengguang .sync_mode = WB_SYNC_NONE, 1807d46db3d5SWu Fengguang .range_cyclic = 1, 18080e175a18SCurt Wohlgemuth .reason = reason, 1809d46db3d5SWu Fengguang }; 1810505a666eSLinus Torvalds struct blk_plug plug; 1811edadfb10SChristoph Hellwig 1812505a666eSLinus Torvalds blk_start_plug(&plug); 1813f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 1814424b351fSWu Fengguang if (list_empty(&wb->b_io)) 1815ad4e38ddSCurt Wohlgemuth queue_io(wb, &work); 1816d46db3d5SWu Fengguang __writeback_inodes_wb(wb, &work); 1817f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 1818505a666eSLinus Torvalds blk_finish_plug(&plug); 1819edadfb10SChristoph Hellwig 1820d46db3d5SWu Fengguang return nr_pages - work.nr_pages; 182166f3b8e2SJens Axboe } 182266f3b8e2SJens Axboe 182303ba3782SJens Axboe /* 182403ba3782SJens Axboe * Explicit flushing or periodic writeback of "old" data. 182503ba3782SJens Axboe * 182603ba3782SJens Axboe * Define "old": the first time one of an inode's pages is dirtied, we mark the 182703ba3782SJens Axboe * dirtying-time in the inode's address_space. So this periodic writeback code 182803ba3782SJens Axboe * just walks the superblock inode list, writing back any inodes which are 182903ba3782SJens Axboe * older than a specific point in time. 183003ba3782SJens Axboe * 183103ba3782SJens Axboe * Try to run once per dirty_writeback_interval. But if a writeback event 183203ba3782SJens Axboe * takes longer than a dirty_writeback_interval interval, then leave a 183303ba3782SJens Axboe * one-second gap. 183403ba3782SJens Axboe * 183503ba3782SJens Axboe * older_than_this takes precedence over nr_to_write. So we'll only write back 183603ba3782SJens Axboe * all dirty pages if they are all attached to "old" mappings. 183703ba3782SJens Axboe */ 1838c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb, 183983ba7b07SChristoph Hellwig struct wb_writeback_work *work) 184003ba3782SJens Axboe { 1841e98be2d5SWu Fengguang unsigned long wb_start = jiffies; 1842d46db3d5SWu Fengguang long nr_pages = work->nr_pages; 18430dc83bd3SJan Kara unsigned long oldest_jif; 1844a5989bdcSJan Kara struct inode *inode; 1845d46db3d5SWu Fengguang long progress; 1846505a666eSLinus Torvalds struct blk_plug plug; 184703ba3782SJens Axboe 18480dc83bd3SJan Kara oldest_jif = jiffies; 18490dc83bd3SJan Kara work->older_than_this = &oldest_jif; 185003ba3782SJens Axboe 1851505a666eSLinus Torvalds blk_start_plug(&plug); 1852e8dfc305SWu Fengguang spin_lock(&wb->list_lock); 185303ba3782SJens Axboe for (;;) { 185403ba3782SJens Axboe /* 1855d3ddec76SWu Fengguang * Stop writeback when nr_pages has been consumed 185603ba3782SJens Axboe */ 185783ba7b07SChristoph Hellwig if (work->nr_pages <= 0) 185803ba3782SJens Axboe break; 185903ba3782SJens Axboe 186003ba3782SJens Axboe /* 1861aa373cf5SJan Kara * Background writeout and kupdate-style writeback may 1862aa373cf5SJan Kara * run forever. Stop them if there is other work to do 1863aa373cf5SJan Kara * so that e.g. sync can proceed. They'll be restarted 1864aa373cf5SJan Kara * after the other works are all done. 1865aa373cf5SJan Kara */ 1866aa373cf5SJan Kara if ((work->for_background || work->for_kupdate) && 1867f0054bb1STejun Heo !list_empty(&wb->work_list)) 1868aa373cf5SJan Kara break; 1869aa373cf5SJan Kara 1870aa373cf5SJan Kara /* 1871d3ddec76SWu Fengguang * For background writeout, stop when we are below the 1872d3ddec76SWu Fengguang * background dirty threshold 187303ba3782SJens Axboe */ 1874aa661bbeSTejun Heo if (work->for_background && !wb_over_bg_thresh(wb)) 187503ba3782SJens Axboe break; 187603ba3782SJens Axboe 18771bc36b64SJan Kara /* 18781bc36b64SJan Kara * Kupdate and background works are special and we want to 18791bc36b64SJan Kara * include all inodes that need writing. Livelock avoidance is 18801bc36b64SJan Kara * handled by these works yielding to any other work so we are 18811bc36b64SJan Kara * safe. 18821bc36b64SJan Kara */ 1883ba9aa839SWu Fengguang if (work->for_kupdate) { 18840dc83bd3SJan Kara oldest_jif = jiffies - 1885ba9aa839SWu Fengguang msecs_to_jiffies(dirty_expire_interval * 10); 18861bc36b64SJan Kara } else if (work->for_background) 18870dc83bd3SJan Kara oldest_jif = jiffies; 1888028c2dd1SDave Chinner 18895634cc2aSTejun Heo trace_writeback_start(wb, work); 1890e8dfc305SWu Fengguang if (list_empty(&wb->b_io)) 1891ad4e38ddSCurt Wohlgemuth queue_io(wb, work); 189283ba7b07SChristoph Hellwig if (work->sb) 1893d46db3d5SWu Fengguang progress = writeback_sb_inodes(work->sb, wb, work); 1894edadfb10SChristoph Hellwig else 1895d46db3d5SWu Fengguang progress = __writeback_inodes_wb(wb, work); 18965634cc2aSTejun Heo trace_writeback_written(wb, work); 1897028c2dd1SDave Chinner 1898e98be2d5SWu Fengguang wb_update_bandwidth(wb, wb_start); 189903ba3782SJens Axboe 190003ba3782SJens Axboe /* 190171fd05a8SJens Axboe * Did we write something? Try for more 1902e6fb6da2SWu Fengguang * 1903e6fb6da2SWu Fengguang * Dirty inodes are moved to b_io for writeback in batches. 1904e6fb6da2SWu Fengguang * The completion of the current batch does not necessarily 1905e6fb6da2SWu Fengguang * mean the overall work is done. So we keep looping as long 1906e6fb6da2SWu Fengguang * as made some progress on cleaning pages or inodes. 190771fd05a8SJens Axboe */ 1908d46db3d5SWu Fengguang if (progress) 190903ba3782SJens Axboe continue; 1910a5989bdcSJan Kara /* 1911e6fb6da2SWu Fengguang * No more inodes for IO, bail 1912a5989bdcSJan Kara */ 1913b7a2441fSWu Fengguang if (list_empty(&wb->b_more_io)) 191403ba3782SJens Axboe break; 191503ba3782SJens Axboe /* 19168010c3b6SJens Axboe * Nothing written. Wait for some inode to 19178010c3b6SJens Axboe * become available for writeback. Otherwise 19188010c3b6SJens Axboe * we'll just busyloop. 191903ba3782SJens Axboe */ 19205634cc2aSTejun Heo trace_writeback_wait(wb, work); 192103ba3782SJens Axboe inode = wb_inode(wb->b_more_io.prev); 1922250df6edSDave Chinner spin_lock(&inode->i_lock); 1923f0d07b7fSJan Kara spin_unlock(&wb->list_lock); 1924169ebd90SJan Kara /* This function drops i_lock... */ 1925169ebd90SJan Kara inode_sleep_on_writeback(inode); 1926f0d07b7fSJan Kara spin_lock(&wb->list_lock); 192703ba3782SJens Axboe } 1928e8dfc305SWu Fengguang spin_unlock(&wb->list_lock); 1929505a666eSLinus Torvalds blk_finish_plug(&plug); 193003ba3782SJens Axboe 1931d46db3d5SWu Fengguang return nr_pages - work->nr_pages; 193203ba3782SJens Axboe } 193303ba3782SJens Axboe 193403ba3782SJens Axboe /* 193583ba7b07SChristoph Hellwig * Return the next wb_writeback_work struct that hasn't been processed yet. 193603ba3782SJens Axboe */ 1937f0054bb1STejun Heo static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb) 193803ba3782SJens Axboe { 193983ba7b07SChristoph Hellwig struct wb_writeback_work *work = NULL; 194003ba3782SJens Axboe 1941f0054bb1STejun Heo spin_lock_bh(&wb->work_lock); 1942f0054bb1STejun Heo if (!list_empty(&wb->work_list)) { 1943f0054bb1STejun Heo work = list_entry(wb->work_list.next, 194483ba7b07SChristoph Hellwig struct wb_writeback_work, list); 194583ba7b07SChristoph Hellwig list_del_init(&work->list); 194603ba3782SJens Axboe } 1947f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 194883ba7b07SChristoph Hellwig return work; 194903ba3782SJens Axboe } 195003ba3782SJens Axboe 19516585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb) 19526585027aSJan Kara { 1953aa661bbeSTejun Heo if (wb_over_bg_thresh(wb)) { 19546585027aSJan Kara 19556585027aSJan Kara struct wb_writeback_work work = { 19566585027aSJan Kara .nr_pages = LONG_MAX, 19576585027aSJan Kara .sync_mode = WB_SYNC_NONE, 19586585027aSJan Kara .for_background = 1, 19596585027aSJan Kara .range_cyclic = 1, 19600e175a18SCurt Wohlgemuth .reason = WB_REASON_BACKGROUND, 19616585027aSJan Kara }; 19626585027aSJan Kara 19636585027aSJan Kara return wb_writeback(wb, &work); 19646585027aSJan Kara } 19656585027aSJan Kara 19666585027aSJan Kara return 0; 19676585027aSJan Kara } 19686585027aSJan Kara 196903ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb) 197003ba3782SJens Axboe { 197103ba3782SJens Axboe unsigned long expired; 197203ba3782SJens Axboe long nr_pages; 197303ba3782SJens Axboe 197469b62d01SJens Axboe /* 197569b62d01SJens Axboe * When set to zero, disable periodic writeback 197669b62d01SJens Axboe */ 197769b62d01SJens Axboe if (!dirty_writeback_interval) 197869b62d01SJens Axboe return 0; 197969b62d01SJens Axboe 198003ba3782SJens Axboe expired = wb->last_old_flush + 198103ba3782SJens Axboe msecs_to_jiffies(dirty_writeback_interval * 10); 198203ba3782SJens Axboe if (time_before(jiffies, expired)) 198303ba3782SJens Axboe return 0; 198403ba3782SJens Axboe 198503ba3782SJens Axboe wb->last_old_flush = jiffies; 1986cdf01dd5SLinus Torvalds nr_pages = get_nr_dirty_pages(); 198703ba3782SJens Axboe 1988c4a77a6cSJens Axboe if (nr_pages) { 198983ba7b07SChristoph Hellwig struct wb_writeback_work work = { 1990c4a77a6cSJens Axboe .nr_pages = nr_pages, 1991c4a77a6cSJens Axboe .sync_mode = WB_SYNC_NONE, 1992c4a77a6cSJens Axboe .for_kupdate = 1, 1993c4a77a6cSJens Axboe .range_cyclic = 1, 19940e175a18SCurt Wohlgemuth .reason = WB_REASON_PERIODIC, 1995c4a77a6cSJens Axboe }; 1996c4a77a6cSJens Axboe 199783ba7b07SChristoph Hellwig return wb_writeback(wb, &work); 1998c4a77a6cSJens Axboe } 199903ba3782SJens Axboe 200003ba3782SJens Axboe return 0; 200103ba3782SJens Axboe } 200203ba3782SJens Axboe 200385009b4fSJens Axboe static long wb_check_start_all(struct bdi_writeback *wb) 200485009b4fSJens Axboe { 200585009b4fSJens Axboe long nr_pages; 200685009b4fSJens Axboe 200785009b4fSJens Axboe if (!test_bit(WB_start_all, &wb->state)) 200885009b4fSJens Axboe return 0; 200985009b4fSJens Axboe 201085009b4fSJens Axboe nr_pages = get_nr_dirty_pages(); 201185009b4fSJens Axboe if (nr_pages) { 201285009b4fSJens Axboe struct wb_writeback_work work = { 201385009b4fSJens Axboe .nr_pages = wb_split_bdi_pages(wb, nr_pages), 201485009b4fSJens Axboe .sync_mode = WB_SYNC_NONE, 201585009b4fSJens Axboe .range_cyclic = 1, 201685009b4fSJens Axboe .reason = wb->start_all_reason, 201785009b4fSJens Axboe }; 201885009b4fSJens Axboe 201985009b4fSJens Axboe nr_pages = wb_writeback(wb, &work); 202085009b4fSJens Axboe } 202185009b4fSJens Axboe 202285009b4fSJens Axboe clear_bit(WB_start_all, &wb->state); 202385009b4fSJens Axboe return nr_pages; 202485009b4fSJens Axboe } 202585009b4fSJens Axboe 202685009b4fSJens Axboe 202703ba3782SJens Axboe /* 202803ba3782SJens Axboe * Retrieve work items and do the writeback they describe 202903ba3782SJens Axboe */ 203025d130baSWanpeng Li static long wb_do_writeback(struct bdi_writeback *wb) 203103ba3782SJens Axboe { 203283ba7b07SChristoph Hellwig struct wb_writeback_work *work; 2033c4a77a6cSJens Axboe long wrote = 0; 203403ba3782SJens Axboe 20354452226eSTejun Heo set_bit(WB_writeback_running, &wb->state); 2036f0054bb1STejun Heo while ((work = get_next_work_item(wb)) != NULL) { 20375634cc2aSTejun Heo trace_writeback_exec(wb, work); 203883ba7b07SChristoph Hellwig wrote += wb_writeback(wb, work); 20394a3a485bSTahsin Erdogan finish_writeback_work(wb, work); 204003ba3782SJens Axboe } 204103ba3782SJens Axboe 204203ba3782SJens Axboe /* 204385009b4fSJens Axboe * Check for a flush-everything request 204485009b4fSJens Axboe */ 204585009b4fSJens Axboe wrote += wb_check_start_all(wb); 204685009b4fSJens Axboe 204785009b4fSJens Axboe /* 204803ba3782SJens Axboe * Check for periodic writeback, kupdated() style 204903ba3782SJens Axboe */ 205003ba3782SJens Axboe wrote += wb_check_old_data_flush(wb); 20516585027aSJan Kara wrote += wb_check_background_flush(wb); 20524452226eSTejun Heo clear_bit(WB_writeback_running, &wb->state); 205303ba3782SJens Axboe 205403ba3782SJens Axboe return wrote; 205503ba3782SJens Axboe } 205603ba3782SJens Axboe 205703ba3782SJens Axboe /* 205803ba3782SJens Axboe * Handle writeback of dirty data for the device backed by this bdi. Also 2059839a8e86STejun Heo * reschedules periodically and does kupdated style flushing. 206003ba3782SJens Axboe */ 2061f0054bb1STejun Heo void wb_workfn(struct work_struct *work) 206203ba3782SJens Axboe { 2063839a8e86STejun Heo struct bdi_writeback *wb = container_of(to_delayed_work(work), 2064839a8e86STejun Heo struct bdi_writeback, dwork); 206503ba3782SJens Axboe long pages_written; 206603ba3782SJens Axboe 206768f23b89STheodore Ts'o set_worker_desc("flush-%s", bdi_dev_name(wb->bdi)); 2068766f9164SPeter Zijlstra current->flags |= PF_SWAPWRITE; 206903ba3782SJens Axboe 2070839a8e86STejun Heo if (likely(!current_is_workqueue_rescuer() || 20714452226eSTejun Heo !test_bit(WB_registered, &wb->state))) { 207203ba3782SJens Axboe /* 2073f0054bb1STejun Heo * The normal path. Keep writing back @wb until its 2074839a8e86STejun Heo * work_list is empty. Note that this path is also taken 2075f0054bb1STejun Heo * if @wb is shutting down even when we're running off the 2076839a8e86STejun Heo * rescuer as work_list needs to be drained. 207703ba3782SJens Axboe */ 2078839a8e86STejun Heo do { 207925d130baSWanpeng Li pages_written = wb_do_writeback(wb); 2080455b2864SDave Chinner trace_writeback_pages_written(pages_written); 2081f0054bb1STejun Heo } while (!list_empty(&wb->work_list)); 2082839a8e86STejun Heo } else { 2083253c34e9SArtem Bityutskiy /* 2084839a8e86STejun Heo * bdi_wq can't get enough workers and we're running off 2085839a8e86STejun Heo * the emergency worker. Don't hog it. Hopefully, 1024 is 2086839a8e86STejun Heo * enough for efficient IO. 2087253c34e9SArtem Bityutskiy */ 2088f0054bb1STejun Heo pages_written = writeback_inodes_wb(wb, 1024, 2089839a8e86STejun Heo WB_REASON_FORKER_THREAD); 2090839a8e86STejun Heo trace_writeback_pages_written(pages_written); 209103ba3782SJens Axboe } 209203ba3782SJens Axboe 2093f0054bb1STejun Heo if (!list_empty(&wb->work_list)) 2094b8b78495SJan Kara wb_wakeup(wb); 20956ca738d6SDerek Basehore else if (wb_has_dirty_io(wb) && dirty_writeback_interval) 2096f0054bb1STejun Heo wb_wakeup_delayed(wb); 2097455b2864SDave Chinner 2098839a8e86STejun Heo current->flags &= ~PF_SWAPWRITE; 209903ba3782SJens Axboe } 210003ba3782SJens Axboe 210103ba3782SJens Axboe /* 2102595043e5SJens Axboe * Start writeback of `nr_pages' pages on this bdi. If `nr_pages' is zero, 2103595043e5SJens Axboe * write back the whole world. 2104595043e5SJens Axboe */ 2105595043e5SJens Axboe static void __wakeup_flusher_threads_bdi(struct backing_dev_info *bdi, 2106e8e8a0c6SJens Axboe enum wb_reason reason) 2107595043e5SJens Axboe { 2108595043e5SJens Axboe struct bdi_writeback *wb; 2109595043e5SJens Axboe 2110595043e5SJens Axboe if (!bdi_has_dirty_io(bdi)) 2111595043e5SJens Axboe return; 2112595043e5SJens Axboe 2113595043e5SJens Axboe list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node) 2114e8e8a0c6SJens Axboe wb_start_writeback(wb, reason); 2115595043e5SJens Axboe } 2116595043e5SJens Axboe 2117595043e5SJens Axboe void wakeup_flusher_threads_bdi(struct backing_dev_info *bdi, 2118595043e5SJens Axboe enum wb_reason reason) 2119595043e5SJens Axboe { 2120595043e5SJens Axboe rcu_read_lock(); 2121e8e8a0c6SJens Axboe __wakeup_flusher_threads_bdi(bdi, reason); 2122595043e5SJens Axboe rcu_read_unlock(); 2123595043e5SJens Axboe } 2124595043e5SJens Axboe 2125595043e5SJens Axboe /* 21269ba4b2dfSJens Axboe * Wakeup the flusher threads to start writeback of all currently dirty pages 212703ba3782SJens Axboe */ 21289ba4b2dfSJens Axboe void wakeup_flusher_threads(enum wb_reason reason) 212903ba3782SJens Axboe { 2130b8c2f347SChristoph Hellwig struct backing_dev_info *bdi; 2131b8c2f347SChristoph Hellwig 213251350ea0SKonstantin Khlebnikov /* 213351350ea0SKonstantin Khlebnikov * If we are expecting writeback progress we must submit plugged IO. 213451350ea0SKonstantin Khlebnikov */ 213551350ea0SKonstantin Khlebnikov if (blk_needs_flush_plug(current)) 213651350ea0SKonstantin Khlebnikov blk_schedule_flush_plug(current); 213751350ea0SKonstantin Khlebnikov 2138b8c2f347SChristoph Hellwig rcu_read_lock(); 2139595043e5SJens Axboe list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) 2140e8e8a0c6SJens Axboe __wakeup_flusher_threads_bdi(bdi, reason); 2141b8c2f347SChristoph Hellwig rcu_read_unlock(); 214203ba3782SJens Axboe } 214303ba3782SJens Axboe 2144a2f48706STheodore Ts'o /* 2145a2f48706STheodore Ts'o * Wake up bdi's periodically to make sure dirtytime inodes gets 2146a2f48706STheodore Ts'o * written back periodically. We deliberately do *not* check the 2147a2f48706STheodore Ts'o * b_dirtytime list in wb_has_dirty_io(), since this would cause the 2148a2f48706STheodore Ts'o * kernel to be constantly waking up once there are any dirtytime 2149a2f48706STheodore Ts'o * inodes on the system. So instead we define a separate delayed work 2150a2f48706STheodore Ts'o * function which gets called much more rarely. (By default, only 2151a2f48706STheodore Ts'o * once every 12 hours.) 2152a2f48706STheodore Ts'o * 2153a2f48706STheodore Ts'o * If there is any other write activity going on in the file system, 2154a2f48706STheodore Ts'o * this function won't be necessary. But if the only thing that has 2155a2f48706STheodore Ts'o * happened on the file system is a dirtytime inode caused by an atime 2156a2f48706STheodore Ts'o * update, we need this infrastructure below to make sure that inode 2157a2f48706STheodore Ts'o * eventually gets pushed out to disk. 2158a2f48706STheodore Ts'o */ 2159a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w); 2160a2f48706STheodore Ts'o static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback); 2161a2f48706STheodore Ts'o 2162a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w) 2163a2f48706STheodore Ts'o { 2164a2f48706STheodore Ts'o struct backing_dev_info *bdi; 2165a2f48706STheodore Ts'o 2166a2f48706STheodore Ts'o rcu_read_lock(); 2167a2f48706STheodore Ts'o list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 2168001fe6f6STejun Heo struct bdi_writeback *wb; 2169001fe6f6STejun Heo 2170b817525aSTejun Heo list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node) 21716fdf860fSTejun Heo if (!list_empty(&wb->b_dirty_time)) 21726fdf860fSTejun Heo wb_wakeup(wb); 2173a2f48706STheodore Ts'o } 2174a2f48706STheodore Ts'o rcu_read_unlock(); 2175a2f48706STheodore Ts'o schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ); 2176a2f48706STheodore Ts'o } 2177a2f48706STheodore Ts'o 2178a2f48706STheodore Ts'o static int __init start_dirtytime_writeback(void) 2179a2f48706STheodore Ts'o { 2180a2f48706STheodore Ts'o schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ); 2181a2f48706STheodore Ts'o return 0; 2182a2f48706STheodore Ts'o } 2183a2f48706STheodore Ts'o __initcall(start_dirtytime_writeback); 2184a2f48706STheodore Ts'o 21851efff914STheodore Ts'o int dirtytime_interval_handler(struct ctl_table *table, int write, 21861efff914STheodore Ts'o void __user *buffer, size_t *lenp, loff_t *ppos) 21871efff914STheodore Ts'o { 21881efff914STheodore Ts'o int ret; 21891efff914STheodore Ts'o 21901efff914STheodore Ts'o ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 21911efff914STheodore Ts'o if (ret == 0 && write) 21921efff914STheodore Ts'o mod_delayed_work(system_wq, &dirtytime_work, 0); 21931efff914STheodore Ts'o return ret; 21941efff914STheodore Ts'o } 21951efff914STheodore Ts'o 219603ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode) 219703ba3782SJens Axboe { 219803ba3782SJens Axboe if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { 219903ba3782SJens Axboe struct dentry *dentry; 220003ba3782SJens Axboe const char *name = "?"; 220103ba3782SJens Axboe 220203ba3782SJens Axboe dentry = d_find_alias(inode); 220303ba3782SJens Axboe if (dentry) { 220403ba3782SJens Axboe spin_lock(&dentry->d_lock); 220503ba3782SJens Axboe name = (const char *) dentry->d_name.name; 220603ba3782SJens Axboe } 220703ba3782SJens Axboe printk(KERN_DEBUG 220803ba3782SJens Axboe "%s(%d): dirtied inode %lu (%s) on %s\n", 220903ba3782SJens Axboe current->comm, task_pid_nr(current), inode->i_ino, 221003ba3782SJens Axboe name, inode->i_sb->s_id); 221103ba3782SJens Axboe if (dentry) { 221203ba3782SJens Axboe spin_unlock(&dentry->d_lock); 221303ba3782SJens Axboe dput(dentry); 221403ba3782SJens Axboe } 221503ba3782SJens Axboe } 221603ba3782SJens Axboe } 221703ba3782SJens Axboe 221803ba3782SJens Axboe /** 221903ba3782SJens Axboe * __mark_inode_dirty - internal function 22200117d427SMauro Carvalho Chehab * 222103ba3782SJens Axboe * @inode: inode to mark 222203ba3782SJens Axboe * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) 22230117d427SMauro Carvalho Chehab * 222403ba3782SJens Axboe * Mark an inode as dirty. Callers should use mark_inode_dirty or 222503ba3782SJens Axboe * mark_inode_dirty_sync. 222603ba3782SJens Axboe * 222703ba3782SJens Axboe * Put the inode on the super block's dirty list. 222803ba3782SJens Axboe * 222903ba3782SJens Axboe * CAREFUL! We mark it dirty unconditionally, but move it onto the 223003ba3782SJens Axboe * dirty list only if it is hashed or if it refers to a blockdev. 223103ba3782SJens Axboe * If it was not hashed, it will never be added to the dirty list 223203ba3782SJens Axboe * even if it is later hashed, as it will have been marked dirty already. 223303ba3782SJens Axboe * 223403ba3782SJens Axboe * In short, make sure you hash any inodes _before_ you start marking 223503ba3782SJens Axboe * them dirty. 223603ba3782SJens Axboe * 223703ba3782SJens Axboe * Note that for blockdevs, inode->dirtied_when represents the dirtying time of 223803ba3782SJens Axboe * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of 223903ba3782SJens Axboe * the kernel-internal blockdev inode represents the dirtying time of the 224003ba3782SJens Axboe * blockdev's pages. This is why for I_DIRTY_PAGES we always use 224103ba3782SJens Axboe * page->mapping->host, so the page-dirtying time is recorded in the internal 224203ba3782SJens Axboe * blockdev inode. 224303ba3782SJens Axboe */ 224403ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags) 224503ba3782SJens Axboe { 224603ba3782SJens Axboe struct super_block *sb = inode->i_sb; 22470ae45f63STheodore Ts'o int dirtytime; 22480ae45f63STheodore Ts'o 22490ae45f63STheodore Ts'o trace_writeback_mark_inode_dirty(inode, flags); 225003ba3782SJens Axboe 225103ba3782SJens Axboe /* 225203ba3782SJens Axboe * Don't do this for I_DIRTY_PAGES - that doesn't actually 225303ba3782SJens Axboe * dirty the inode itself 225403ba3782SJens Axboe */ 22550e11f644SChristoph Hellwig if (flags & (I_DIRTY_INODE | I_DIRTY_TIME)) { 22569fb0a7daSTejun Heo trace_writeback_dirty_inode_start(inode, flags); 22579fb0a7daSTejun Heo 225803ba3782SJens Axboe if (sb->s_op->dirty_inode) 2259aa385729SChristoph Hellwig sb->s_op->dirty_inode(inode, flags); 22609fb0a7daSTejun Heo 22619fb0a7daSTejun Heo trace_writeback_dirty_inode(inode, flags); 226203ba3782SJens Axboe } 22630ae45f63STheodore Ts'o if (flags & I_DIRTY_INODE) 22640ae45f63STheodore Ts'o flags &= ~I_DIRTY_TIME; 22650ae45f63STheodore Ts'o dirtytime = flags & I_DIRTY_TIME; 226603ba3782SJens Axboe 226703ba3782SJens Axboe /* 22689c6ac78eSTejun Heo * Paired with smp_mb() in __writeback_single_inode() for the 22699c6ac78eSTejun Heo * following lockless i_state test. See there for details. 227003ba3782SJens Axboe */ 227103ba3782SJens Axboe smp_mb(); 227203ba3782SJens Axboe 22730ae45f63STheodore Ts'o if (((inode->i_state & flags) == flags) || 22740ae45f63STheodore Ts'o (dirtytime && (inode->i_state & I_DIRTY_INODE))) 227503ba3782SJens Axboe return; 227603ba3782SJens Axboe 227703ba3782SJens Axboe if (unlikely(block_dump)) 227803ba3782SJens Axboe block_dump___mark_inode_dirty(inode); 227903ba3782SJens Axboe 2280250df6edSDave Chinner spin_lock(&inode->i_lock); 22810ae45f63STheodore Ts'o if (dirtytime && (inode->i_state & I_DIRTY_INODE)) 22820ae45f63STheodore Ts'o goto out_unlock_inode; 228303ba3782SJens Axboe if ((inode->i_state & flags) != flags) { 228403ba3782SJens Axboe const int was_dirty = inode->i_state & I_DIRTY; 228503ba3782SJens Axboe 228652ebea74STejun Heo inode_attach_wb(inode, NULL); 228752ebea74STejun Heo 22880ae45f63STheodore Ts'o if (flags & I_DIRTY_INODE) 22890ae45f63STheodore Ts'o inode->i_state &= ~I_DIRTY_TIME; 229003ba3782SJens Axboe inode->i_state |= flags; 229103ba3782SJens Axboe 229203ba3782SJens Axboe /* 229303ba3782SJens Axboe * If the inode is being synced, just update its dirty state. 229403ba3782SJens Axboe * The unlocker will place the inode on the appropriate 229503ba3782SJens Axboe * superblock list, based upon its state. 229603ba3782SJens Axboe */ 229703ba3782SJens Axboe if (inode->i_state & I_SYNC) 2298250df6edSDave Chinner goto out_unlock_inode; 229903ba3782SJens Axboe 230003ba3782SJens Axboe /* 230103ba3782SJens Axboe * Only add valid (hashed) inodes to the superblock's 230203ba3782SJens Axboe * dirty list. Add blockdev inodes as well. 230303ba3782SJens Axboe */ 230403ba3782SJens Axboe if (!S_ISBLK(inode->i_mode)) { 23051d3382cbSAl Viro if (inode_unhashed(inode)) 2306250df6edSDave Chinner goto out_unlock_inode; 230703ba3782SJens Axboe } 2308a4ffdde6SAl Viro if (inode->i_state & I_FREEING) 2309250df6edSDave Chinner goto out_unlock_inode; 231003ba3782SJens Axboe 231103ba3782SJens Axboe /* 231203ba3782SJens Axboe * If the inode was already on b_dirty/b_io/b_more_io, don't 231303ba3782SJens Axboe * reposition it (that would break b_dirty time-ordering). 231403ba3782SJens Axboe */ 231503ba3782SJens Axboe if (!was_dirty) { 231687e1d789STejun Heo struct bdi_writeback *wb; 2317d6c10f1fSTejun Heo struct list_head *dirty_list; 2318a66979abSDave Chinner bool wakeup_bdi = false; 2319500b067cSJens Axboe 232087e1d789STejun Heo wb = locked_inode_to_wb_and_lock_list(inode); 2321253c34e9SArtem Bityutskiy 23220747259dSTejun Heo WARN(bdi_cap_writeback_dirty(wb->bdi) && 23230747259dSTejun Heo !test_bit(WB_registered, &wb->state), 23240747259dSTejun Heo "bdi-%s not registered\n", wb->bdi->name); 232503ba3782SJens Axboe 232603ba3782SJens Axboe inode->dirtied_when = jiffies; 2327a2f48706STheodore Ts'o if (dirtytime) 2328a2f48706STheodore Ts'o inode->dirtied_time_when = jiffies; 2329d6c10f1fSTejun Heo 23300e11f644SChristoph Hellwig if (inode->i_state & I_DIRTY) 23310747259dSTejun Heo dirty_list = &wb->b_dirty; 2332a2f48706STheodore Ts'o else 23330747259dSTejun Heo dirty_list = &wb->b_dirty_time; 2334d6c10f1fSTejun Heo 2335c7f54084SDave Chinner wakeup_bdi = inode_io_list_move_locked(inode, wb, 2336d6c10f1fSTejun Heo dirty_list); 2337d6c10f1fSTejun Heo 23380747259dSTejun Heo spin_unlock(&wb->list_lock); 23390ae45f63STheodore Ts'o trace_writeback_dirty_inode_enqueue(inode); 2340253c34e9SArtem Bityutskiy 2341d6c10f1fSTejun Heo /* 2342d6c10f1fSTejun Heo * If this is the first dirty inode for this bdi, 2343d6c10f1fSTejun Heo * we have to wake-up the corresponding bdi thread 2344d6c10f1fSTejun Heo * to make sure background write-back happens 2345d6c10f1fSTejun Heo * later. 2346d6c10f1fSTejun Heo */ 23470747259dSTejun Heo if (bdi_cap_writeback_dirty(wb->bdi) && wakeup_bdi) 23480747259dSTejun Heo wb_wakeup_delayed(wb); 2349a66979abSDave Chinner return; 2350a66979abSDave Chinner } 2351a66979abSDave Chinner } 2352a66979abSDave Chinner out_unlock_inode: 2353a66979abSDave Chinner spin_unlock(&inode->i_lock); 235403ba3782SJens Axboe } 235503ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty); 235603ba3782SJens Axboe 2357e97fedb9SDave Chinner /* 2358e97fedb9SDave Chinner * The @s_sync_lock is used to serialise concurrent sync operations 2359e97fedb9SDave Chinner * to avoid lock contention problems with concurrent wait_sb_inodes() calls. 2360e97fedb9SDave Chinner * Concurrent callers will block on the s_sync_lock rather than doing contending 2361e97fedb9SDave Chinner * walks. The queueing maintains sync(2) required behaviour as all the IO that 2362e97fedb9SDave Chinner * has been issued up to the time this function is enter is guaranteed to be 2363e97fedb9SDave Chinner * completed by the time we have gained the lock and waited for all IO that is 2364e97fedb9SDave Chinner * in progress regardless of the order callers are granted the lock. 2365e97fedb9SDave Chinner */ 2366b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb) 236766f3b8e2SJens Axboe { 23686c60d2b5SDave Chinner LIST_HEAD(sync_list); 236938f21977SNick Piggin 237003ba3782SJens Axboe /* 237103ba3782SJens Axboe * We need to be protected against the filesystem going from 237203ba3782SJens Axboe * r/o to r/w or vice versa. 237303ba3782SJens Axboe */ 2374b6e51316SJens Axboe WARN_ON(!rwsem_is_locked(&sb->s_umount)); 237503ba3782SJens Axboe 2376e97fedb9SDave Chinner mutex_lock(&sb->s_sync_lock); 237766f3b8e2SJens Axboe 237838f21977SNick Piggin /* 23796c60d2b5SDave Chinner * Splice the writeback list onto a temporary list to avoid waiting on 23806c60d2b5SDave Chinner * inodes that have started writeback after this point. 23816c60d2b5SDave Chinner * 23826c60d2b5SDave Chinner * Use rcu_read_lock() to keep the inodes around until we have a 23836c60d2b5SDave Chinner * reference. s_inode_wblist_lock protects sb->s_inodes_wb as well as 23846c60d2b5SDave Chinner * the local list because inodes can be dropped from either by writeback 23856c60d2b5SDave Chinner * completion. 238638f21977SNick Piggin */ 23876c60d2b5SDave Chinner rcu_read_lock(); 23886c60d2b5SDave Chinner spin_lock_irq(&sb->s_inode_wblist_lock); 23896c60d2b5SDave Chinner list_splice_init(&sb->s_inodes_wb, &sync_list); 23906c60d2b5SDave Chinner 23916c60d2b5SDave Chinner /* 23926c60d2b5SDave Chinner * Data integrity sync. Must wait for all pages under writeback, because 23936c60d2b5SDave Chinner * there may have been pages dirtied before our sync call, but which had 23946c60d2b5SDave Chinner * writeout started before we write it out. In which case, the inode 23956c60d2b5SDave Chinner * may not be on the dirty list, but we still have to wait for that 23966c60d2b5SDave Chinner * writeout. 23976c60d2b5SDave Chinner */ 23986c60d2b5SDave Chinner while (!list_empty(&sync_list)) { 23996c60d2b5SDave Chinner struct inode *inode = list_first_entry(&sync_list, struct inode, 24006c60d2b5SDave Chinner i_wb_list); 2401250df6edSDave Chinner struct address_space *mapping = inode->i_mapping; 240238f21977SNick Piggin 24036c60d2b5SDave Chinner /* 24046c60d2b5SDave Chinner * Move each inode back to the wb list before we drop the lock 24056c60d2b5SDave Chinner * to preserve consistency between i_wb_list and the mapping 24066c60d2b5SDave Chinner * writeback tag. Writeback completion is responsible to remove 24076c60d2b5SDave Chinner * the inode from either list once the writeback tag is cleared. 24086c60d2b5SDave Chinner */ 24096c60d2b5SDave Chinner list_move_tail(&inode->i_wb_list, &sb->s_inodes_wb); 24106c60d2b5SDave Chinner 24116c60d2b5SDave Chinner /* 24126c60d2b5SDave Chinner * The mapping can appear untagged while still on-list since we 24136c60d2b5SDave Chinner * do not have the mapping lock. Skip it here, wb completion 24146c60d2b5SDave Chinner * will remove it. 24156c60d2b5SDave Chinner */ 24166c60d2b5SDave Chinner if (!mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK)) 24176c60d2b5SDave Chinner continue; 24186c60d2b5SDave Chinner 24196c60d2b5SDave Chinner spin_unlock_irq(&sb->s_inode_wblist_lock); 24206c60d2b5SDave Chinner 2421250df6edSDave Chinner spin_lock(&inode->i_lock); 24226c60d2b5SDave Chinner if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) { 2423250df6edSDave Chinner spin_unlock(&inode->i_lock); 24246c60d2b5SDave Chinner 24256c60d2b5SDave Chinner spin_lock_irq(&sb->s_inode_wblist_lock); 242638f21977SNick Piggin continue; 2427250df6edSDave Chinner } 242838f21977SNick Piggin __iget(inode); 2429250df6edSDave Chinner spin_unlock(&inode->i_lock); 24306c60d2b5SDave Chinner rcu_read_unlock(); 243138f21977SNick Piggin 2432aa750fd7SJunichi Nomura /* 2433aa750fd7SJunichi Nomura * We keep the error status of individual mapping so that 2434aa750fd7SJunichi Nomura * applications can catch the writeback error using fsync(2). 2435aa750fd7SJunichi Nomura * See filemap_fdatawait_keep_errors() for details. 2436aa750fd7SJunichi Nomura */ 2437aa750fd7SJunichi Nomura filemap_fdatawait_keep_errors(mapping); 243838f21977SNick Piggin 243938f21977SNick Piggin cond_resched(); 244038f21977SNick Piggin 24416c60d2b5SDave Chinner iput(inode); 24426c60d2b5SDave Chinner 24436c60d2b5SDave Chinner rcu_read_lock(); 24446c60d2b5SDave Chinner spin_lock_irq(&sb->s_inode_wblist_lock); 244538f21977SNick Piggin } 24466c60d2b5SDave Chinner spin_unlock_irq(&sb->s_inode_wblist_lock); 24476c60d2b5SDave Chinner rcu_read_unlock(); 2448e97fedb9SDave Chinner mutex_unlock(&sb->s_sync_lock); 244966f3b8e2SJens Axboe } 24501da177e4SLinus Torvalds 2451f30a7d0cSTejun Heo static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr, 2452f30a7d0cSTejun Heo enum wb_reason reason, bool skip_if_busy) 24531da177e4SLinus Torvalds { 24545b9cce4cSTejun Heo struct backing_dev_info *bdi = sb->s_bdi; 24555b9cce4cSTejun Heo DEFINE_WB_COMPLETION(done, bdi); 245683ba7b07SChristoph Hellwig struct wb_writeback_work work = { 24573c4d7165SChristoph Hellwig .sb = sb, 24583c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_NONE, 24596e6938b6SWu Fengguang .tagged_writepages = 1, 246083ba7b07SChristoph Hellwig .done = &done, 24613259f8beSChris Mason .nr_pages = nr, 24620e175a18SCurt Wohlgemuth .reason = reason, 24633c4d7165SChristoph Hellwig }; 24640e3c9a22SJens Axboe 2465e7972912STejun Heo if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info) 24666eedc701SJan Kara return; 2467cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 2468f30a7d0cSTejun Heo 2469db125360STejun Heo bdi_split_work_to_wbs(sb->s_bdi, &work, skip_if_busy); 24705b9cce4cSTejun Heo wb_wait_for_completion(&done); 24711da177e4SLinus Torvalds } 2472f30a7d0cSTejun Heo 2473f30a7d0cSTejun Heo /** 2474f30a7d0cSTejun Heo * writeback_inodes_sb_nr - writeback dirty inodes from given super_block 2475f30a7d0cSTejun Heo * @sb: the superblock 2476f30a7d0cSTejun Heo * @nr: the number of pages to write 2477f30a7d0cSTejun Heo * @reason: reason why some writeback work initiated 2478f30a7d0cSTejun Heo * 2479f30a7d0cSTejun Heo * Start writeback on some inodes on this super_block. No guarantees are made 2480f30a7d0cSTejun Heo * on how many (if any) will be written, and this function does not wait 2481f30a7d0cSTejun Heo * for IO completion of submitted IO. 2482f30a7d0cSTejun Heo */ 2483f30a7d0cSTejun Heo void writeback_inodes_sb_nr(struct super_block *sb, 2484f30a7d0cSTejun Heo unsigned long nr, 2485f30a7d0cSTejun Heo enum wb_reason reason) 2486f30a7d0cSTejun Heo { 2487f30a7d0cSTejun Heo __writeback_inodes_sb_nr(sb, nr, reason, false); 2488f30a7d0cSTejun Heo } 24893259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr); 24903259f8beSChris Mason 24913259f8beSChris Mason /** 24923259f8beSChris Mason * writeback_inodes_sb - writeback dirty inodes from given super_block 24933259f8beSChris Mason * @sb: the superblock 2494786228abSMarcos Paulo de Souza * @reason: reason why some writeback work was initiated 24953259f8beSChris Mason * 24963259f8beSChris Mason * Start writeback on some inodes on this super_block. No guarantees are made 24973259f8beSChris Mason * on how many (if any) will be written, and this function does not wait 24983259f8beSChris Mason * for IO completion of submitted IO. 24993259f8beSChris Mason */ 25000e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 25013259f8beSChris Mason { 25020e175a18SCurt Wohlgemuth return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); 25033259f8beSChris Mason } 2504d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb); 2505d8a8559cSJens Axboe 2506d8a8559cSJens Axboe /** 250710ee27a0SMiao Xie * try_to_writeback_inodes_sb - try to start writeback if none underway 250810ee27a0SMiao Xie * @sb: the superblock 250910ee27a0SMiao Xie * @reason: reason why some writeback work was initiated 251010ee27a0SMiao Xie * 25118264c321SRakesh Pandit * Invoke __writeback_inodes_sb_nr if no writeback is currently underway. 251210ee27a0SMiao Xie */ 25138264c321SRakesh Pandit void try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 251410ee27a0SMiao Xie { 25158264c321SRakesh Pandit if (!down_read_trylock(&sb->s_umount)) 25168264c321SRakesh Pandit return; 25178264c321SRakesh Pandit 25188264c321SRakesh Pandit __writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason, true); 25198264c321SRakesh Pandit up_read(&sb->s_umount); 252010ee27a0SMiao Xie } 252110ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb); 25223259f8beSChris Mason 25233259f8beSChris Mason /** 2524d8a8559cSJens Axboe * sync_inodes_sb - sync sb inode pages 2525d8a8559cSJens Axboe * @sb: the superblock 2526d8a8559cSJens Axboe * 2527d8a8559cSJens Axboe * This function writes and waits on any dirty inode belonging to this 25280dc83bd3SJan Kara * super_block. 2529d8a8559cSJens Axboe */ 25300dc83bd3SJan Kara void sync_inodes_sb(struct super_block *sb) 2531d8a8559cSJens Axboe { 25325b9cce4cSTejun Heo struct backing_dev_info *bdi = sb->s_bdi; 25335b9cce4cSTejun Heo DEFINE_WB_COMPLETION(done, bdi); 253483ba7b07SChristoph Hellwig struct wb_writeback_work work = { 25353c4d7165SChristoph Hellwig .sb = sb, 25363c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_ALL, 25373c4d7165SChristoph Hellwig .nr_pages = LONG_MAX, 25383c4d7165SChristoph Hellwig .range_cyclic = 0, 253983ba7b07SChristoph Hellwig .done = &done, 25400e175a18SCurt Wohlgemuth .reason = WB_REASON_SYNC, 25417747bd4bSDave Chinner .for_sync = 1, 25423c4d7165SChristoph Hellwig }; 25433c4d7165SChristoph Hellwig 2544006a0973STejun Heo /* 2545006a0973STejun Heo * Can't skip on !bdi_has_dirty() because we should wait for !dirty 2546006a0973STejun Heo * inodes under writeback and I_DIRTY_TIME inodes ignored by 2547006a0973STejun Heo * bdi_has_dirty() need to be written out too. 2548006a0973STejun Heo */ 2549006a0973STejun Heo if (bdi == &noop_backing_dev_info) 25506eedc701SJan Kara return; 2551cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 2552cf37e972SChristoph Hellwig 25537fc5854fSTejun Heo /* protect against inode wb switch, see inode_switch_wbs_work_fn() */ 25547fc5854fSTejun Heo bdi_down_write_wb_switch_rwsem(bdi); 2555db125360STejun Heo bdi_split_work_to_wbs(bdi, &work, false); 25565b9cce4cSTejun Heo wb_wait_for_completion(&done); 25577fc5854fSTejun Heo bdi_up_write_wb_switch_rwsem(bdi); 255883ba7b07SChristoph Hellwig 2559b6e51316SJens Axboe wait_sb_inodes(sb); 2560d8a8559cSJens Axboe } 2561d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb); 25621da177e4SLinus Torvalds 25631da177e4SLinus Torvalds /** 25641da177e4SLinus Torvalds * write_inode_now - write an inode to disk 25651da177e4SLinus Torvalds * @inode: inode to write to disk 25661da177e4SLinus Torvalds * @sync: whether the write should be synchronous or not 25671da177e4SLinus Torvalds * 25687f04c26dSAndrea Arcangeli * This function commits an inode to disk immediately if it is dirty. This is 25697f04c26dSAndrea Arcangeli * primarily needed by knfsd. 25707f04c26dSAndrea Arcangeli * 25717f04c26dSAndrea Arcangeli * The caller must either have a ref on the inode or must have set I_WILL_FREE. 25721da177e4SLinus Torvalds */ 25731da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync) 25741da177e4SLinus Torvalds { 25751da177e4SLinus Torvalds struct writeback_control wbc = { 25761da177e4SLinus Torvalds .nr_to_write = LONG_MAX, 257718914b18SMike Galbraith .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 2578111ebb6eSOGAWA Hirofumi .range_start = 0, 2579111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 25801da177e4SLinus Torvalds }; 25811da177e4SLinus Torvalds 25821da177e4SLinus Torvalds if (!mapping_cap_writeback_dirty(inode->i_mapping)) 258349364ce2SAndrew Morton wbc.nr_to_write = 0; 25841da177e4SLinus Torvalds 25851da177e4SLinus Torvalds might_sleep(); 2586aaf25593STejun Heo return writeback_single_inode(inode, &wbc); 25871da177e4SLinus Torvalds } 25881da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now); 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds /** 25911da177e4SLinus Torvalds * sync_inode - write an inode and its pages to disk. 25921da177e4SLinus Torvalds * @inode: the inode to sync 25931da177e4SLinus Torvalds * @wbc: controls the writeback mode 25941da177e4SLinus Torvalds * 25951da177e4SLinus Torvalds * sync_inode() will write an inode and its pages to disk. It will also 25961da177e4SLinus Torvalds * correctly update the inode on its superblock's dirty inode lists and will 25971da177e4SLinus Torvalds * update inode->i_state. 25981da177e4SLinus Torvalds * 25991da177e4SLinus Torvalds * The caller must have a ref on the inode. 26001da177e4SLinus Torvalds */ 26011da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc) 26021da177e4SLinus Torvalds { 2603aaf25593STejun Heo return writeback_single_inode(inode, wbc); 26041da177e4SLinus Torvalds } 26051da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode); 2606c3765016SChristoph Hellwig 2607c3765016SChristoph Hellwig /** 2608c691b9d9SAndrew Morton * sync_inode_metadata - write an inode to disk 2609c3765016SChristoph Hellwig * @inode: the inode to sync 2610c3765016SChristoph Hellwig * @wait: wait for I/O to complete. 2611c3765016SChristoph Hellwig * 2612c691b9d9SAndrew Morton * Write an inode to disk and adjust its dirty state after completion. 2613c3765016SChristoph Hellwig * 2614c3765016SChristoph Hellwig * Note: only writes the actual inode, no associated data or other metadata. 2615c3765016SChristoph Hellwig */ 2616c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait) 2617c3765016SChristoph Hellwig { 2618c3765016SChristoph Hellwig struct writeback_control wbc = { 2619c3765016SChristoph Hellwig .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, 2620c3765016SChristoph Hellwig .nr_to_write = 0, /* metadata-only */ 2621c3765016SChristoph Hellwig }; 2622c3765016SChristoph Hellwig 2623c3765016SChristoph Hellwig return sync_inode(inode, &wbc); 2624c3765016SChristoph Hellwig } 2625c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata); 2626