11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * fs/fs-writeback.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2002, Linus Torvalds. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Contains all the functions related to writing back and waiting 71da177e4SLinus Torvalds * upon dirty inodes against superblocks, and writing back dirty 81da177e4SLinus Torvalds * pages against inodes. ie: data writeback. Writeout of the 91da177e4SLinus Torvalds * inode itself is not handled here. 101da177e4SLinus Torvalds * 11e1f8e874SFrancois Cami * 10Apr2002 Andrew Morton 121da177e4SLinus Torvalds * Split out of fs/inode.c 131da177e4SLinus Torvalds * Additions for address_space-based writeback 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/kernel.h> 17630d9c47SPaul Gortmaker #include <linux/export.h> 181da177e4SLinus Torvalds #include <linux/spinlock.h> 195a0e3ad6STejun Heo #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/sched.h> 211da177e4SLinus Torvalds #include <linux/fs.h> 221da177e4SLinus Torvalds #include <linux/mm.h> 23bc31b86aSWu Fengguang #include <linux/pagemap.h> 2403ba3782SJens Axboe #include <linux/kthread.h> 251da177e4SLinus Torvalds #include <linux/writeback.h> 261da177e4SLinus Torvalds #include <linux/blkdev.h> 271da177e4SLinus Torvalds #include <linux/backing-dev.h> 28455b2864SDave Chinner #include <linux/tracepoint.h> 29719ea2fbSAl Viro #include <linux/device.h> 3021c6321fSTejun Heo #include <linux/memcontrol.h> 3107f3f05cSDavid Howells #include "internal.h" 321da177e4SLinus Torvalds 33d0bceac7SJens Axboe /* 34bc31b86aSWu Fengguang * 4MB minimal write chunk size 35bc31b86aSWu Fengguang */ 36bc31b86aSWu Fengguang #define MIN_WRITEBACK_PAGES (4096UL >> (PAGE_CACHE_SHIFT - 10)) 37bc31b86aSWu Fengguang 38cc395d7fSTejun Heo struct wb_completion { 39cc395d7fSTejun Heo atomic_t cnt; 40cc395d7fSTejun Heo }; 41cc395d7fSTejun Heo 42bc31b86aSWu Fengguang /* 43c4a77a6cSJens Axboe * Passed into wb_writeback(), essentially a subset of writeback_control 44c4a77a6cSJens Axboe */ 4583ba7b07SChristoph Hellwig struct wb_writeback_work { 46c4a77a6cSJens Axboe long nr_pages; 47c4a77a6cSJens Axboe struct super_block *sb; 480dc83bd3SJan Kara unsigned long *older_than_this; 49c4a77a6cSJens Axboe enum writeback_sync_modes sync_mode; 506e6938b6SWu Fengguang unsigned int tagged_writepages:1; 5152957fe1SH Hartley Sweeten unsigned int for_kupdate:1; 5252957fe1SH Hartley Sweeten unsigned int range_cyclic:1; 5352957fe1SH Hartley Sweeten unsigned int for_background:1; 547747bd4bSDave Chinner unsigned int for_sync:1; /* sync(2) WB_SYNC_ALL writeback */ 55ac7b19a3STejun Heo unsigned int auto_free:1; /* free on completion */ 5698754bf7STejun Heo unsigned int single_wait:1; 5798754bf7STejun Heo unsigned int single_done:1; 580e175a18SCurt Wohlgemuth enum wb_reason reason; /* why was writeback initiated? */ 59c4a77a6cSJens Axboe 608010c3b6SJens Axboe struct list_head list; /* pending work list */ 61cc395d7fSTejun Heo struct wb_completion *done; /* set if the caller waits */ 6203ba3782SJens Axboe }; 6303ba3782SJens Axboe 64a2f48706STheodore Ts'o /* 65cc395d7fSTejun Heo * If one wants to wait for one or more wb_writeback_works, each work's 66cc395d7fSTejun Heo * ->done should be set to a wb_completion defined using the following 67cc395d7fSTejun Heo * macro. Once all work items are issued with wb_queue_work(), the caller 68cc395d7fSTejun Heo * can wait for the completion of all using wb_wait_for_completion(). Work 69cc395d7fSTejun Heo * items which are waited upon aren't freed automatically on completion. 70cc395d7fSTejun Heo */ 71cc395d7fSTejun Heo #define DEFINE_WB_COMPLETION_ONSTACK(cmpl) \ 72cc395d7fSTejun Heo struct wb_completion cmpl = { \ 73cc395d7fSTejun Heo .cnt = ATOMIC_INIT(1), \ 74cc395d7fSTejun Heo } 75cc395d7fSTejun Heo 76cc395d7fSTejun Heo 77cc395d7fSTejun Heo /* 78a2f48706STheodore Ts'o * If an inode is constantly having its pages dirtied, but then the 79a2f48706STheodore Ts'o * updates stop dirtytime_expire_interval seconds in the past, it's 80a2f48706STheodore Ts'o * possible for the worst case time between when an inode has its 81a2f48706STheodore Ts'o * timestamps updated and when they finally get written out to be two 82a2f48706STheodore Ts'o * dirtytime_expire_intervals. We set the default to 12 hours (in 83a2f48706STheodore Ts'o * seconds), which means most of the time inodes will have their 84a2f48706STheodore Ts'o * timestamps written to disk after 12 hours, but in the worst case a 85a2f48706STheodore Ts'o * few inodes might not their timestamps updated for 24 hours. 86a2f48706STheodore Ts'o */ 87a2f48706STheodore Ts'o unsigned int dirtytime_expire_interval = 12 * 60 * 60; 88a2f48706STheodore Ts'o 897ccf19a8SNick Piggin static inline struct inode *wb_inode(struct list_head *head) 907ccf19a8SNick Piggin { 917ccf19a8SNick Piggin return list_entry(head, struct inode, i_wb_list); 927ccf19a8SNick Piggin } 937ccf19a8SNick Piggin 9415eb77a0SWu Fengguang /* 9515eb77a0SWu Fengguang * Include the creation of the trace points after defining the 9615eb77a0SWu Fengguang * wb_writeback_work structure and inline functions so that the definition 9715eb77a0SWu Fengguang * remains local to this file. 9815eb77a0SWu Fengguang */ 9915eb77a0SWu Fengguang #define CREATE_TRACE_POINTS 10015eb77a0SWu Fengguang #include <trace/events/writeback.h> 10115eb77a0SWu Fengguang 102774016b2SSteven Whitehouse EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage); 103774016b2SSteven Whitehouse 104d6c10f1fSTejun Heo static bool wb_io_lists_populated(struct bdi_writeback *wb) 105d6c10f1fSTejun Heo { 106d6c10f1fSTejun Heo if (wb_has_dirty_io(wb)) { 107d6c10f1fSTejun Heo return false; 108d6c10f1fSTejun Heo } else { 109d6c10f1fSTejun Heo set_bit(WB_has_dirty_io, &wb->state); 11095a46c65STejun Heo WARN_ON_ONCE(!wb->avg_write_bandwidth); 111766a9d6eSTejun Heo atomic_long_add(wb->avg_write_bandwidth, 112766a9d6eSTejun Heo &wb->bdi->tot_write_bandwidth); 113d6c10f1fSTejun Heo return true; 114d6c10f1fSTejun Heo } 115d6c10f1fSTejun Heo } 116d6c10f1fSTejun Heo 117d6c10f1fSTejun Heo static void wb_io_lists_depopulated(struct bdi_writeback *wb) 118d6c10f1fSTejun Heo { 119d6c10f1fSTejun Heo if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) && 120766a9d6eSTejun Heo list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) { 121d6c10f1fSTejun Heo clear_bit(WB_has_dirty_io, &wb->state); 12295a46c65STejun Heo WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth, 12395a46c65STejun Heo &wb->bdi->tot_write_bandwidth) < 0); 124766a9d6eSTejun Heo } 125d6c10f1fSTejun Heo } 126d6c10f1fSTejun Heo 127d6c10f1fSTejun Heo /** 128d6c10f1fSTejun Heo * inode_wb_list_move_locked - move an inode onto a bdi_writeback IO list 129d6c10f1fSTejun Heo * @inode: inode to be moved 130d6c10f1fSTejun Heo * @wb: target bdi_writeback 131d6c10f1fSTejun Heo * @head: one of @wb->b_{dirty|io|more_io} 132d6c10f1fSTejun Heo * 133d6c10f1fSTejun Heo * Move @inode->i_wb_list to @list of @wb and set %WB_has_dirty_io. 134d6c10f1fSTejun Heo * Returns %true if @inode is the first occupant of the !dirty_time IO 135d6c10f1fSTejun Heo * lists; otherwise, %false. 136d6c10f1fSTejun Heo */ 137d6c10f1fSTejun Heo static bool inode_wb_list_move_locked(struct inode *inode, 138d6c10f1fSTejun Heo struct bdi_writeback *wb, 139d6c10f1fSTejun Heo struct list_head *head) 140d6c10f1fSTejun Heo { 141d6c10f1fSTejun Heo assert_spin_locked(&wb->list_lock); 142d6c10f1fSTejun Heo 143d6c10f1fSTejun Heo list_move(&inode->i_wb_list, head); 144d6c10f1fSTejun Heo 145d6c10f1fSTejun Heo /* dirty_time doesn't count as dirty_io until expiration */ 146d6c10f1fSTejun Heo if (head != &wb->b_dirty_time) 147d6c10f1fSTejun Heo return wb_io_lists_populated(wb); 148d6c10f1fSTejun Heo 149d6c10f1fSTejun Heo wb_io_lists_depopulated(wb); 150d6c10f1fSTejun Heo return false; 151d6c10f1fSTejun Heo } 152d6c10f1fSTejun Heo 153d6c10f1fSTejun Heo /** 154d6c10f1fSTejun Heo * inode_wb_list_del_locked - remove an inode from its bdi_writeback IO list 155d6c10f1fSTejun Heo * @inode: inode to be removed 156d6c10f1fSTejun Heo * @wb: bdi_writeback @inode is being removed from 157d6c10f1fSTejun Heo * 158d6c10f1fSTejun Heo * Remove @inode which may be on one of @wb->b_{dirty|io|more_io} lists and 159d6c10f1fSTejun Heo * clear %WB_has_dirty_io if all are empty afterwards. 160d6c10f1fSTejun Heo */ 161d6c10f1fSTejun Heo static void inode_wb_list_del_locked(struct inode *inode, 162d6c10f1fSTejun Heo struct bdi_writeback *wb) 163d6c10f1fSTejun Heo { 164d6c10f1fSTejun Heo assert_spin_locked(&wb->list_lock); 165d6c10f1fSTejun Heo 166d6c10f1fSTejun Heo list_del_init(&inode->i_wb_list); 167d6c10f1fSTejun Heo wb_io_lists_depopulated(wb); 168d6c10f1fSTejun Heo } 169d6c10f1fSTejun Heo 170f0054bb1STejun Heo static void wb_wakeup(struct bdi_writeback *wb) 1715acda9d1SJan Kara { 172f0054bb1STejun Heo spin_lock_bh(&wb->work_lock); 173f0054bb1STejun Heo if (test_bit(WB_registered, &wb->state)) 174f0054bb1STejun Heo mod_delayed_work(bdi_wq, &wb->dwork, 0); 175f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 1765acda9d1SJan Kara } 1775acda9d1SJan Kara 178f0054bb1STejun Heo static void wb_queue_work(struct bdi_writeback *wb, 1796585027aSJan Kara struct wb_writeback_work *work) 1806585027aSJan Kara { 181f0054bb1STejun Heo trace_writeback_queue(wb->bdi, work); 1826585027aSJan Kara 183f0054bb1STejun Heo spin_lock_bh(&wb->work_lock); 18498754bf7STejun Heo if (!test_bit(WB_registered, &wb->state)) { 18598754bf7STejun Heo if (work->single_wait) 18698754bf7STejun Heo work->single_done = 1; 1875acda9d1SJan Kara goto out_unlock; 18898754bf7STejun Heo } 189cc395d7fSTejun Heo if (work->done) 190cc395d7fSTejun Heo atomic_inc(&work->done->cnt); 191f0054bb1STejun Heo list_add_tail(&work->list, &wb->work_list); 192f0054bb1STejun Heo mod_delayed_work(bdi_wq, &wb->dwork, 0); 1935acda9d1SJan Kara out_unlock: 194f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 19503ba3782SJens Axboe } 1961da177e4SLinus Torvalds 197cc395d7fSTejun Heo /** 198cc395d7fSTejun Heo * wb_wait_for_completion - wait for completion of bdi_writeback_works 199cc395d7fSTejun Heo * @bdi: bdi work items were issued to 200cc395d7fSTejun Heo * @done: target wb_completion 201cc395d7fSTejun Heo * 202cc395d7fSTejun Heo * Wait for one or more work items issued to @bdi with their ->done field 203cc395d7fSTejun Heo * set to @done, which should have been defined with 204cc395d7fSTejun Heo * DEFINE_WB_COMPLETION_ONSTACK(). This function returns after all such 205cc395d7fSTejun Heo * work items are completed. Work items which are waited upon aren't freed 206cc395d7fSTejun Heo * automatically on completion. 207cc395d7fSTejun Heo */ 208cc395d7fSTejun Heo static void wb_wait_for_completion(struct backing_dev_info *bdi, 209cc395d7fSTejun Heo struct wb_completion *done) 210cc395d7fSTejun Heo { 211cc395d7fSTejun Heo atomic_dec(&done->cnt); /* put down the initial count */ 212cc395d7fSTejun Heo wait_event(bdi->wb_waitq, !atomic_read(&done->cnt)); 213cc395d7fSTejun Heo } 214cc395d7fSTejun Heo 215703c2708STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 216703c2708STejun Heo 21721c6321fSTejun Heo void __inode_attach_wb(struct inode *inode, struct page *page) 21821c6321fSTejun Heo { 21921c6321fSTejun Heo struct backing_dev_info *bdi = inode_to_bdi(inode); 22021c6321fSTejun Heo struct bdi_writeback *wb = NULL; 22121c6321fSTejun Heo 22221c6321fSTejun Heo if (inode_cgwb_enabled(inode)) { 22321c6321fSTejun Heo struct cgroup_subsys_state *memcg_css; 22421c6321fSTejun Heo 22521c6321fSTejun Heo if (page) { 22621c6321fSTejun Heo memcg_css = mem_cgroup_css_from_page(page); 22721c6321fSTejun Heo wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 22821c6321fSTejun Heo } else { 22921c6321fSTejun Heo /* must pin memcg_css, see wb_get_create() */ 23021c6321fSTejun Heo memcg_css = task_get_css(current, memory_cgrp_id); 23121c6321fSTejun Heo wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 23221c6321fSTejun Heo css_put(memcg_css); 23321c6321fSTejun Heo } 23421c6321fSTejun Heo } 23521c6321fSTejun Heo 23621c6321fSTejun Heo if (!wb) 23721c6321fSTejun Heo wb = &bdi->wb; 23821c6321fSTejun Heo 23921c6321fSTejun Heo /* 24021c6321fSTejun Heo * There may be multiple instances of this function racing to 24121c6321fSTejun Heo * update the same inode. Use cmpxchg() to tell the winner. 24221c6321fSTejun Heo */ 24321c6321fSTejun Heo if (unlikely(cmpxchg(&inode->i_wb, NULL, wb))) 24421c6321fSTejun Heo wb_put(wb); 24521c6321fSTejun Heo } 24621c6321fSTejun Heo 247703c2708STejun Heo /** 248703c2708STejun Heo * inode_congested - test whether an inode is congested 249703c2708STejun Heo * @inode: inode to test for congestion 250703c2708STejun Heo * @cong_bits: mask of WB_[a]sync_congested bits to test 251703c2708STejun Heo * 252703c2708STejun Heo * Tests whether @inode is congested. @cong_bits is the mask of congestion 253703c2708STejun Heo * bits to test and the return value is the mask of set bits. 254703c2708STejun Heo * 255703c2708STejun Heo * If cgroup writeback is enabled for @inode, the congestion state is 256703c2708STejun Heo * determined by whether the cgwb (cgroup bdi_writeback) for the blkcg 257703c2708STejun Heo * associated with @inode is congested; otherwise, the root wb's congestion 258703c2708STejun Heo * state is used. 259703c2708STejun Heo */ 260703c2708STejun Heo int inode_congested(struct inode *inode, int cong_bits) 261703c2708STejun Heo { 262703c2708STejun Heo if (inode) { 263703c2708STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 264703c2708STejun Heo if (wb) 265703c2708STejun Heo return wb_congested(wb, cong_bits); 266703c2708STejun Heo } 267703c2708STejun Heo 268703c2708STejun Heo return wb_congested(&inode_to_bdi(inode)->wb, cong_bits); 269703c2708STejun Heo } 270703c2708STejun Heo EXPORT_SYMBOL_GPL(inode_congested); 271703c2708STejun Heo 272f2b65121STejun Heo /** 27398754bf7STejun Heo * wb_wait_for_single_work - wait for completion of a single bdi_writeback_work 27498754bf7STejun Heo * @bdi: bdi the work item was issued to 27598754bf7STejun Heo * @work: work item to wait for 27698754bf7STejun Heo * 27798754bf7STejun Heo * Wait for the completion of @work which was issued to one of @bdi's 27898754bf7STejun Heo * bdi_writeback's. The caller must have set @work->single_wait before 27998754bf7STejun Heo * issuing it. This wait operates independently fo 28098754bf7STejun Heo * wb_wait_for_completion() and also disables automatic freeing of @work. 28198754bf7STejun Heo */ 28298754bf7STejun Heo static void wb_wait_for_single_work(struct backing_dev_info *bdi, 28398754bf7STejun Heo struct wb_writeback_work *work) 28498754bf7STejun Heo { 28598754bf7STejun Heo if (WARN_ON_ONCE(!work->single_wait)) 28698754bf7STejun Heo return; 28798754bf7STejun Heo 28898754bf7STejun Heo wait_event(bdi->wb_waitq, work->single_done); 28998754bf7STejun Heo 29098754bf7STejun Heo /* 29198754bf7STejun Heo * Paired with smp_wmb() in wb_do_writeback() and ensures that all 29298754bf7STejun Heo * modifications to @work prior to assertion of ->single_done is 29398754bf7STejun Heo * visible to the caller once this function returns. 29498754bf7STejun Heo */ 29598754bf7STejun Heo smp_rmb(); 29698754bf7STejun Heo } 29798754bf7STejun Heo 29898754bf7STejun Heo /** 299f2b65121STejun Heo * wb_split_bdi_pages - split nr_pages to write according to bandwidth 300f2b65121STejun Heo * @wb: target bdi_writeback to split @nr_pages to 301f2b65121STejun Heo * @nr_pages: number of pages to write for the whole bdi 302f2b65121STejun Heo * 303f2b65121STejun Heo * Split @wb's portion of @nr_pages according to @wb's write bandwidth in 304f2b65121STejun Heo * relation to the total write bandwidth of all wb's w/ dirty inodes on 305f2b65121STejun Heo * @wb->bdi. 306f2b65121STejun Heo */ 307f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) 308f2b65121STejun Heo { 309f2b65121STejun Heo unsigned long this_bw = wb->avg_write_bandwidth; 310f2b65121STejun Heo unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth); 311f2b65121STejun Heo 312f2b65121STejun Heo if (nr_pages == LONG_MAX) 313f2b65121STejun Heo return LONG_MAX; 314f2b65121STejun Heo 315f2b65121STejun Heo /* 316f2b65121STejun Heo * This may be called on clean wb's and proportional distribution 317f2b65121STejun Heo * may not make sense, just use the original @nr_pages in those 318f2b65121STejun Heo * cases. In general, we wanna err on the side of writing more. 319f2b65121STejun Heo */ 320f2b65121STejun Heo if (!tot_bw || this_bw >= tot_bw) 321f2b65121STejun Heo return nr_pages; 322f2b65121STejun Heo else 323f2b65121STejun Heo return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw); 324f2b65121STejun Heo } 325f2b65121STejun Heo 326db125360STejun Heo /** 327db125360STejun Heo * wb_clone_and_queue_work - clone a wb_writeback_work and issue it to a wb 328db125360STejun Heo * @wb: target bdi_writeback 329db125360STejun Heo * @base_work: source wb_writeback_work 330db125360STejun Heo * 331db125360STejun Heo * Try to make a clone of @base_work and issue it to @wb. If cloning 332db125360STejun Heo * succeeds, %true is returned; otherwise, @base_work is issued directly 333db125360STejun Heo * and %false is returned. In the latter case, the caller is required to 334db125360STejun Heo * wait for @base_work's completion using wb_wait_for_single_work(). 335db125360STejun Heo * 336db125360STejun Heo * A clone is auto-freed on completion. @base_work never is. 337db125360STejun Heo */ 338db125360STejun Heo static bool wb_clone_and_queue_work(struct bdi_writeback *wb, 339db125360STejun Heo struct wb_writeback_work *base_work) 340db125360STejun Heo { 341db125360STejun Heo struct wb_writeback_work *work; 342db125360STejun Heo 343db125360STejun Heo work = kmalloc(sizeof(*work), GFP_ATOMIC); 344db125360STejun Heo if (work) { 345db125360STejun Heo *work = *base_work; 346db125360STejun Heo work->auto_free = 1; 347db125360STejun Heo work->single_wait = 0; 348db125360STejun Heo } else { 349db125360STejun Heo work = base_work; 350db125360STejun Heo work->auto_free = 0; 351db125360STejun Heo work->single_wait = 1; 352db125360STejun Heo } 353db125360STejun Heo work->single_done = 0; 354db125360STejun Heo wb_queue_work(wb, work); 355db125360STejun Heo return work != base_work; 356db125360STejun Heo } 357db125360STejun Heo 358db125360STejun Heo /** 359db125360STejun Heo * bdi_split_work_to_wbs - split a wb_writeback_work to all wb's of a bdi 360db125360STejun Heo * @bdi: target backing_dev_info 361db125360STejun Heo * @base_work: wb_writeback_work to issue 362db125360STejun Heo * @skip_if_busy: skip wb's which already have writeback in progress 363db125360STejun Heo * 364db125360STejun Heo * Split and issue @base_work to all wb's (bdi_writeback's) of @bdi which 365db125360STejun Heo * have dirty inodes. If @base_work->nr_page isn't %LONG_MAX, it's 366db125360STejun Heo * distributed to the busy wbs according to each wb's proportion in the 367db125360STejun Heo * total active write bandwidth of @bdi. 368db125360STejun Heo */ 369db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, 370db125360STejun Heo struct wb_writeback_work *base_work, 371db125360STejun Heo bool skip_if_busy) 372db125360STejun Heo { 373db125360STejun Heo long nr_pages = base_work->nr_pages; 374db125360STejun Heo int next_blkcg_id = 0; 375db125360STejun Heo struct bdi_writeback *wb; 376db125360STejun Heo struct wb_iter iter; 377db125360STejun Heo 378db125360STejun Heo might_sleep(); 379db125360STejun Heo 380db125360STejun Heo if (!bdi_has_dirty_io(bdi)) 381db125360STejun Heo return; 382db125360STejun Heo restart: 383db125360STejun Heo rcu_read_lock(); 384db125360STejun Heo bdi_for_each_wb(wb, bdi, &iter, next_blkcg_id) { 385db125360STejun Heo if (!wb_has_dirty_io(wb) || 386db125360STejun Heo (skip_if_busy && writeback_in_progress(wb))) 387db125360STejun Heo continue; 388db125360STejun Heo 389db125360STejun Heo base_work->nr_pages = wb_split_bdi_pages(wb, nr_pages); 390db125360STejun Heo if (!wb_clone_and_queue_work(wb, base_work)) { 391db125360STejun Heo next_blkcg_id = wb->blkcg_css->id + 1; 392db125360STejun Heo rcu_read_unlock(); 393db125360STejun Heo wb_wait_for_single_work(bdi, base_work); 394db125360STejun Heo goto restart; 395db125360STejun Heo } 396db125360STejun Heo } 397db125360STejun Heo rcu_read_unlock(); 398db125360STejun Heo } 399db125360STejun Heo 400f2b65121STejun Heo #else /* CONFIG_CGROUP_WRITEBACK */ 401f2b65121STejun Heo 402f2b65121STejun Heo static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) 403f2b65121STejun Heo { 404f2b65121STejun Heo return nr_pages; 405f2b65121STejun Heo } 406f2b65121STejun Heo 407db125360STejun Heo static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, 408db125360STejun Heo struct wb_writeback_work *base_work, 409db125360STejun Heo bool skip_if_busy) 410db125360STejun Heo { 411db125360STejun Heo might_sleep(); 412db125360STejun Heo 413db125360STejun Heo if (bdi_has_dirty_io(bdi) && 414db125360STejun Heo (!skip_if_busy || !writeback_in_progress(&bdi->wb))) { 415db125360STejun Heo base_work->auto_free = 0; 416db125360STejun Heo base_work->single_wait = 0; 417db125360STejun Heo base_work->single_done = 0; 418db125360STejun Heo wb_queue_work(&bdi->wb, base_work); 419db125360STejun Heo } 420db125360STejun Heo } 421db125360STejun Heo 422703c2708STejun Heo #endif /* CONFIG_CGROUP_WRITEBACK */ 423703c2708STejun Heo 424c00ddad3STejun Heo void wb_start_writeback(struct bdi_writeback *wb, long nr_pages, 425c00ddad3STejun Heo bool range_cyclic, enum wb_reason reason) 426b6e51316SJens Axboe { 427c00ddad3STejun Heo struct wb_writeback_work *work; 428c00ddad3STejun Heo 429c00ddad3STejun Heo if (!wb_has_dirty_io(wb)) 430c00ddad3STejun Heo return; 431c00ddad3STejun Heo 432c00ddad3STejun Heo /* 433c00ddad3STejun Heo * This is WB_SYNC_NONE writeback, so if allocation fails just 434c00ddad3STejun Heo * wakeup the thread for old dirty data writeback 435c00ddad3STejun Heo */ 436c00ddad3STejun Heo work = kzalloc(sizeof(*work), GFP_ATOMIC); 437c00ddad3STejun Heo if (!work) { 438c00ddad3STejun Heo trace_writeback_nowork(wb->bdi); 439c00ddad3STejun Heo wb_wakeup(wb); 440c00ddad3STejun Heo return; 441c00ddad3STejun Heo } 442c00ddad3STejun Heo 443c00ddad3STejun Heo work->sync_mode = WB_SYNC_NONE; 444c00ddad3STejun Heo work->nr_pages = nr_pages; 445c00ddad3STejun Heo work->range_cyclic = range_cyclic; 446c00ddad3STejun Heo work->reason = reason; 447ac7b19a3STejun Heo work->auto_free = 1; 448c00ddad3STejun Heo 449c00ddad3STejun Heo wb_queue_work(wb, work); 450d3ddec76SWu Fengguang } 451d3ddec76SWu Fengguang 452c5444198SChristoph Hellwig /** 4539ecf4866STejun Heo * wb_start_background_writeback - start background writeback 4549ecf4866STejun Heo * @wb: bdi_writback to write from 455c5444198SChristoph Hellwig * 456c5444198SChristoph Hellwig * Description: 4576585027aSJan Kara * This makes sure WB_SYNC_NONE background writeback happens. When 4589ecf4866STejun Heo * this function returns, it is only guaranteed that for given wb 4596585027aSJan Kara * some IO is happening if we are over background dirty threshold. 4606585027aSJan Kara * Caller need not hold sb s_umount semaphore. 461c5444198SChristoph Hellwig */ 4629ecf4866STejun Heo void wb_start_background_writeback(struct bdi_writeback *wb) 463c5444198SChristoph Hellwig { 4646585027aSJan Kara /* 4656585027aSJan Kara * We just wake up the flusher thread. It will perform background 4666585027aSJan Kara * writeback as soon as there is no other work to do. 4676585027aSJan Kara */ 4689ecf4866STejun Heo trace_writeback_wake_background(wb->bdi); 4699ecf4866STejun Heo wb_wakeup(wb); 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds /* 473a66979abSDave Chinner * Remove the inode from the writeback list it is on. 474a66979abSDave Chinner */ 475a66979abSDave Chinner void inode_wb_list_del(struct inode *inode) 476a66979abSDave Chinner { 47752ebea74STejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 478a66979abSDave Chinner 47952ebea74STejun Heo spin_lock(&wb->list_lock); 480d6c10f1fSTejun Heo inode_wb_list_del_locked(inode, wb); 48152ebea74STejun Heo spin_unlock(&wb->list_lock); 482f758eeabSChristoph Hellwig } 483a66979abSDave Chinner 484a66979abSDave Chinner /* 4856610a0bcSAndrew Morton * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 4866610a0bcSAndrew Morton * furthest end of its superblock's dirty-inode list. 4876610a0bcSAndrew Morton * 4886610a0bcSAndrew Morton * Before stamping the inode's ->dirtied_when, we check to see whether it is 48966f3b8e2SJens Axboe * already the most-recently-dirtied inode on the b_dirty list. If that is 4906610a0bcSAndrew Morton * the case then the inode must have been redirtied while it was being written 4916610a0bcSAndrew Morton * out and we don't reset its dirtied_when. 4926610a0bcSAndrew Morton */ 493f758eeabSChristoph Hellwig static void redirty_tail(struct inode *inode, struct bdi_writeback *wb) 4946610a0bcSAndrew Morton { 49503ba3782SJens Axboe if (!list_empty(&wb->b_dirty)) { 49666f3b8e2SJens Axboe struct inode *tail; 4976610a0bcSAndrew Morton 4987ccf19a8SNick Piggin tail = wb_inode(wb->b_dirty.next); 49966f3b8e2SJens Axboe if (time_before(inode->dirtied_when, tail->dirtied_when)) 5006610a0bcSAndrew Morton inode->dirtied_when = jiffies; 5016610a0bcSAndrew Morton } 502d6c10f1fSTejun Heo inode_wb_list_move_locked(inode, wb, &wb->b_dirty); 5036610a0bcSAndrew Morton } 5046610a0bcSAndrew Morton 5056610a0bcSAndrew Morton /* 50666f3b8e2SJens Axboe * requeue inode for re-scanning after bdi->b_io list is exhausted. 507c986d1e2SAndrew Morton */ 508f758eeabSChristoph Hellwig static void requeue_io(struct inode *inode, struct bdi_writeback *wb) 509c986d1e2SAndrew Morton { 510d6c10f1fSTejun Heo inode_wb_list_move_locked(inode, wb, &wb->b_more_io); 511c986d1e2SAndrew Morton } 512c986d1e2SAndrew Morton 5131c0eeaf5SJoern Engel static void inode_sync_complete(struct inode *inode) 5141c0eeaf5SJoern Engel { 515365b94aeSJan Kara inode->i_state &= ~I_SYNC; 5164eff96ddSJan Kara /* If inode is clean an unused, put it into LRU now... */ 5174eff96ddSJan Kara inode_add_lru(inode); 518365b94aeSJan Kara /* Waiters must see I_SYNC cleared before being woken up */ 5191c0eeaf5SJoern Engel smp_mb(); 5201c0eeaf5SJoern Engel wake_up_bit(&inode->i_state, __I_SYNC); 5211c0eeaf5SJoern Engel } 5221c0eeaf5SJoern Engel 523d2caa3c5SJeff Layton static bool inode_dirtied_after(struct inode *inode, unsigned long t) 524d2caa3c5SJeff Layton { 525d2caa3c5SJeff Layton bool ret = time_after(inode->dirtied_when, t); 526d2caa3c5SJeff Layton #ifndef CONFIG_64BIT 527d2caa3c5SJeff Layton /* 528d2caa3c5SJeff Layton * For inodes being constantly redirtied, dirtied_when can get stuck. 529d2caa3c5SJeff Layton * It _appears_ to be in the future, but is actually in distant past. 530d2caa3c5SJeff Layton * This test is necessary to prevent such wrapped-around relative times 5315b0830cbSJens Axboe * from permanently stopping the whole bdi writeback. 532d2caa3c5SJeff Layton */ 533d2caa3c5SJeff Layton ret = ret && time_before_eq(inode->dirtied_when, jiffies); 534d2caa3c5SJeff Layton #endif 535d2caa3c5SJeff Layton return ret; 536d2caa3c5SJeff Layton } 537d2caa3c5SJeff Layton 5380ae45f63STheodore Ts'o #define EXPIRE_DIRTY_ATIME 0x0001 5390ae45f63STheodore Ts'o 540c986d1e2SAndrew Morton /* 5410e2f2b23SWang Sheng-Hui * Move expired (dirtied before work->older_than_this) dirty inodes from 542697e6fedSJan Kara * @delaying_queue to @dispatch_queue. 5432c136579SFengguang Wu */ 544e84d0a4fSWu Fengguang static int move_expired_inodes(struct list_head *delaying_queue, 5452c136579SFengguang Wu struct list_head *dispatch_queue, 5460ae45f63STheodore Ts'o int flags, 547ad4e38ddSCurt Wohlgemuth struct wb_writeback_work *work) 5482c136579SFengguang Wu { 5490ae45f63STheodore Ts'o unsigned long *older_than_this = NULL; 5500ae45f63STheodore Ts'o unsigned long expire_time; 5515c03449dSShaohua Li LIST_HEAD(tmp); 5525c03449dSShaohua Li struct list_head *pos, *node; 553cf137307SJens Axboe struct super_block *sb = NULL; 5545c03449dSShaohua Li struct inode *inode; 555cf137307SJens Axboe int do_sb_sort = 0; 556e84d0a4fSWu Fengguang int moved = 0; 5575c03449dSShaohua Li 5580ae45f63STheodore Ts'o if ((flags & EXPIRE_DIRTY_ATIME) == 0) 5590ae45f63STheodore Ts'o older_than_this = work->older_than_this; 560a2f48706STheodore Ts'o else if (!work->for_sync) { 561a2f48706STheodore Ts'o expire_time = jiffies - (dirtytime_expire_interval * HZ); 5620ae45f63STheodore Ts'o older_than_this = &expire_time; 5630ae45f63STheodore Ts'o } 5642c136579SFengguang Wu while (!list_empty(delaying_queue)) { 5657ccf19a8SNick Piggin inode = wb_inode(delaying_queue->prev); 5660ae45f63STheodore Ts'o if (older_than_this && 5670ae45f63STheodore Ts'o inode_dirtied_after(inode, *older_than_this)) 5682c136579SFengguang Wu break; 569a8855990SJan Kara list_move(&inode->i_wb_list, &tmp); 570a8855990SJan Kara moved++; 5710ae45f63STheodore Ts'o if (flags & EXPIRE_DIRTY_ATIME) 5720ae45f63STheodore Ts'o set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state); 573a8855990SJan Kara if (sb_is_blkdev_sb(inode->i_sb)) 574a8855990SJan Kara continue; 575cf137307SJens Axboe if (sb && sb != inode->i_sb) 576cf137307SJens Axboe do_sb_sort = 1; 577cf137307SJens Axboe sb = inode->i_sb; 5785c03449dSShaohua Li } 5795c03449dSShaohua Li 580cf137307SJens Axboe /* just one sb in list, splice to dispatch_queue and we're done */ 581cf137307SJens Axboe if (!do_sb_sort) { 582cf137307SJens Axboe list_splice(&tmp, dispatch_queue); 583e84d0a4fSWu Fengguang goto out; 584cf137307SJens Axboe } 585cf137307SJens Axboe 5865c03449dSShaohua Li /* Move inodes from one superblock together */ 5875c03449dSShaohua Li while (!list_empty(&tmp)) { 5887ccf19a8SNick Piggin sb = wb_inode(tmp.prev)->i_sb; 5895c03449dSShaohua Li list_for_each_prev_safe(pos, node, &tmp) { 5907ccf19a8SNick Piggin inode = wb_inode(pos); 5915c03449dSShaohua Li if (inode->i_sb == sb) 5927ccf19a8SNick Piggin list_move(&inode->i_wb_list, dispatch_queue); 5932c136579SFengguang Wu } 5942c136579SFengguang Wu } 595e84d0a4fSWu Fengguang out: 596e84d0a4fSWu Fengguang return moved; 5975c03449dSShaohua Li } 5982c136579SFengguang Wu 5992c136579SFengguang Wu /* 6002c136579SFengguang Wu * Queue all expired dirty inodes for io, eldest first. 6014ea879b9SWu Fengguang * Before 6024ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 6034ea879b9SWu Fengguang * =============> gf edc BA 6044ea879b9SWu Fengguang * After 6054ea879b9SWu Fengguang * newly dirtied b_dirty b_io b_more_io 6064ea879b9SWu Fengguang * =============> g fBAedc 6074ea879b9SWu Fengguang * | 6084ea879b9SWu Fengguang * +--> dequeue for IO 6092c136579SFengguang Wu */ 610ad4e38ddSCurt Wohlgemuth static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work) 6112c136579SFengguang Wu { 612e84d0a4fSWu Fengguang int moved; 6130ae45f63STheodore Ts'o 614f758eeabSChristoph Hellwig assert_spin_locked(&wb->list_lock); 6154ea879b9SWu Fengguang list_splice_init(&wb->b_more_io, &wb->b_io); 6160ae45f63STheodore Ts'o moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work); 6170ae45f63STheodore Ts'o moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io, 6180ae45f63STheodore Ts'o EXPIRE_DIRTY_ATIME, work); 619d6c10f1fSTejun Heo if (moved) 620d6c10f1fSTejun Heo wb_io_lists_populated(wb); 621ad4e38ddSCurt Wohlgemuth trace_writeback_queue_io(wb, work, moved); 62266f3b8e2SJens Axboe } 62366f3b8e2SJens Axboe 624a9185b41SChristoph Hellwig static int write_inode(struct inode *inode, struct writeback_control *wbc) 62566f3b8e2SJens Axboe { 6269fb0a7daSTejun Heo int ret; 6279fb0a7daSTejun Heo 6289fb0a7daSTejun Heo if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) { 6299fb0a7daSTejun Heo trace_writeback_write_inode_start(inode, wbc); 6309fb0a7daSTejun Heo ret = inode->i_sb->s_op->write_inode(inode, wbc); 6319fb0a7daSTejun Heo trace_writeback_write_inode(inode, wbc); 6329fb0a7daSTejun Heo return ret; 6339fb0a7daSTejun Heo } 63403ba3782SJens Axboe return 0; 63566f3b8e2SJens Axboe } 63608d8e974SFengguang Wu 6372c136579SFengguang Wu /* 638169ebd90SJan Kara * Wait for writeback on an inode to complete. Called with i_lock held. 639169ebd90SJan Kara * Caller must make sure inode cannot go away when we drop i_lock. 64001c03194SChristoph Hellwig */ 641169ebd90SJan Kara static void __inode_wait_for_writeback(struct inode *inode) 642169ebd90SJan Kara __releases(inode->i_lock) 643169ebd90SJan Kara __acquires(inode->i_lock) 64401c03194SChristoph Hellwig { 64501c03194SChristoph Hellwig DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); 64601c03194SChristoph Hellwig wait_queue_head_t *wqh; 64701c03194SChristoph Hellwig 64801c03194SChristoph Hellwig wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 64958a9d3d8SRichard Kennedy while (inode->i_state & I_SYNC) { 650250df6edSDave Chinner spin_unlock(&inode->i_lock); 65174316201SNeilBrown __wait_on_bit(wqh, &wq, bit_wait, 65274316201SNeilBrown TASK_UNINTERRUPTIBLE); 653250df6edSDave Chinner spin_lock(&inode->i_lock); 65458a9d3d8SRichard Kennedy } 65501c03194SChristoph Hellwig } 65601c03194SChristoph Hellwig 65701c03194SChristoph Hellwig /* 658169ebd90SJan Kara * Wait for writeback on an inode to complete. Caller must have inode pinned. 659169ebd90SJan Kara */ 660169ebd90SJan Kara void inode_wait_for_writeback(struct inode *inode) 661169ebd90SJan Kara { 662169ebd90SJan Kara spin_lock(&inode->i_lock); 663169ebd90SJan Kara __inode_wait_for_writeback(inode); 664169ebd90SJan Kara spin_unlock(&inode->i_lock); 665169ebd90SJan Kara } 666169ebd90SJan Kara 667169ebd90SJan Kara /* 668169ebd90SJan Kara * Sleep until I_SYNC is cleared. This function must be called with i_lock 669169ebd90SJan Kara * held and drops it. It is aimed for callers not holding any inode reference 670169ebd90SJan Kara * so once i_lock is dropped, inode can go away. 671169ebd90SJan Kara */ 672169ebd90SJan Kara static void inode_sleep_on_writeback(struct inode *inode) 673169ebd90SJan Kara __releases(inode->i_lock) 674169ebd90SJan Kara { 675169ebd90SJan Kara DEFINE_WAIT(wait); 676169ebd90SJan Kara wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC); 677169ebd90SJan Kara int sleep; 678169ebd90SJan Kara 679169ebd90SJan Kara prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); 680169ebd90SJan Kara sleep = inode->i_state & I_SYNC; 681169ebd90SJan Kara spin_unlock(&inode->i_lock); 682169ebd90SJan Kara if (sleep) 683169ebd90SJan Kara schedule(); 684169ebd90SJan Kara finish_wait(wqh, &wait); 685169ebd90SJan Kara } 686169ebd90SJan Kara 687169ebd90SJan Kara /* 688ccb26b5aSJan Kara * Find proper writeback list for the inode depending on its current state and 689ccb26b5aSJan Kara * possibly also change of its state while we were doing writeback. Here we 690ccb26b5aSJan Kara * handle things such as livelock prevention or fairness of writeback among 691ccb26b5aSJan Kara * inodes. This function can be called only by flusher thread - noone else 692ccb26b5aSJan Kara * processes all inodes in writeback lists and requeueing inodes behind flusher 693ccb26b5aSJan Kara * thread's back can have unexpected consequences. 694ccb26b5aSJan Kara */ 695ccb26b5aSJan Kara static void requeue_inode(struct inode *inode, struct bdi_writeback *wb, 696ccb26b5aSJan Kara struct writeback_control *wbc) 697ccb26b5aSJan Kara { 698ccb26b5aSJan Kara if (inode->i_state & I_FREEING) 699ccb26b5aSJan Kara return; 700ccb26b5aSJan Kara 701ccb26b5aSJan Kara /* 702ccb26b5aSJan Kara * Sync livelock prevention. Each inode is tagged and synced in one 703ccb26b5aSJan Kara * shot. If still dirty, it will be redirty_tail()'ed below. Update 704ccb26b5aSJan Kara * the dirty time to prevent enqueue and sync it again. 705ccb26b5aSJan Kara */ 706ccb26b5aSJan Kara if ((inode->i_state & I_DIRTY) && 707ccb26b5aSJan Kara (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)) 708ccb26b5aSJan Kara inode->dirtied_when = jiffies; 709ccb26b5aSJan Kara 7104f8ad655SJan Kara if (wbc->pages_skipped) { 7114f8ad655SJan Kara /* 7124f8ad655SJan Kara * writeback is not making progress due to locked 7134f8ad655SJan Kara * buffers. Skip this inode for now. 7144f8ad655SJan Kara */ 7154f8ad655SJan Kara redirty_tail(inode, wb); 7164f8ad655SJan Kara return; 7174f8ad655SJan Kara } 7184f8ad655SJan Kara 719ccb26b5aSJan Kara if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) { 720ccb26b5aSJan Kara /* 721ccb26b5aSJan Kara * We didn't write back all the pages. nfs_writepages() 722ccb26b5aSJan Kara * sometimes bales out without doing anything. 723ccb26b5aSJan Kara */ 724ccb26b5aSJan Kara if (wbc->nr_to_write <= 0) { 725ccb26b5aSJan Kara /* Slice used up. Queue for next turn. */ 726ccb26b5aSJan Kara requeue_io(inode, wb); 727ccb26b5aSJan Kara } else { 728ccb26b5aSJan Kara /* 729ccb26b5aSJan Kara * Writeback blocked by something other than 730ccb26b5aSJan Kara * congestion. Delay the inode for some time to 731ccb26b5aSJan Kara * avoid spinning on the CPU (100% iowait) 732ccb26b5aSJan Kara * retrying writeback of the dirty page/inode 733ccb26b5aSJan Kara * that cannot be performed immediately. 734ccb26b5aSJan Kara */ 735ccb26b5aSJan Kara redirty_tail(inode, wb); 736ccb26b5aSJan Kara } 737ccb26b5aSJan Kara } else if (inode->i_state & I_DIRTY) { 738ccb26b5aSJan Kara /* 739ccb26b5aSJan Kara * Filesystems can dirty the inode during writeback operations, 740ccb26b5aSJan Kara * such as delayed allocation during submission or metadata 741ccb26b5aSJan Kara * updates after data IO completion. 742ccb26b5aSJan Kara */ 743ccb26b5aSJan Kara redirty_tail(inode, wb); 7440ae45f63STheodore Ts'o } else if (inode->i_state & I_DIRTY_TIME) { 745a2f48706STheodore Ts'o inode->dirtied_when = jiffies; 746d6c10f1fSTejun Heo inode_wb_list_move_locked(inode, wb, &wb->b_dirty_time); 747ccb26b5aSJan Kara } else { 748ccb26b5aSJan Kara /* The inode is clean. Remove from writeback lists. */ 749d6c10f1fSTejun Heo inode_wb_list_del_locked(inode, wb); 750ccb26b5aSJan Kara } 751ccb26b5aSJan Kara } 752ccb26b5aSJan Kara 753ccb26b5aSJan Kara /* 7544f8ad655SJan Kara * Write out an inode and its dirty pages. Do not update the writeback list 7554f8ad655SJan Kara * linkage. That is left to the caller. The caller is also responsible for 7564f8ad655SJan Kara * setting I_SYNC flag and calling inode_sync_complete() to clear it. 7571da177e4SLinus Torvalds */ 7581da177e4SLinus Torvalds static int 759cd8ed2a4SYan Hong __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) 7601da177e4SLinus Torvalds { 7611da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 762251d6a47SWu Fengguang long nr_to_write = wbc->nr_to_write; 76301c03194SChristoph Hellwig unsigned dirty; 7641da177e4SLinus Torvalds int ret; 7651da177e4SLinus Torvalds 7664f8ad655SJan Kara WARN_ON(!(inode->i_state & I_SYNC)); 7671da177e4SLinus Torvalds 7689fb0a7daSTejun Heo trace_writeback_single_inode_start(inode, wbc, nr_to_write); 7699fb0a7daSTejun Heo 7701da177e4SLinus Torvalds ret = do_writepages(mapping, wbc); 7711da177e4SLinus Torvalds 77226821ed4SChristoph Hellwig /* 77326821ed4SChristoph Hellwig * Make sure to wait on the data before writing out the metadata. 77426821ed4SChristoph Hellwig * This is important for filesystems that modify metadata on data 7757747bd4bSDave Chinner * I/O completion. We don't do it for sync(2) writeback because it has a 7767747bd4bSDave Chinner * separate, external IO completion path and ->sync_fs for guaranteeing 7777747bd4bSDave Chinner * inode metadata is written back correctly. 77826821ed4SChristoph Hellwig */ 7797747bd4bSDave Chinner if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) { 78026821ed4SChristoph Hellwig int err = filemap_fdatawait(mapping); 7811da177e4SLinus Torvalds if (ret == 0) 7821da177e4SLinus Torvalds ret = err; 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds 7855547e8aaSDmitry Monakhov /* 7865547e8aaSDmitry Monakhov * Some filesystems may redirty the inode during the writeback 7875547e8aaSDmitry Monakhov * due to delalloc, clear dirty metadata flags right before 7885547e8aaSDmitry Monakhov * write_inode() 7895547e8aaSDmitry Monakhov */ 790250df6edSDave Chinner spin_lock(&inode->i_lock); 7919c6ac78eSTejun Heo 7925547e8aaSDmitry Monakhov dirty = inode->i_state & I_DIRTY; 793a2f48706STheodore Ts'o if (inode->i_state & I_DIRTY_TIME) { 794a2f48706STheodore Ts'o if ((dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) || 795a2f48706STheodore Ts'o unlikely(inode->i_state & I_DIRTY_TIME_EXPIRED) || 796a2f48706STheodore Ts'o unlikely(time_after(jiffies, 797a2f48706STheodore Ts'o (inode->dirtied_time_when + 798a2f48706STheodore Ts'o dirtytime_expire_interval * HZ)))) { 7990ae45f63STheodore Ts'o dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED; 8000ae45f63STheodore Ts'o trace_writeback_lazytime(inode); 8010ae45f63STheodore Ts'o } 802a2f48706STheodore Ts'o } else 803a2f48706STheodore Ts'o inode->i_state &= ~I_DIRTY_TIME_EXPIRED; 8040ae45f63STheodore Ts'o inode->i_state &= ~dirty; 8059c6ac78eSTejun Heo 8069c6ac78eSTejun Heo /* 8079c6ac78eSTejun Heo * Paired with smp_mb() in __mark_inode_dirty(). This allows 8089c6ac78eSTejun Heo * __mark_inode_dirty() to test i_state without grabbing i_lock - 8099c6ac78eSTejun Heo * either they see the I_DIRTY bits cleared or we see the dirtied 8109c6ac78eSTejun Heo * inode. 8119c6ac78eSTejun Heo * 8129c6ac78eSTejun Heo * I_DIRTY_PAGES is always cleared together above even if @mapping 8139c6ac78eSTejun Heo * still has dirty pages. The flag is reinstated after smp_mb() if 8149c6ac78eSTejun Heo * necessary. This guarantees that either __mark_inode_dirty() 8159c6ac78eSTejun Heo * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY. 8169c6ac78eSTejun Heo */ 8179c6ac78eSTejun Heo smp_mb(); 8189c6ac78eSTejun Heo 8199c6ac78eSTejun Heo if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 8209c6ac78eSTejun Heo inode->i_state |= I_DIRTY_PAGES; 8219c6ac78eSTejun Heo 822250df6edSDave Chinner spin_unlock(&inode->i_lock); 8239c6ac78eSTejun Heo 8240ae45f63STheodore Ts'o if (dirty & I_DIRTY_TIME) 8250ae45f63STheodore Ts'o mark_inode_dirty_sync(inode); 82626821ed4SChristoph Hellwig /* Don't write the inode if only I_DIRTY_PAGES was set */ 8270ae45f63STheodore Ts'o if (dirty & ~I_DIRTY_PAGES) { 828a9185b41SChristoph Hellwig int err = write_inode(inode, wbc); 8291da177e4SLinus Torvalds if (ret == 0) 8301da177e4SLinus Torvalds ret = err; 8311da177e4SLinus Torvalds } 8324f8ad655SJan Kara trace_writeback_single_inode(inode, wbc, nr_to_write); 8334f8ad655SJan Kara return ret; 8344f8ad655SJan Kara } 8354f8ad655SJan Kara 8364f8ad655SJan Kara /* 8374f8ad655SJan Kara * Write out an inode's dirty pages. Either the caller has an active reference 8384f8ad655SJan Kara * on the inode or the inode has I_WILL_FREE set. 8394f8ad655SJan Kara * 8404f8ad655SJan Kara * This function is designed to be called for writing back one inode which 8414f8ad655SJan Kara * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode() 8424f8ad655SJan Kara * and does more profound writeback list handling in writeback_sb_inodes(). 8434f8ad655SJan Kara */ 8444f8ad655SJan Kara static int 8454f8ad655SJan Kara writeback_single_inode(struct inode *inode, struct bdi_writeback *wb, 8464f8ad655SJan Kara struct writeback_control *wbc) 8474f8ad655SJan Kara { 8484f8ad655SJan Kara int ret = 0; 8494f8ad655SJan Kara 8504f8ad655SJan Kara spin_lock(&inode->i_lock); 8514f8ad655SJan Kara if (!atomic_read(&inode->i_count)) 8524f8ad655SJan Kara WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); 8534f8ad655SJan Kara else 8544f8ad655SJan Kara WARN_ON(inode->i_state & I_WILL_FREE); 8554f8ad655SJan Kara 8564f8ad655SJan Kara if (inode->i_state & I_SYNC) { 8574f8ad655SJan Kara if (wbc->sync_mode != WB_SYNC_ALL) 8584f8ad655SJan Kara goto out; 8594f8ad655SJan Kara /* 860169ebd90SJan Kara * It's a data-integrity sync. We must wait. Since callers hold 861169ebd90SJan Kara * inode reference or inode has I_WILL_FREE set, it cannot go 862169ebd90SJan Kara * away under us. 8634f8ad655SJan Kara */ 864169ebd90SJan Kara __inode_wait_for_writeback(inode); 8654f8ad655SJan Kara } 8664f8ad655SJan Kara WARN_ON(inode->i_state & I_SYNC); 8674f8ad655SJan Kara /* 868f9b0e058SJan Kara * Skip inode if it is clean and we have no outstanding writeback in 869f9b0e058SJan Kara * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this 870f9b0e058SJan Kara * function since flusher thread may be doing for example sync in 871f9b0e058SJan Kara * parallel and if we move the inode, it could get skipped. So here we 872f9b0e058SJan Kara * make sure inode is on some writeback list and leave it there unless 873f9b0e058SJan Kara * we have completely cleaned the inode. 8744f8ad655SJan Kara */ 8750ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL) && 876f9b0e058SJan Kara (wbc->sync_mode != WB_SYNC_ALL || 877f9b0e058SJan Kara !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))) 8784f8ad655SJan Kara goto out; 8794f8ad655SJan Kara inode->i_state |= I_SYNC; 8804f8ad655SJan Kara spin_unlock(&inode->i_lock); 8814f8ad655SJan Kara 882cd8ed2a4SYan Hong ret = __writeback_single_inode(inode, wbc); 8831da177e4SLinus Torvalds 884f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 885250df6edSDave Chinner spin_lock(&inode->i_lock); 8864f8ad655SJan Kara /* 8874f8ad655SJan Kara * If inode is clean, remove it from writeback lists. Otherwise don't 8884f8ad655SJan Kara * touch it. See comment above for explanation. 8894f8ad655SJan Kara */ 8900ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 891d6c10f1fSTejun Heo inode_wb_list_del_locked(inode, wb); 8924f8ad655SJan Kara spin_unlock(&wb->list_lock); 8931c0eeaf5SJoern Engel inode_sync_complete(inode); 8944f8ad655SJan Kara out: 8954f8ad655SJan Kara spin_unlock(&inode->i_lock); 8961da177e4SLinus Torvalds return ret; 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds 899a88a341aSTejun Heo static long writeback_chunk_size(struct bdi_writeback *wb, 9001a12d8bdSWu Fengguang struct wb_writeback_work *work) 901d46db3d5SWu Fengguang { 902d46db3d5SWu Fengguang long pages; 903d46db3d5SWu Fengguang 904d46db3d5SWu Fengguang /* 905d46db3d5SWu Fengguang * WB_SYNC_ALL mode does livelock avoidance by syncing dirty 906d46db3d5SWu Fengguang * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX 907d46db3d5SWu Fengguang * here avoids calling into writeback_inodes_wb() more than once. 908d46db3d5SWu Fengguang * 909d46db3d5SWu Fengguang * The intended call sequence for WB_SYNC_ALL writeback is: 910d46db3d5SWu Fengguang * 911d46db3d5SWu Fengguang * wb_writeback() 912d46db3d5SWu Fengguang * writeback_sb_inodes() <== called only once 913d46db3d5SWu Fengguang * write_cache_pages() <== called once for each inode 914d46db3d5SWu Fengguang * (quickly) tag currently dirty pages 915d46db3d5SWu Fengguang * (maybe slowly) sync all tagged pages 916d46db3d5SWu Fengguang */ 917d46db3d5SWu Fengguang if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages) 918d46db3d5SWu Fengguang pages = LONG_MAX; 9191a12d8bdSWu Fengguang else { 920a88a341aSTejun Heo pages = min(wb->avg_write_bandwidth / 2, 921dcc25ae7STejun Heo global_wb_domain.dirty_limit / DIRTY_SCOPE); 9221a12d8bdSWu Fengguang pages = min(pages, work->nr_pages); 9231a12d8bdSWu Fengguang pages = round_down(pages + MIN_WRITEBACK_PAGES, 9241a12d8bdSWu Fengguang MIN_WRITEBACK_PAGES); 9251a12d8bdSWu Fengguang } 926d46db3d5SWu Fengguang 927d46db3d5SWu Fengguang return pages; 928d46db3d5SWu Fengguang } 929d46db3d5SWu Fengguang 93003ba3782SJens Axboe /* 931f11c9c5cSEdward Shishkin * Write a portion of b_io inodes which belong to @sb. 932edadfb10SChristoph Hellwig * 933d46db3d5SWu Fengguang * Return the number of pages and/or inodes written. 934f11c9c5cSEdward Shishkin */ 935d46db3d5SWu Fengguang static long writeback_sb_inodes(struct super_block *sb, 936d46db3d5SWu Fengguang struct bdi_writeback *wb, 937d46db3d5SWu Fengguang struct wb_writeback_work *work) 93803ba3782SJens Axboe { 939d46db3d5SWu Fengguang struct writeback_control wbc = { 940d46db3d5SWu Fengguang .sync_mode = work->sync_mode, 941d46db3d5SWu Fengguang .tagged_writepages = work->tagged_writepages, 942d46db3d5SWu Fengguang .for_kupdate = work->for_kupdate, 943d46db3d5SWu Fengguang .for_background = work->for_background, 9447747bd4bSDave Chinner .for_sync = work->for_sync, 945d46db3d5SWu Fengguang .range_cyclic = work->range_cyclic, 946d46db3d5SWu Fengguang .range_start = 0, 947d46db3d5SWu Fengguang .range_end = LLONG_MAX, 948d46db3d5SWu Fengguang }; 949d46db3d5SWu Fengguang unsigned long start_time = jiffies; 950d46db3d5SWu Fengguang long write_chunk; 951d46db3d5SWu Fengguang long wrote = 0; /* count both pages and inodes */ 952d46db3d5SWu Fengguang 95303ba3782SJens Axboe while (!list_empty(&wb->b_io)) { 9547ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 955edadfb10SChristoph Hellwig 956edadfb10SChristoph Hellwig if (inode->i_sb != sb) { 957d46db3d5SWu Fengguang if (work->sb) { 958edadfb10SChristoph Hellwig /* 959edadfb10SChristoph Hellwig * We only want to write back data for this 960edadfb10SChristoph Hellwig * superblock, move all inodes not belonging 961edadfb10SChristoph Hellwig * to it back onto the dirty list. 962edadfb10SChristoph Hellwig */ 963f758eeabSChristoph Hellwig redirty_tail(inode, wb); 96466f3b8e2SJens Axboe continue; 96566f3b8e2SJens Axboe } 966edadfb10SChristoph Hellwig 967edadfb10SChristoph Hellwig /* 968edadfb10SChristoph Hellwig * The inode belongs to a different superblock. 969edadfb10SChristoph Hellwig * Bounce back to the caller to unpin this and 970edadfb10SChristoph Hellwig * pin the next superblock. 971edadfb10SChristoph Hellwig */ 972d46db3d5SWu Fengguang break; 973edadfb10SChristoph Hellwig } 974edadfb10SChristoph Hellwig 9759843b76aSChristoph Hellwig /* 976331cbdeeSWanpeng Li * Don't bother with new inodes or inodes being freed, first 977331cbdeeSWanpeng Li * kind does not need periodic writeout yet, and for the latter 9789843b76aSChristoph Hellwig * kind writeout is handled by the freer. 9799843b76aSChristoph Hellwig */ 980250df6edSDave Chinner spin_lock(&inode->i_lock); 9819843b76aSChristoph Hellwig if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { 982250df6edSDave Chinner spin_unlock(&inode->i_lock); 983fcc5c222SWu Fengguang redirty_tail(inode, wb); 9847ef0d737SNick Piggin continue; 9857ef0d737SNick Piggin } 986cc1676d9SJan Kara if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) { 987cc1676d9SJan Kara /* 988cc1676d9SJan Kara * If this inode is locked for writeback and we are not 989cc1676d9SJan Kara * doing writeback-for-data-integrity, move it to 990cc1676d9SJan Kara * b_more_io so that writeback can proceed with the 991cc1676d9SJan Kara * other inodes on s_io. 992cc1676d9SJan Kara * 993cc1676d9SJan Kara * We'll have another go at writing back this inode 994cc1676d9SJan Kara * when we completed a full scan of b_io. 995cc1676d9SJan Kara */ 996cc1676d9SJan Kara spin_unlock(&inode->i_lock); 997cc1676d9SJan Kara requeue_io(inode, wb); 998cc1676d9SJan Kara trace_writeback_sb_inodes_requeue(inode); 999cc1676d9SJan Kara continue; 1000cc1676d9SJan Kara } 1001f0d07b7fSJan Kara spin_unlock(&wb->list_lock); 1002f0d07b7fSJan Kara 10034f8ad655SJan Kara /* 10044f8ad655SJan Kara * We already requeued the inode if it had I_SYNC set and we 10054f8ad655SJan Kara * are doing WB_SYNC_NONE writeback. So this catches only the 10064f8ad655SJan Kara * WB_SYNC_ALL case. 10074f8ad655SJan Kara */ 1008169ebd90SJan Kara if (inode->i_state & I_SYNC) { 1009169ebd90SJan Kara /* Wait for I_SYNC. This function drops i_lock... */ 1010169ebd90SJan Kara inode_sleep_on_writeback(inode); 1011169ebd90SJan Kara /* Inode may be gone, start again */ 1012ead188f9SJan Kara spin_lock(&wb->list_lock); 1013169ebd90SJan Kara continue; 1014169ebd90SJan Kara } 10154f8ad655SJan Kara inode->i_state |= I_SYNC; 10164f8ad655SJan Kara spin_unlock(&inode->i_lock); 1017169ebd90SJan Kara 1018a88a341aSTejun Heo write_chunk = writeback_chunk_size(wb, work); 1019d46db3d5SWu Fengguang wbc.nr_to_write = write_chunk; 1020d46db3d5SWu Fengguang wbc.pages_skipped = 0; 1021250df6edSDave Chinner 1022169ebd90SJan Kara /* 1023169ebd90SJan Kara * We use I_SYNC to pin the inode in memory. While it is set 1024169ebd90SJan Kara * evict_inode() will wait so the inode cannot be freed. 1025169ebd90SJan Kara */ 1026cd8ed2a4SYan Hong __writeback_single_inode(inode, &wbc); 1027d46db3d5SWu Fengguang 1028d46db3d5SWu Fengguang work->nr_pages -= write_chunk - wbc.nr_to_write; 1029d46db3d5SWu Fengguang wrote += write_chunk - wbc.nr_to_write; 10304f8ad655SJan Kara spin_lock(&wb->list_lock); 10314f8ad655SJan Kara spin_lock(&inode->i_lock); 10320ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 1033d46db3d5SWu Fengguang wrote++; 10344f8ad655SJan Kara requeue_inode(inode, wb, &wbc); 10354f8ad655SJan Kara inode_sync_complete(inode); 10360f1b1fd8SDave Chinner spin_unlock(&inode->i_lock); 1037169ebd90SJan Kara cond_resched_lock(&wb->list_lock); 1038d46db3d5SWu Fengguang /* 1039d46db3d5SWu Fengguang * bail out to wb_writeback() often enough to check 1040d46db3d5SWu Fengguang * background threshold and other termination conditions. 1041d46db3d5SWu Fengguang */ 1042d46db3d5SWu Fengguang if (wrote) { 1043d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 1044d46db3d5SWu Fengguang break; 1045d46db3d5SWu Fengguang if (work->nr_pages <= 0) 1046d46db3d5SWu Fengguang break; 10471da177e4SLinus Torvalds } 10488bc3be27SFengguang Wu } 1049d46db3d5SWu Fengguang return wrote; 1050f11c9c5cSEdward Shishkin } 105138f21977SNick Piggin 1052d46db3d5SWu Fengguang static long __writeback_inodes_wb(struct bdi_writeback *wb, 1053d46db3d5SWu Fengguang struct wb_writeback_work *work) 1054f11c9c5cSEdward Shishkin { 1055d46db3d5SWu Fengguang unsigned long start_time = jiffies; 1056d46db3d5SWu Fengguang long wrote = 0; 1057f11c9c5cSEdward Shishkin 1058f11c9c5cSEdward Shishkin while (!list_empty(&wb->b_io)) { 10597ccf19a8SNick Piggin struct inode *inode = wb_inode(wb->b_io.prev); 1060f11c9c5cSEdward Shishkin struct super_block *sb = inode->i_sb; 1061f11c9c5cSEdward Shishkin 1062eb6ef3dfSKonstantin Khlebnikov if (!trylock_super(sb)) { 10630e995816SWu Fengguang /* 1064eb6ef3dfSKonstantin Khlebnikov * trylock_super() may fail consistently due to 10650e995816SWu Fengguang * s_umount being grabbed by someone else. Don't use 10660e995816SWu Fengguang * requeue_io() to avoid busy retrying the inode/sb. 10670e995816SWu Fengguang */ 10680e995816SWu Fengguang redirty_tail(inode, wb); 1069d19de7edSChristoph Hellwig continue; 1070334132aeSChristoph Hellwig } 1071d46db3d5SWu Fengguang wrote += writeback_sb_inodes(sb, wb, work); 1072eb6ef3dfSKonstantin Khlebnikov up_read(&sb->s_umount); 1073f11c9c5cSEdward Shishkin 1074d46db3d5SWu Fengguang /* refer to the same tests at the end of writeback_sb_inodes */ 1075d46db3d5SWu Fengguang if (wrote) { 1076d46db3d5SWu Fengguang if (time_is_before_jiffies(start_time + HZ / 10UL)) 1077d46db3d5SWu Fengguang break; 1078d46db3d5SWu Fengguang if (work->nr_pages <= 0) 1079f11c9c5cSEdward Shishkin break; 1080f11c9c5cSEdward Shishkin } 1081d46db3d5SWu Fengguang } 108266f3b8e2SJens Axboe /* Leave any unwritten inodes on b_io */ 1083d46db3d5SWu Fengguang return wrote; 108466f3b8e2SJens Axboe } 108566f3b8e2SJens Axboe 10867d9f073bSWanpeng Li static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages, 10870e175a18SCurt Wohlgemuth enum wb_reason reason) 1088edadfb10SChristoph Hellwig { 1089d46db3d5SWu Fengguang struct wb_writeback_work work = { 1090d46db3d5SWu Fengguang .nr_pages = nr_pages, 1091d46db3d5SWu Fengguang .sync_mode = WB_SYNC_NONE, 1092d46db3d5SWu Fengguang .range_cyclic = 1, 10930e175a18SCurt Wohlgemuth .reason = reason, 1094d46db3d5SWu Fengguang }; 1095edadfb10SChristoph Hellwig 1096f758eeabSChristoph Hellwig spin_lock(&wb->list_lock); 1097424b351fSWu Fengguang if (list_empty(&wb->b_io)) 1098ad4e38ddSCurt Wohlgemuth queue_io(wb, &work); 1099d46db3d5SWu Fengguang __writeback_inodes_wb(wb, &work); 1100f758eeabSChristoph Hellwig spin_unlock(&wb->list_lock); 1101edadfb10SChristoph Hellwig 1102d46db3d5SWu Fengguang return nr_pages - work.nr_pages; 110366f3b8e2SJens Axboe } 110466f3b8e2SJens Axboe 110503ba3782SJens Axboe /* 110603ba3782SJens Axboe * Explicit flushing or periodic writeback of "old" data. 110703ba3782SJens Axboe * 110803ba3782SJens Axboe * Define "old": the first time one of an inode's pages is dirtied, we mark the 110903ba3782SJens Axboe * dirtying-time in the inode's address_space. So this periodic writeback code 111003ba3782SJens Axboe * just walks the superblock inode list, writing back any inodes which are 111103ba3782SJens Axboe * older than a specific point in time. 111203ba3782SJens Axboe * 111303ba3782SJens Axboe * Try to run once per dirty_writeback_interval. But if a writeback event 111403ba3782SJens Axboe * takes longer than a dirty_writeback_interval interval, then leave a 111503ba3782SJens Axboe * one-second gap. 111603ba3782SJens Axboe * 111703ba3782SJens Axboe * older_than_this takes precedence over nr_to_write. So we'll only write back 111803ba3782SJens Axboe * all dirty pages if they are all attached to "old" mappings. 111903ba3782SJens Axboe */ 1120c4a77a6cSJens Axboe static long wb_writeback(struct bdi_writeback *wb, 112183ba7b07SChristoph Hellwig struct wb_writeback_work *work) 112203ba3782SJens Axboe { 1123e98be2d5SWu Fengguang unsigned long wb_start = jiffies; 1124d46db3d5SWu Fengguang long nr_pages = work->nr_pages; 11250dc83bd3SJan Kara unsigned long oldest_jif; 1126a5989bdcSJan Kara struct inode *inode; 1127d46db3d5SWu Fengguang long progress; 112803ba3782SJens Axboe 11290dc83bd3SJan Kara oldest_jif = jiffies; 11300dc83bd3SJan Kara work->older_than_this = &oldest_jif; 113103ba3782SJens Axboe 1132e8dfc305SWu Fengguang spin_lock(&wb->list_lock); 113303ba3782SJens Axboe for (;;) { 113403ba3782SJens Axboe /* 1135d3ddec76SWu Fengguang * Stop writeback when nr_pages has been consumed 113603ba3782SJens Axboe */ 113783ba7b07SChristoph Hellwig if (work->nr_pages <= 0) 113803ba3782SJens Axboe break; 113903ba3782SJens Axboe 114003ba3782SJens Axboe /* 1141aa373cf5SJan Kara * Background writeout and kupdate-style writeback may 1142aa373cf5SJan Kara * run forever. Stop them if there is other work to do 1143aa373cf5SJan Kara * so that e.g. sync can proceed. They'll be restarted 1144aa373cf5SJan Kara * after the other works are all done. 1145aa373cf5SJan Kara */ 1146aa373cf5SJan Kara if ((work->for_background || work->for_kupdate) && 1147f0054bb1STejun Heo !list_empty(&wb->work_list)) 1148aa373cf5SJan Kara break; 1149aa373cf5SJan Kara 1150aa373cf5SJan Kara /* 1151d3ddec76SWu Fengguang * For background writeout, stop when we are below the 1152d3ddec76SWu Fengguang * background dirty threshold 115303ba3782SJens Axboe */ 1154aa661bbeSTejun Heo if (work->for_background && !wb_over_bg_thresh(wb)) 115503ba3782SJens Axboe break; 115603ba3782SJens Axboe 11571bc36b64SJan Kara /* 11581bc36b64SJan Kara * Kupdate and background works are special and we want to 11591bc36b64SJan Kara * include all inodes that need writing. Livelock avoidance is 11601bc36b64SJan Kara * handled by these works yielding to any other work so we are 11611bc36b64SJan Kara * safe. 11621bc36b64SJan Kara */ 1163ba9aa839SWu Fengguang if (work->for_kupdate) { 11640dc83bd3SJan Kara oldest_jif = jiffies - 1165ba9aa839SWu Fengguang msecs_to_jiffies(dirty_expire_interval * 10); 11661bc36b64SJan Kara } else if (work->for_background) 11670dc83bd3SJan Kara oldest_jif = jiffies; 1168028c2dd1SDave Chinner 1169d46db3d5SWu Fengguang trace_writeback_start(wb->bdi, work); 1170e8dfc305SWu Fengguang if (list_empty(&wb->b_io)) 1171ad4e38ddSCurt Wohlgemuth queue_io(wb, work); 117283ba7b07SChristoph Hellwig if (work->sb) 1173d46db3d5SWu Fengguang progress = writeback_sb_inodes(work->sb, wb, work); 1174edadfb10SChristoph Hellwig else 1175d46db3d5SWu Fengguang progress = __writeback_inodes_wb(wb, work); 1176d46db3d5SWu Fengguang trace_writeback_written(wb->bdi, work); 1177028c2dd1SDave Chinner 1178e98be2d5SWu Fengguang wb_update_bandwidth(wb, wb_start); 117903ba3782SJens Axboe 118003ba3782SJens Axboe /* 118171fd05a8SJens Axboe * Did we write something? Try for more 1182e6fb6da2SWu Fengguang * 1183e6fb6da2SWu Fengguang * Dirty inodes are moved to b_io for writeback in batches. 1184e6fb6da2SWu Fengguang * The completion of the current batch does not necessarily 1185e6fb6da2SWu Fengguang * mean the overall work is done. So we keep looping as long 1186e6fb6da2SWu Fengguang * as made some progress on cleaning pages or inodes. 118771fd05a8SJens Axboe */ 1188d46db3d5SWu Fengguang if (progress) 118903ba3782SJens Axboe continue; 1190a5989bdcSJan Kara /* 1191e6fb6da2SWu Fengguang * No more inodes for IO, bail 1192a5989bdcSJan Kara */ 1193b7a2441fSWu Fengguang if (list_empty(&wb->b_more_io)) 119403ba3782SJens Axboe break; 119503ba3782SJens Axboe /* 11968010c3b6SJens Axboe * Nothing written. Wait for some inode to 11978010c3b6SJens Axboe * become available for writeback. Otherwise 11988010c3b6SJens Axboe * we'll just busyloop. 119903ba3782SJens Axboe */ 120003ba3782SJens Axboe if (!list_empty(&wb->b_more_io)) { 1201d46db3d5SWu Fengguang trace_writeback_wait(wb->bdi, work); 120203ba3782SJens Axboe inode = wb_inode(wb->b_more_io.prev); 1203250df6edSDave Chinner spin_lock(&inode->i_lock); 1204f0d07b7fSJan Kara spin_unlock(&wb->list_lock); 1205169ebd90SJan Kara /* This function drops i_lock... */ 1206169ebd90SJan Kara inode_sleep_on_writeback(inode); 1207f0d07b7fSJan Kara spin_lock(&wb->list_lock); 120803ba3782SJens Axboe } 120903ba3782SJens Axboe } 1210e8dfc305SWu Fengguang spin_unlock(&wb->list_lock); 121103ba3782SJens Axboe 1212d46db3d5SWu Fengguang return nr_pages - work->nr_pages; 121303ba3782SJens Axboe } 121403ba3782SJens Axboe 121503ba3782SJens Axboe /* 121683ba7b07SChristoph Hellwig * Return the next wb_writeback_work struct that hasn't been processed yet. 121703ba3782SJens Axboe */ 1218f0054bb1STejun Heo static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb) 121903ba3782SJens Axboe { 122083ba7b07SChristoph Hellwig struct wb_writeback_work *work = NULL; 122103ba3782SJens Axboe 1222f0054bb1STejun Heo spin_lock_bh(&wb->work_lock); 1223f0054bb1STejun Heo if (!list_empty(&wb->work_list)) { 1224f0054bb1STejun Heo work = list_entry(wb->work_list.next, 122583ba7b07SChristoph Hellwig struct wb_writeback_work, list); 122683ba7b07SChristoph Hellwig list_del_init(&work->list); 122703ba3782SJens Axboe } 1228f0054bb1STejun Heo spin_unlock_bh(&wb->work_lock); 122983ba7b07SChristoph Hellwig return work; 123003ba3782SJens Axboe } 123103ba3782SJens Axboe 1232cdf01dd5SLinus Torvalds /* 1233cdf01dd5SLinus Torvalds * Add in the number of potentially dirty inodes, because each inode 1234cdf01dd5SLinus Torvalds * write can dirty pagecache in the underlying blockdev. 1235cdf01dd5SLinus Torvalds */ 1236cdf01dd5SLinus Torvalds static unsigned long get_nr_dirty_pages(void) 1237cdf01dd5SLinus Torvalds { 1238cdf01dd5SLinus Torvalds return global_page_state(NR_FILE_DIRTY) + 1239cdf01dd5SLinus Torvalds global_page_state(NR_UNSTABLE_NFS) + 1240cdf01dd5SLinus Torvalds get_nr_dirty_inodes(); 1241cdf01dd5SLinus Torvalds } 1242cdf01dd5SLinus Torvalds 12436585027aSJan Kara static long wb_check_background_flush(struct bdi_writeback *wb) 12446585027aSJan Kara { 1245aa661bbeSTejun Heo if (wb_over_bg_thresh(wb)) { 12466585027aSJan Kara 12476585027aSJan Kara struct wb_writeback_work work = { 12486585027aSJan Kara .nr_pages = LONG_MAX, 12496585027aSJan Kara .sync_mode = WB_SYNC_NONE, 12506585027aSJan Kara .for_background = 1, 12516585027aSJan Kara .range_cyclic = 1, 12520e175a18SCurt Wohlgemuth .reason = WB_REASON_BACKGROUND, 12536585027aSJan Kara }; 12546585027aSJan Kara 12556585027aSJan Kara return wb_writeback(wb, &work); 12566585027aSJan Kara } 12576585027aSJan Kara 12586585027aSJan Kara return 0; 12596585027aSJan Kara } 12606585027aSJan Kara 126103ba3782SJens Axboe static long wb_check_old_data_flush(struct bdi_writeback *wb) 126203ba3782SJens Axboe { 126303ba3782SJens Axboe unsigned long expired; 126403ba3782SJens Axboe long nr_pages; 126503ba3782SJens Axboe 126669b62d01SJens Axboe /* 126769b62d01SJens Axboe * When set to zero, disable periodic writeback 126869b62d01SJens Axboe */ 126969b62d01SJens Axboe if (!dirty_writeback_interval) 127069b62d01SJens Axboe return 0; 127169b62d01SJens Axboe 127203ba3782SJens Axboe expired = wb->last_old_flush + 127303ba3782SJens Axboe msecs_to_jiffies(dirty_writeback_interval * 10); 127403ba3782SJens Axboe if (time_before(jiffies, expired)) 127503ba3782SJens Axboe return 0; 127603ba3782SJens Axboe 127703ba3782SJens Axboe wb->last_old_flush = jiffies; 1278cdf01dd5SLinus Torvalds nr_pages = get_nr_dirty_pages(); 127903ba3782SJens Axboe 1280c4a77a6cSJens Axboe if (nr_pages) { 128183ba7b07SChristoph Hellwig struct wb_writeback_work work = { 1282c4a77a6cSJens Axboe .nr_pages = nr_pages, 1283c4a77a6cSJens Axboe .sync_mode = WB_SYNC_NONE, 1284c4a77a6cSJens Axboe .for_kupdate = 1, 1285c4a77a6cSJens Axboe .range_cyclic = 1, 12860e175a18SCurt Wohlgemuth .reason = WB_REASON_PERIODIC, 1287c4a77a6cSJens Axboe }; 1288c4a77a6cSJens Axboe 128983ba7b07SChristoph Hellwig return wb_writeback(wb, &work); 1290c4a77a6cSJens Axboe } 129103ba3782SJens Axboe 129203ba3782SJens Axboe return 0; 129303ba3782SJens Axboe } 129403ba3782SJens Axboe 129503ba3782SJens Axboe /* 129603ba3782SJens Axboe * Retrieve work items and do the writeback they describe 129703ba3782SJens Axboe */ 129825d130baSWanpeng Li static long wb_do_writeback(struct bdi_writeback *wb) 129903ba3782SJens Axboe { 130083ba7b07SChristoph Hellwig struct wb_writeback_work *work; 1301c4a77a6cSJens Axboe long wrote = 0; 130203ba3782SJens Axboe 13034452226eSTejun Heo set_bit(WB_writeback_running, &wb->state); 1304f0054bb1STejun Heo while ((work = get_next_work_item(wb)) != NULL) { 1305cc395d7fSTejun Heo struct wb_completion *done = work->done; 130698754bf7STejun Heo bool need_wake_up = false; 130783ba7b07SChristoph Hellwig 1308f0054bb1STejun Heo trace_writeback_exec(wb->bdi, work); 1309455b2864SDave Chinner 131083ba7b07SChristoph Hellwig wrote += wb_writeback(wb, work); 131103ba3782SJens Axboe 131298754bf7STejun Heo if (work->single_wait) { 131398754bf7STejun Heo WARN_ON_ONCE(work->auto_free); 131498754bf7STejun Heo /* paired w/ rmb in wb_wait_for_single_work() */ 131598754bf7STejun Heo smp_wmb(); 131698754bf7STejun Heo work->single_done = 1; 131798754bf7STejun Heo need_wake_up = true; 131898754bf7STejun Heo } else if (work->auto_free) { 131983ba7b07SChristoph Hellwig kfree(work); 132098754bf7STejun Heo } 132198754bf7STejun Heo 1322cc395d7fSTejun Heo if (done && atomic_dec_and_test(&done->cnt)) 132398754bf7STejun Heo need_wake_up = true; 132498754bf7STejun Heo 132598754bf7STejun Heo if (need_wake_up) 1326cc395d7fSTejun Heo wake_up_all(&wb->bdi->wb_waitq); 132703ba3782SJens Axboe } 132803ba3782SJens Axboe 132903ba3782SJens Axboe /* 133003ba3782SJens Axboe * Check for periodic writeback, kupdated() style 133103ba3782SJens Axboe */ 133203ba3782SJens Axboe wrote += wb_check_old_data_flush(wb); 13336585027aSJan Kara wrote += wb_check_background_flush(wb); 13344452226eSTejun Heo clear_bit(WB_writeback_running, &wb->state); 133503ba3782SJens Axboe 133603ba3782SJens Axboe return wrote; 133703ba3782SJens Axboe } 133803ba3782SJens Axboe 133903ba3782SJens Axboe /* 134003ba3782SJens Axboe * Handle writeback of dirty data for the device backed by this bdi. Also 1341839a8e86STejun Heo * reschedules periodically and does kupdated style flushing. 134203ba3782SJens Axboe */ 1343f0054bb1STejun Heo void wb_workfn(struct work_struct *work) 134403ba3782SJens Axboe { 1345839a8e86STejun Heo struct bdi_writeback *wb = container_of(to_delayed_work(work), 1346839a8e86STejun Heo struct bdi_writeback, dwork); 134703ba3782SJens Axboe long pages_written; 134803ba3782SJens Axboe 1349f0054bb1STejun Heo set_worker_desc("flush-%s", dev_name(wb->bdi->dev)); 1350766f9164SPeter Zijlstra current->flags |= PF_SWAPWRITE; 135103ba3782SJens Axboe 1352839a8e86STejun Heo if (likely(!current_is_workqueue_rescuer() || 13534452226eSTejun Heo !test_bit(WB_registered, &wb->state))) { 135403ba3782SJens Axboe /* 1355f0054bb1STejun Heo * The normal path. Keep writing back @wb until its 1356839a8e86STejun Heo * work_list is empty. Note that this path is also taken 1357f0054bb1STejun Heo * if @wb is shutting down even when we're running off the 1358839a8e86STejun Heo * rescuer as work_list needs to be drained. 135903ba3782SJens Axboe */ 1360839a8e86STejun Heo do { 136125d130baSWanpeng Li pages_written = wb_do_writeback(wb); 1362455b2864SDave Chinner trace_writeback_pages_written(pages_written); 1363f0054bb1STejun Heo } while (!list_empty(&wb->work_list)); 1364839a8e86STejun Heo } else { 1365253c34e9SArtem Bityutskiy /* 1366839a8e86STejun Heo * bdi_wq can't get enough workers and we're running off 1367839a8e86STejun Heo * the emergency worker. Don't hog it. Hopefully, 1024 is 1368839a8e86STejun Heo * enough for efficient IO. 1369253c34e9SArtem Bityutskiy */ 1370f0054bb1STejun Heo pages_written = writeback_inodes_wb(wb, 1024, 1371839a8e86STejun Heo WB_REASON_FORKER_THREAD); 1372839a8e86STejun Heo trace_writeback_pages_written(pages_written); 137303ba3782SJens Axboe } 137403ba3782SJens Axboe 1375f0054bb1STejun Heo if (!list_empty(&wb->work_list)) 13766ca738d6SDerek Basehore mod_delayed_work(bdi_wq, &wb->dwork, 0); 13776ca738d6SDerek Basehore else if (wb_has_dirty_io(wb) && dirty_writeback_interval) 1378f0054bb1STejun Heo wb_wakeup_delayed(wb); 1379455b2864SDave Chinner 1380839a8e86STejun Heo current->flags &= ~PF_SWAPWRITE; 138103ba3782SJens Axboe } 138203ba3782SJens Axboe 138303ba3782SJens Axboe /* 138403ba3782SJens Axboe * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back 138503ba3782SJens Axboe * the whole world. 138603ba3782SJens Axboe */ 13870e175a18SCurt Wohlgemuth void wakeup_flusher_threads(long nr_pages, enum wb_reason reason) 138803ba3782SJens Axboe { 1389b8c2f347SChristoph Hellwig struct backing_dev_info *bdi; 1390b8c2f347SChristoph Hellwig 139147df3ddeSJan Kara if (!nr_pages) 139247df3ddeSJan Kara nr_pages = get_nr_dirty_pages(); 1393b8c2f347SChristoph Hellwig 1394b8c2f347SChristoph Hellwig rcu_read_lock(); 1395f2b65121STejun Heo list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 1396f2b65121STejun Heo struct bdi_writeback *wb; 1397f2b65121STejun Heo struct wb_iter iter; 1398f2b65121STejun Heo 1399f2b65121STejun Heo if (!bdi_has_dirty_io(bdi)) 1400f2b65121STejun Heo continue; 1401f2b65121STejun Heo 1402f2b65121STejun Heo bdi_for_each_wb(wb, bdi, &iter, 0) 1403f2b65121STejun Heo wb_start_writeback(wb, wb_split_bdi_pages(wb, nr_pages), 1404f2b65121STejun Heo false, reason); 1405f2b65121STejun Heo } 1406b8c2f347SChristoph Hellwig rcu_read_unlock(); 140703ba3782SJens Axboe } 140803ba3782SJens Axboe 1409a2f48706STheodore Ts'o /* 1410a2f48706STheodore Ts'o * Wake up bdi's periodically to make sure dirtytime inodes gets 1411a2f48706STheodore Ts'o * written back periodically. We deliberately do *not* check the 1412a2f48706STheodore Ts'o * b_dirtytime list in wb_has_dirty_io(), since this would cause the 1413a2f48706STheodore Ts'o * kernel to be constantly waking up once there are any dirtytime 1414a2f48706STheodore Ts'o * inodes on the system. So instead we define a separate delayed work 1415a2f48706STheodore Ts'o * function which gets called much more rarely. (By default, only 1416a2f48706STheodore Ts'o * once every 12 hours.) 1417a2f48706STheodore Ts'o * 1418a2f48706STheodore Ts'o * If there is any other write activity going on in the file system, 1419a2f48706STheodore Ts'o * this function won't be necessary. But if the only thing that has 1420a2f48706STheodore Ts'o * happened on the file system is a dirtytime inode caused by an atime 1421a2f48706STheodore Ts'o * update, we need this infrastructure below to make sure that inode 1422a2f48706STheodore Ts'o * eventually gets pushed out to disk. 1423a2f48706STheodore Ts'o */ 1424a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w); 1425a2f48706STheodore Ts'o static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback); 1426a2f48706STheodore Ts'o 1427a2f48706STheodore Ts'o static void wakeup_dirtytime_writeback(struct work_struct *w) 1428a2f48706STheodore Ts'o { 1429a2f48706STheodore Ts'o struct backing_dev_info *bdi; 1430a2f48706STheodore Ts'o 1431a2f48706STheodore Ts'o rcu_read_lock(); 1432a2f48706STheodore Ts'o list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 1433001fe6f6STejun Heo struct bdi_writeback *wb; 1434001fe6f6STejun Heo struct wb_iter iter; 1435001fe6f6STejun Heo 1436001fe6f6STejun Heo bdi_for_each_wb(wb, bdi, &iter, 0) 1437001fe6f6STejun Heo if (!list_empty(&bdi->wb.b_dirty_time)) 1438f0054bb1STejun Heo wb_wakeup(&bdi->wb); 1439a2f48706STheodore Ts'o } 1440a2f48706STheodore Ts'o rcu_read_unlock(); 1441a2f48706STheodore Ts'o schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ); 1442a2f48706STheodore Ts'o } 1443a2f48706STheodore Ts'o 1444a2f48706STheodore Ts'o static int __init start_dirtytime_writeback(void) 1445a2f48706STheodore Ts'o { 1446a2f48706STheodore Ts'o schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ); 1447a2f48706STheodore Ts'o return 0; 1448a2f48706STheodore Ts'o } 1449a2f48706STheodore Ts'o __initcall(start_dirtytime_writeback); 1450a2f48706STheodore Ts'o 14511efff914STheodore Ts'o int dirtytime_interval_handler(struct ctl_table *table, int write, 14521efff914STheodore Ts'o void __user *buffer, size_t *lenp, loff_t *ppos) 14531efff914STheodore Ts'o { 14541efff914STheodore Ts'o int ret; 14551efff914STheodore Ts'o 14561efff914STheodore Ts'o ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 14571efff914STheodore Ts'o if (ret == 0 && write) 14581efff914STheodore Ts'o mod_delayed_work(system_wq, &dirtytime_work, 0); 14591efff914STheodore Ts'o return ret; 14601efff914STheodore Ts'o } 14611efff914STheodore Ts'o 146203ba3782SJens Axboe static noinline void block_dump___mark_inode_dirty(struct inode *inode) 146303ba3782SJens Axboe { 146403ba3782SJens Axboe if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { 146503ba3782SJens Axboe struct dentry *dentry; 146603ba3782SJens Axboe const char *name = "?"; 146703ba3782SJens Axboe 146803ba3782SJens Axboe dentry = d_find_alias(inode); 146903ba3782SJens Axboe if (dentry) { 147003ba3782SJens Axboe spin_lock(&dentry->d_lock); 147103ba3782SJens Axboe name = (const char *) dentry->d_name.name; 147203ba3782SJens Axboe } 147303ba3782SJens Axboe printk(KERN_DEBUG 147403ba3782SJens Axboe "%s(%d): dirtied inode %lu (%s) on %s\n", 147503ba3782SJens Axboe current->comm, task_pid_nr(current), inode->i_ino, 147603ba3782SJens Axboe name, inode->i_sb->s_id); 147703ba3782SJens Axboe if (dentry) { 147803ba3782SJens Axboe spin_unlock(&dentry->d_lock); 147903ba3782SJens Axboe dput(dentry); 148003ba3782SJens Axboe } 148103ba3782SJens Axboe } 148203ba3782SJens Axboe } 148303ba3782SJens Axboe 148403ba3782SJens Axboe /** 148503ba3782SJens Axboe * __mark_inode_dirty - internal function 148603ba3782SJens Axboe * @inode: inode to mark 148703ba3782SJens Axboe * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) 148803ba3782SJens Axboe * Mark an inode as dirty. Callers should use mark_inode_dirty or 148903ba3782SJens Axboe * mark_inode_dirty_sync. 149003ba3782SJens Axboe * 149103ba3782SJens Axboe * Put the inode on the super block's dirty list. 149203ba3782SJens Axboe * 149303ba3782SJens Axboe * CAREFUL! We mark it dirty unconditionally, but move it onto the 149403ba3782SJens Axboe * dirty list only if it is hashed or if it refers to a blockdev. 149503ba3782SJens Axboe * If it was not hashed, it will never be added to the dirty list 149603ba3782SJens Axboe * even if it is later hashed, as it will have been marked dirty already. 149703ba3782SJens Axboe * 149803ba3782SJens Axboe * In short, make sure you hash any inodes _before_ you start marking 149903ba3782SJens Axboe * them dirty. 150003ba3782SJens Axboe * 150103ba3782SJens Axboe * Note that for blockdevs, inode->dirtied_when represents the dirtying time of 150203ba3782SJens Axboe * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of 150303ba3782SJens Axboe * the kernel-internal blockdev inode represents the dirtying time of the 150403ba3782SJens Axboe * blockdev's pages. This is why for I_DIRTY_PAGES we always use 150503ba3782SJens Axboe * page->mapping->host, so the page-dirtying time is recorded in the internal 150603ba3782SJens Axboe * blockdev inode. 150703ba3782SJens Axboe */ 15080ae45f63STheodore Ts'o #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC) 150903ba3782SJens Axboe void __mark_inode_dirty(struct inode *inode, int flags) 151003ba3782SJens Axboe { 151103ba3782SJens Axboe struct super_block *sb = inode->i_sb; 15120ae45f63STheodore Ts'o int dirtytime; 15130ae45f63STheodore Ts'o 15140ae45f63STheodore Ts'o trace_writeback_mark_inode_dirty(inode, flags); 151503ba3782SJens Axboe 151603ba3782SJens Axboe /* 151703ba3782SJens Axboe * Don't do this for I_DIRTY_PAGES - that doesn't actually 151803ba3782SJens Axboe * dirty the inode itself 151903ba3782SJens Axboe */ 15200ae45f63STheodore Ts'o if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_TIME)) { 15219fb0a7daSTejun Heo trace_writeback_dirty_inode_start(inode, flags); 15229fb0a7daSTejun Heo 152303ba3782SJens Axboe if (sb->s_op->dirty_inode) 1524aa385729SChristoph Hellwig sb->s_op->dirty_inode(inode, flags); 15259fb0a7daSTejun Heo 15269fb0a7daSTejun Heo trace_writeback_dirty_inode(inode, flags); 152703ba3782SJens Axboe } 15280ae45f63STheodore Ts'o if (flags & I_DIRTY_INODE) 15290ae45f63STheodore Ts'o flags &= ~I_DIRTY_TIME; 15300ae45f63STheodore Ts'o dirtytime = flags & I_DIRTY_TIME; 153103ba3782SJens Axboe 153203ba3782SJens Axboe /* 15339c6ac78eSTejun Heo * Paired with smp_mb() in __writeback_single_inode() for the 15349c6ac78eSTejun Heo * following lockless i_state test. See there for details. 153503ba3782SJens Axboe */ 153603ba3782SJens Axboe smp_mb(); 153703ba3782SJens Axboe 15380ae45f63STheodore Ts'o if (((inode->i_state & flags) == flags) || 15390ae45f63STheodore Ts'o (dirtytime && (inode->i_state & I_DIRTY_INODE))) 154003ba3782SJens Axboe return; 154103ba3782SJens Axboe 154203ba3782SJens Axboe if (unlikely(block_dump)) 154303ba3782SJens Axboe block_dump___mark_inode_dirty(inode); 154403ba3782SJens Axboe 1545250df6edSDave Chinner spin_lock(&inode->i_lock); 15460ae45f63STheodore Ts'o if (dirtytime && (inode->i_state & I_DIRTY_INODE)) 15470ae45f63STheodore Ts'o goto out_unlock_inode; 154803ba3782SJens Axboe if ((inode->i_state & flags) != flags) { 154903ba3782SJens Axboe const int was_dirty = inode->i_state & I_DIRTY; 155003ba3782SJens Axboe 155152ebea74STejun Heo inode_attach_wb(inode, NULL); 155252ebea74STejun Heo 15530ae45f63STheodore Ts'o if (flags & I_DIRTY_INODE) 15540ae45f63STheodore Ts'o inode->i_state &= ~I_DIRTY_TIME; 155503ba3782SJens Axboe inode->i_state |= flags; 155603ba3782SJens Axboe 155703ba3782SJens Axboe /* 155803ba3782SJens Axboe * If the inode is being synced, just update its dirty state. 155903ba3782SJens Axboe * The unlocker will place the inode on the appropriate 156003ba3782SJens Axboe * superblock list, based upon its state. 156103ba3782SJens Axboe */ 156203ba3782SJens Axboe if (inode->i_state & I_SYNC) 1563250df6edSDave Chinner goto out_unlock_inode; 156403ba3782SJens Axboe 156503ba3782SJens Axboe /* 156603ba3782SJens Axboe * Only add valid (hashed) inodes to the superblock's 156703ba3782SJens Axboe * dirty list. Add blockdev inodes as well. 156803ba3782SJens Axboe */ 156903ba3782SJens Axboe if (!S_ISBLK(inode->i_mode)) { 15701d3382cbSAl Viro if (inode_unhashed(inode)) 1571250df6edSDave Chinner goto out_unlock_inode; 157203ba3782SJens Axboe } 1573a4ffdde6SAl Viro if (inode->i_state & I_FREEING) 1574250df6edSDave Chinner goto out_unlock_inode; 157503ba3782SJens Axboe 157603ba3782SJens Axboe /* 157703ba3782SJens Axboe * If the inode was already on b_dirty/b_io/b_more_io, don't 157803ba3782SJens Axboe * reposition it (that would break b_dirty time-ordering). 157903ba3782SJens Axboe */ 158003ba3782SJens Axboe if (!was_dirty) { 15810747259dSTejun Heo struct bdi_writeback *wb = inode_to_wb(inode); 1582d6c10f1fSTejun Heo struct list_head *dirty_list; 1583a66979abSDave Chinner bool wakeup_bdi = false; 1584500b067cSJens Axboe 1585146d7009SJunxiao Bi spin_unlock(&inode->i_lock); 15860747259dSTejun Heo spin_lock(&wb->list_lock); 1587253c34e9SArtem Bityutskiy 15880747259dSTejun Heo WARN(bdi_cap_writeback_dirty(wb->bdi) && 15890747259dSTejun Heo !test_bit(WB_registered, &wb->state), 15900747259dSTejun Heo "bdi-%s not registered\n", wb->bdi->name); 159103ba3782SJens Axboe 159203ba3782SJens Axboe inode->dirtied_when = jiffies; 1593a2f48706STheodore Ts'o if (dirtytime) 1594a2f48706STheodore Ts'o inode->dirtied_time_when = jiffies; 1595d6c10f1fSTejun Heo 1596a2f48706STheodore Ts'o if (inode->i_state & (I_DIRTY_INODE | I_DIRTY_PAGES)) 15970747259dSTejun Heo dirty_list = &wb->b_dirty; 1598a2f48706STheodore Ts'o else 15990747259dSTejun Heo dirty_list = &wb->b_dirty_time; 1600d6c10f1fSTejun Heo 16010747259dSTejun Heo wakeup_bdi = inode_wb_list_move_locked(inode, wb, 1602d6c10f1fSTejun Heo dirty_list); 1603d6c10f1fSTejun Heo 16040747259dSTejun Heo spin_unlock(&wb->list_lock); 16050ae45f63STheodore Ts'o trace_writeback_dirty_inode_enqueue(inode); 1606253c34e9SArtem Bityutskiy 1607d6c10f1fSTejun Heo /* 1608d6c10f1fSTejun Heo * If this is the first dirty inode for this bdi, 1609d6c10f1fSTejun Heo * we have to wake-up the corresponding bdi thread 1610d6c10f1fSTejun Heo * to make sure background write-back happens 1611d6c10f1fSTejun Heo * later. 1612d6c10f1fSTejun Heo */ 16130747259dSTejun Heo if (bdi_cap_writeback_dirty(wb->bdi) && wakeup_bdi) 16140747259dSTejun Heo wb_wakeup_delayed(wb); 1615a66979abSDave Chinner return; 1616a66979abSDave Chinner } 1617a66979abSDave Chinner } 1618a66979abSDave Chinner out_unlock_inode: 1619a66979abSDave Chinner spin_unlock(&inode->i_lock); 1620a66979abSDave Chinner 162103ba3782SJens Axboe } 162203ba3782SJens Axboe EXPORT_SYMBOL(__mark_inode_dirty); 162303ba3782SJens Axboe 1624b6e51316SJens Axboe static void wait_sb_inodes(struct super_block *sb) 162566f3b8e2SJens Axboe { 162638f21977SNick Piggin struct inode *inode, *old_inode = NULL; 162738f21977SNick Piggin 162803ba3782SJens Axboe /* 162903ba3782SJens Axboe * We need to be protected against the filesystem going from 163003ba3782SJens Axboe * r/o to r/w or vice versa. 163103ba3782SJens Axboe */ 1632b6e51316SJens Axboe WARN_ON(!rwsem_is_locked(&sb->s_umount)); 163303ba3782SJens Axboe 163455fa6091SDave Chinner spin_lock(&inode_sb_list_lock); 163566f3b8e2SJens Axboe 163638f21977SNick Piggin /* 163738f21977SNick Piggin * Data integrity sync. Must wait for all pages under writeback, 163838f21977SNick Piggin * because there may have been pages dirtied before our sync 163938f21977SNick Piggin * call, but which had writeout started before we write it out. 164038f21977SNick Piggin * In which case, the inode may not be on the dirty list, but 164138f21977SNick Piggin * we still have to wait for that writeout. 164238f21977SNick Piggin */ 1643b6e51316SJens Axboe list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 1644250df6edSDave Chinner struct address_space *mapping = inode->i_mapping; 164538f21977SNick Piggin 1646250df6edSDave Chinner spin_lock(&inode->i_lock); 1647250df6edSDave Chinner if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) || 1648250df6edSDave Chinner (mapping->nrpages == 0)) { 1649250df6edSDave Chinner spin_unlock(&inode->i_lock); 165038f21977SNick Piggin continue; 1651250df6edSDave Chinner } 165238f21977SNick Piggin __iget(inode); 1653250df6edSDave Chinner spin_unlock(&inode->i_lock); 165455fa6091SDave Chinner spin_unlock(&inode_sb_list_lock); 165555fa6091SDave Chinner 165638f21977SNick Piggin /* 165755fa6091SDave Chinner * We hold a reference to 'inode' so it couldn't have been 165855fa6091SDave Chinner * removed from s_inodes list while we dropped the 165955fa6091SDave Chinner * inode_sb_list_lock. We cannot iput the inode now as we can 166055fa6091SDave Chinner * be holding the last reference and we cannot iput it under 166155fa6091SDave Chinner * inode_sb_list_lock. So we keep the reference and iput it 166255fa6091SDave Chinner * later. 166338f21977SNick Piggin */ 166438f21977SNick Piggin iput(old_inode); 166538f21977SNick Piggin old_inode = inode; 166638f21977SNick Piggin 166738f21977SNick Piggin filemap_fdatawait(mapping); 166838f21977SNick Piggin 166938f21977SNick Piggin cond_resched(); 167038f21977SNick Piggin 167155fa6091SDave Chinner spin_lock(&inode_sb_list_lock); 167238f21977SNick Piggin } 167355fa6091SDave Chinner spin_unlock(&inode_sb_list_lock); 167438f21977SNick Piggin iput(old_inode); 167566f3b8e2SJens Axboe } 16761da177e4SLinus Torvalds 1677f30a7d0cSTejun Heo static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr, 1678f30a7d0cSTejun Heo enum wb_reason reason, bool skip_if_busy) 16791da177e4SLinus Torvalds { 1680cc395d7fSTejun Heo DEFINE_WB_COMPLETION_ONSTACK(done); 168183ba7b07SChristoph Hellwig struct wb_writeback_work work = { 16823c4d7165SChristoph Hellwig .sb = sb, 16833c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_NONE, 16846e6938b6SWu Fengguang .tagged_writepages = 1, 168583ba7b07SChristoph Hellwig .done = &done, 16863259f8beSChris Mason .nr_pages = nr, 16870e175a18SCurt Wohlgemuth .reason = reason, 16883c4d7165SChristoph Hellwig }; 1689e7972912STejun Heo struct backing_dev_info *bdi = sb->s_bdi; 16900e3c9a22SJens Axboe 1691e7972912STejun Heo if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info) 16926eedc701SJan Kara return; 1693cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 1694f30a7d0cSTejun Heo 1695db125360STejun Heo bdi_split_work_to_wbs(sb->s_bdi, &work, skip_if_busy); 1696cc395d7fSTejun Heo wb_wait_for_completion(bdi, &done); 16971da177e4SLinus Torvalds } 1698f30a7d0cSTejun Heo 1699f30a7d0cSTejun Heo /** 1700f30a7d0cSTejun Heo * writeback_inodes_sb_nr - writeback dirty inodes from given super_block 1701f30a7d0cSTejun Heo * @sb: the superblock 1702f30a7d0cSTejun Heo * @nr: the number of pages to write 1703f30a7d0cSTejun Heo * @reason: reason why some writeback work initiated 1704f30a7d0cSTejun Heo * 1705f30a7d0cSTejun Heo * Start writeback on some inodes on this super_block. No guarantees are made 1706f30a7d0cSTejun Heo * on how many (if any) will be written, and this function does not wait 1707f30a7d0cSTejun Heo * for IO completion of submitted IO. 1708f30a7d0cSTejun Heo */ 1709f30a7d0cSTejun Heo void writeback_inodes_sb_nr(struct super_block *sb, 1710f30a7d0cSTejun Heo unsigned long nr, 1711f30a7d0cSTejun Heo enum wb_reason reason) 1712f30a7d0cSTejun Heo { 1713f30a7d0cSTejun Heo __writeback_inodes_sb_nr(sb, nr, reason, false); 1714f30a7d0cSTejun Heo } 17153259f8beSChris Mason EXPORT_SYMBOL(writeback_inodes_sb_nr); 17163259f8beSChris Mason 17173259f8beSChris Mason /** 17183259f8beSChris Mason * writeback_inodes_sb - writeback dirty inodes from given super_block 17193259f8beSChris Mason * @sb: the superblock 1720786228abSMarcos Paulo de Souza * @reason: reason why some writeback work was initiated 17213259f8beSChris Mason * 17223259f8beSChris Mason * Start writeback on some inodes on this super_block. No guarantees are made 17233259f8beSChris Mason * on how many (if any) will be written, and this function does not wait 17243259f8beSChris Mason * for IO completion of submitted IO. 17253259f8beSChris Mason */ 17260e175a18SCurt Wohlgemuth void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 17273259f8beSChris Mason { 17280e175a18SCurt Wohlgemuth return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); 17293259f8beSChris Mason } 1730d8a8559cSJens Axboe EXPORT_SYMBOL(writeback_inodes_sb); 1731d8a8559cSJens Axboe 1732d8a8559cSJens Axboe /** 173310ee27a0SMiao Xie * try_to_writeback_inodes_sb_nr - try to start writeback if none underway 17343259f8beSChris Mason * @sb: the superblock 17353259f8beSChris Mason * @nr: the number of pages to write 173610ee27a0SMiao Xie * @reason: the reason of writeback 17373259f8beSChris Mason * 173810ee27a0SMiao Xie * Invoke writeback_inodes_sb_nr if no writeback is currently underway. 17393259f8beSChris Mason * Returns 1 if writeback was started, 0 if not. 17403259f8beSChris Mason */ 1741f30a7d0cSTejun Heo bool try_to_writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr, 17420e175a18SCurt Wohlgemuth enum wb_reason reason) 17433259f8beSChris Mason { 174410ee27a0SMiao Xie if (!down_read_trylock(&sb->s_umount)) 1745f30a7d0cSTejun Heo return false; 174610ee27a0SMiao Xie 1747f30a7d0cSTejun Heo __writeback_inodes_sb_nr(sb, nr, reason, true); 17483259f8beSChris Mason up_read(&sb->s_umount); 1749f30a7d0cSTejun Heo return true; 17503259f8beSChris Mason } 175110ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb_nr); 175210ee27a0SMiao Xie 175310ee27a0SMiao Xie /** 175410ee27a0SMiao Xie * try_to_writeback_inodes_sb - try to start writeback if none underway 175510ee27a0SMiao Xie * @sb: the superblock 175610ee27a0SMiao Xie * @reason: reason why some writeback work was initiated 175710ee27a0SMiao Xie * 175810ee27a0SMiao Xie * Implement by try_to_writeback_inodes_sb_nr() 175910ee27a0SMiao Xie * Returns 1 if writeback was started, 0 if not. 176010ee27a0SMiao Xie */ 1761f30a7d0cSTejun Heo bool try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 176210ee27a0SMiao Xie { 176310ee27a0SMiao Xie return try_to_writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); 176410ee27a0SMiao Xie } 176510ee27a0SMiao Xie EXPORT_SYMBOL(try_to_writeback_inodes_sb); 17663259f8beSChris Mason 17673259f8beSChris Mason /** 1768d8a8559cSJens Axboe * sync_inodes_sb - sync sb inode pages 1769d8a8559cSJens Axboe * @sb: the superblock 1770d8a8559cSJens Axboe * 1771d8a8559cSJens Axboe * This function writes and waits on any dirty inode belonging to this 17720dc83bd3SJan Kara * super_block. 1773d8a8559cSJens Axboe */ 17740dc83bd3SJan Kara void sync_inodes_sb(struct super_block *sb) 1775d8a8559cSJens Axboe { 1776cc395d7fSTejun Heo DEFINE_WB_COMPLETION_ONSTACK(done); 177783ba7b07SChristoph Hellwig struct wb_writeback_work work = { 17783c4d7165SChristoph Hellwig .sb = sb, 17793c4d7165SChristoph Hellwig .sync_mode = WB_SYNC_ALL, 17803c4d7165SChristoph Hellwig .nr_pages = LONG_MAX, 17813c4d7165SChristoph Hellwig .range_cyclic = 0, 178283ba7b07SChristoph Hellwig .done = &done, 17830e175a18SCurt Wohlgemuth .reason = WB_REASON_SYNC, 17847747bd4bSDave Chinner .for_sync = 1, 17853c4d7165SChristoph Hellwig }; 1786e7972912STejun Heo struct backing_dev_info *bdi = sb->s_bdi; 17873c4d7165SChristoph Hellwig 17886eedc701SJan Kara /* Nothing to do? */ 1789e7972912STejun Heo if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info) 17906eedc701SJan Kara return; 1791cf37e972SChristoph Hellwig WARN_ON(!rwsem_is_locked(&sb->s_umount)); 1792cf37e972SChristoph Hellwig 1793db125360STejun Heo bdi_split_work_to_wbs(bdi, &work, false); 1794cc395d7fSTejun Heo wb_wait_for_completion(bdi, &done); 179583ba7b07SChristoph Hellwig 1796b6e51316SJens Axboe wait_sb_inodes(sb); 1797d8a8559cSJens Axboe } 1798d8a8559cSJens Axboe EXPORT_SYMBOL(sync_inodes_sb); 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds /** 18011da177e4SLinus Torvalds * write_inode_now - write an inode to disk 18021da177e4SLinus Torvalds * @inode: inode to write to disk 18031da177e4SLinus Torvalds * @sync: whether the write should be synchronous or not 18041da177e4SLinus Torvalds * 18057f04c26dSAndrea Arcangeli * This function commits an inode to disk immediately if it is dirty. This is 18067f04c26dSAndrea Arcangeli * primarily needed by knfsd. 18077f04c26dSAndrea Arcangeli * 18087f04c26dSAndrea Arcangeli * The caller must either have a ref on the inode or must have set I_WILL_FREE. 18091da177e4SLinus Torvalds */ 18101da177e4SLinus Torvalds int write_inode_now(struct inode *inode, int sync) 18111da177e4SLinus Torvalds { 1812f758eeabSChristoph Hellwig struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; 18131da177e4SLinus Torvalds struct writeback_control wbc = { 18141da177e4SLinus Torvalds .nr_to_write = LONG_MAX, 181518914b18SMike Galbraith .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 1816111ebb6eSOGAWA Hirofumi .range_start = 0, 1817111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 18181da177e4SLinus Torvalds }; 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds if (!mapping_cap_writeback_dirty(inode->i_mapping)) 182149364ce2SAndrew Morton wbc.nr_to_write = 0; 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds might_sleep(); 18244f8ad655SJan Kara return writeback_single_inode(inode, wb, &wbc); 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds EXPORT_SYMBOL(write_inode_now); 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds /** 18291da177e4SLinus Torvalds * sync_inode - write an inode and its pages to disk. 18301da177e4SLinus Torvalds * @inode: the inode to sync 18311da177e4SLinus Torvalds * @wbc: controls the writeback mode 18321da177e4SLinus Torvalds * 18331da177e4SLinus Torvalds * sync_inode() will write an inode and its pages to disk. It will also 18341da177e4SLinus Torvalds * correctly update the inode on its superblock's dirty inode lists and will 18351da177e4SLinus Torvalds * update inode->i_state. 18361da177e4SLinus Torvalds * 18371da177e4SLinus Torvalds * The caller must have a ref on the inode. 18381da177e4SLinus Torvalds */ 18391da177e4SLinus Torvalds int sync_inode(struct inode *inode, struct writeback_control *wbc) 18401da177e4SLinus Torvalds { 18414f8ad655SJan Kara return writeback_single_inode(inode, &inode_to_bdi(inode)->wb, wbc); 18421da177e4SLinus Torvalds } 18431da177e4SLinus Torvalds EXPORT_SYMBOL(sync_inode); 1844c3765016SChristoph Hellwig 1845c3765016SChristoph Hellwig /** 1846c691b9d9SAndrew Morton * sync_inode_metadata - write an inode to disk 1847c3765016SChristoph Hellwig * @inode: the inode to sync 1848c3765016SChristoph Hellwig * @wait: wait for I/O to complete. 1849c3765016SChristoph Hellwig * 1850c691b9d9SAndrew Morton * Write an inode to disk and adjust its dirty state after completion. 1851c3765016SChristoph Hellwig * 1852c3765016SChristoph Hellwig * Note: only writes the actual inode, no associated data or other metadata. 1853c3765016SChristoph Hellwig */ 1854c3765016SChristoph Hellwig int sync_inode_metadata(struct inode *inode, int wait) 1855c3765016SChristoph Hellwig { 1856c3765016SChristoph Hellwig struct writeback_control wbc = { 1857c3765016SChristoph Hellwig .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, 1858c3765016SChristoph Hellwig .nr_to_write = 0, /* metadata-only */ 1859c3765016SChristoph Hellwig }; 1860c3765016SChristoph Hellwig 1861c3765016SChristoph Hellwig return sync_inode(inode, &wbc); 1862c3765016SChristoph Hellwig } 1863c3765016SChristoph Hellwig EXPORT_SYMBOL(sync_inode_metadata); 1864