| /* |
| * fs/fs-writeback.c |
| * |
| * Copyright (C) 2002, Linus Torvalds. |
| * |
| * Contains all the functions related to writing back and waiting |
| * upon dirty inodes against superblocks, and writing back dirty |
| * pages against inodes. ie: data writeback. Writeout of the |
| * inode itself is not handled here. |
| * |
| * 10Apr2002 Andrew Morton |
| * Split out of fs/inode.c |
| * Additions for address_space-based writeback |
| */ |
| |
| #include <linux/kernel.h> |
| #include <linux/export.h> |
| #include <linux/spinlock.h> |
| #include <linux/slab.h> |
| #include <linux/sched.h> |
| #include <linux/fs.h> |
| #include <linux/mm.h> |
| #include <linux/pagemap.h> |
| #include <linux/kthread.h> |
| #include <linux/writeback.h> |
| #include <linux/blkdev.h> |
| #include <linux/backing-dev.h> |
| #include <linux/tracepoint.h> |
| #include <linux/device.h> |
| #include "internal.h" |
| |
| /* |
| * 4MB minimal write chunk size |
| */ |
| #define MIN_WRITEBACK_PAGES (4096UL >> (PAGE_CACHE_SHIFT - 10)) |
| |
| struct wb_completion { |
| atomic_t cnt; |
| #ifdef CONFIG_CGROUP_WRITEBACK |
| int mapping_ret; /* used by works w/ ->mapping */ |
| #endif |
| }; |
| |
| /* |
| * Passed into wb_writeback(), essentially a subset of writeback_control |
| */ |
| struct wb_writeback_work { |
| long nr_pages; |
| |
| struct super_block *sb; |
| unsigned long *older_than_this; |
| |
| /* |
| * If ->mapping is set, only that mapping is written out using |
| * do_writepages(). ->mapping_range_{start|end} are meaningful |
| * only in such cases. |
| */ |
| struct address_space *mapping; |
| loff_t mapping_range_start; |
| loff_t mapping_range_end; |
| |
| enum writeback_sync_modes sync_mode; |
| unsigned int tagged_writepages:1; |
| unsigned int for_kupdate:1; |
| unsigned int range_cyclic:1; |
| unsigned int for_background:1; |
| unsigned int for_sync:1; /* sync(2) WB_SYNC_ALL writeback */ |
| unsigned int auto_free:1; /* free on completion */ |
| unsigned int single_wait:1; |
| unsigned int single_done:1; |
| enum wb_reason reason; /* why was writeback initiated? */ |
| |
| struct list_head list; /* pending work list */ |
| struct wb_completion *done; /* set if the caller waits */ |
| }; |
| |
| /* |
| * If one wants to wait for one or more wb_writeback_works, each work's |
| * ->done should be set to a wb_completion defined using the following |
| * macro. Once all work items are issued with wb_queue_work(), the caller |
| * can wait for the completion of all using wb_wait_for_completion(). Work |
| * items which are waited upon aren't freed automatically on completion. |
| */ |
| #define DEFINE_WB_COMPLETION_ONSTACK(cmpl) \ |
| struct wb_completion cmpl = { \ |
| .cnt = ATOMIC_INIT(1), \ |
| } |
| |
| static struct inode_wb_link *dirty_list_to_iwbl(struct list_head *head) |
| { |
| return list_entry(head, struct inode_wb_link, dirty_list); |
| } |
| |
| /* |
| * Include the creation of the trace points after defining the |
| * wb_writeback_work structure and inline functions so that the definition |
| * remains local to this file. |
| */ |
| #define CREATE_TRACE_POINTS |
| #include <trace/events/writeback.h> |
| |
| EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage); |
| |
| static void wb_wakeup(struct bdi_writeback *wb) |
| { |
| spin_lock_bh(&wb->work_lock); |
| if (test_bit(WB_registered, &wb->state)) |
| mod_delayed_work(bdi_wq, &wb->dwork, 0); |
| spin_unlock_bh(&wb->work_lock); |
| } |
| |
| static void wb_queue_work(struct bdi_writeback *wb, |
| struct wb_writeback_work *work) |
| { |
| trace_writeback_queue(wb->bdi, work); |
| |
| spin_lock_bh(&wb->work_lock); |
| if (!test_bit(WB_registered, &wb->state)) { |
| if (work->single_wait) |
| work->single_done = 1; |
| goto out_unlock; |
| } |
| if (work->done) |
| atomic_inc(&work->done->cnt); |
| list_add_tail(&work->list, &wb->work_list); |
| mod_delayed_work(bdi_wq, &wb->dwork, 0); |
| out_unlock: |
| spin_unlock_bh(&wb->work_lock); |
| } |
| |
| /** |
| * wb_wait_for_completion - wait for completion of bdi_writeback_works |
| * @bdi: bdi work items were issued to |
| * @done: target wb_completion |
| * |
| * Wait for one or more work items issued to @bdi with their ->done field |
| * set to @done, which should have been defined with |
| * DEFINE_WB_COMPLETION_ONSTACK(). This function returns after all such |
| * work items are completed. Work items which are waited upon aren't freed |
| * automatically on completion. |
| */ |
| static void wb_wait_for_completion(struct backing_dev_info *bdi, |
| struct wb_completion *done) |
| { |
| atomic_dec(&done->cnt); /* put down the initial count */ |
| wait_event(bdi->wb_waitq, !atomic_read(&done->cnt)); |
| } |
| |
| /** |
| * iwbl_move_locked - move an inode_wb_link onto a bdi_writeback IO list |
| * @iwbl: inode_wb_link to be moved |
| * @wb: target bdi_writeback |
| * @head: one of @wb->b_{dirty|io|more_io} |
| * |
| * Move @iwbl->dirty_list to @list of @wb and set %WB_has_dirty_io. |
| * Returns %true if all IO lists were empty before; otherwise, %false. |
| */ |
| static bool iwbl_move_locked(struct inode_wb_link *iwbl, |
| struct bdi_writeback *wb, struct list_head *head) |
| { |
| assert_spin_locked(&wb->list_lock); |
| |
| list_move(&iwbl->dirty_list, head); |
| |
| if (wb_has_dirty_io(wb)) { |
| return false; |
| } else { |
| set_bit(WB_has_dirty_io, &wb->state); |
| WARN_ON_ONCE(!wb->avg_write_bandwidth); |
| atomic_long_add(wb->avg_write_bandwidth, |
| &wb->bdi->tot_write_bandwidth); |
| return true; |
| } |
| } |
| |
| /** |
| * iwbl_del_locked - remove an inode_wb_link from its bdi_writeback IO list |
| * @iwbl: inode_wb_link to be removed |
| * @wb: bdi_writeback @inode is being removed from |
| * |
| * Remove @iwbl which may be on one of @wb->b_{dirty|io|more_io} lists and |
| * clear %WB_has_dirty_io if all are empty afterwards. |
| */ |
| static void iwbl_del_locked(struct inode_wb_link *iwbl, |
| struct bdi_writeback *wb) |
| { |
| assert_spin_locked(&wb->list_lock); |
| |
| list_del_init(&iwbl->dirty_list); |
| |
| if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) && |
| list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) { |
| clear_bit(WB_has_dirty_io, &wb->state); |
| WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth, |
| &wb->bdi->tot_write_bandwidth) < 0); |
| } |
| } |
| |
| static void iwbl_del(struct inode_wb_link *iwbl) |
| { |
| struct bdi_writeback *wb = iwbl_to_wb(iwbl); |
| |
| if (list_empty(&iwbl->dirty_list)) |
| return; |
| |
| spin_lock(&wb->list_lock); |
| iwbl_del_locked(iwbl, wb); |
| spin_unlock(&wb->list_lock); |
| } |
| |
| /* |
| * Wait for writeback on an inode to complete. Called with i_lock held. |
| * Caller must make sure inode cannot go away when we drop i_lock. |
| */ |
| static void __inode_wait_for_writeback(struct inode *inode) |
| __releases(inode->i_lock) |
| __acquires(inode->i_lock) |
| { |
| DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); |
| wait_queue_head_t *wqh; |
| |
| wqh = bit_waitqueue(&inode->i_state, __I_SYNC); |
| while (inode->i_state & I_SYNC) { |
| spin_unlock(&inode->i_lock); |
| __wait_on_bit(wqh, &wq, bit_wait, TASK_UNINTERRUPTIBLE); |
| spin_lock(&inode->i_lock); |
| } |
| } |
| |
| #ifdef CONFIG_CGROUP_WRITEBACK |
| |
| /** |
| * mapping_congested - test whether a mapping is congested for a task |
| * @mapping: address space to test for congestion |
| * @task: task to test congestion for |
| * @bdi_bits: mask of WB_[a]sync_congested bits to test |
| * |
| * Tests whether @mapping is congested for @task. @bdi_bits is the mask of |
| * congestion bits to test and the return value is the mask of set bits. |
| * |
| * If cgroup writeback is enabled for @mapping, its congestion state for |
| * @task is determined by whether the cgwb (cgroup bdi_writeback) for the |
| * blkcg of %current on @mapping->backing_dev_info is congested; otherwise, |
| * the root's congestion state is used. |
| */ |
| int mapping_congested(struct address_space *mapping, |
| struct task_struct *task, int bdi_bits) |
| { |
| struct backing_dev_info *bdi = mapping->backing_dev_info; |
| struct bdi_writeback *wb; |
| int ret = 0; |
| |
| if (!mapping_cgwb_enabled(mapping)) |
| return wb_congested(&bdi->wb, bdi_bits); |
| |
| rcu_read_lock(); |
| wb = cgwb_lookup(bdi, task_css(task, blkio_cgrp_id)); |
| if (wb) |
| ret = wb_congested(wb, bdi_bits); |
| rcu_read_unlock(); |
| |
| return ret; |
| } |
| EXPORT_SYMBOL_GPL(mapping_congested); |
| |
| /** |
| * init_cgwb_dirty_page_context - init cgwb part of dirty_context |
| * @dctx: dirty_context being initialized |
| * |
| * @dctx is being initialized by init_dirty_page_context(). Initialize |
| * cgroup writeback part of it. |
| */ |
| static void init_cgwb_dirty_page_context(struct dirty_context *dctx) |
| { |
| struct backing_dev_info *bdi = dctx->mapping->backing_dev_info; |
| struct cgroup_subsys_state *blkcg_css; |
| |
| /* cgroup writeback requires support from both the bdi and filesystem */ |
| if (!mapping_cgwb_enabled(dctx->mapping)) |
| goto force_root; |
| |
| /* |
| * @dctx->page is a candidate for cgroup writeback and about to be |
| * dirtied. Attach the dirty blkcg to the page and pre-allocate |
| * all resources necessary for cgroup writeback. On failure, fall |
| * back to the root blkcg. |
| */ |
| blkcg_css = page_blkcg_attach_dirty(dctx->page); |
| |
| /* if iwbl already exists, wb can be determined from that too */ |
| dctx->iwbl = iwbl_lookup(dctx->inode, blkcg_css); |
| if (dctx->iwbl) { |
| dctx->wb = iwbl_to_wb(dctx->iwbl); |
| return; |
| } |
| |
| /* slow path, let's create wb and iwbl */ |
| dctx->wb = cgwb_lookup_create(bdi, blkcg_css); |
| if (!dctx->wb) |
| goto detach_dirty; |
| |
| dctx->iwbl = iwbl_create(dctx->inode, dctx->wb); |
| if (!dctx->iwbl) |
| goto detach_dirty; |
| |
| return; |
| |
| detach_dirty: |
| page_blkcg_detach_dirty(dctx->page); |
| force_root: |
| page_blkcg_force_root_dirty(dctx->page); |
| dctx->wb = &bdi->wb; |
| if (dctx->inode) |
| dctx->iwbl = &dctx->inode->i_wb_link; |
| else |
| dctx->iwbl = NULL; |
| } |
| |
| /** |
| * wb_wait_for_single_work - wait for completion of a single bdi_writeback_work |
| * @bdi: bdi the work item was issued to |
| * @work: work item to wait for |
| * |
| * Wait for the completion of @work which was issued to one of @bdi's |
| * bdi_writeback's. The caller must have set @work->single_wait before |
| * issuing it. This wait operates independently fo |
| * wb_wait_for_completion() and also disables automatic freeing of @work. |
| */ |
| static void wb_wait_for_single_work(struct backing_dev_info *bdi, |
| struct wb_writeback_work *work) |
| { |
| if (WARN_ON_ONCE(!work->single_wait)) |
| return; |
| |
| wait_event(bdi->wb_waitq, work->single_done); |
| |
| /* |
| * Paired with smp_wmb() in wb_do_writeback() and ensures that all |
| * modifications to @work prior to assertion of ->single_done is |
| * visible to the caller once this function returns. |
| */ |
| smp_rmb(); |
| } |
| |
| /** |
| * wb_split_bdi_pages - split nr_pages to write according to bandwidth |
| * @wb: target bdi_writeback to split @nr_pages to |
| * @nr_pages: number of pages to write for the whole bdi |
| * |
| * Split @wb's portion of @nr_pages according to @wb's write bandwidth in |
| * relation to the total write bandwidth of all wb's w/ dirty inodes on |
| * @wb->bdi. |
| */ |
| static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) |
| { |
| unsigned long this_bw = wb->avg_write_bandwidth; |
| unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth); |
| |
| if (nr_pages == LONG_MAX) |
| return LONG_MAX; |
| |
| /* |
| * This may be called on clean wb's and proportional distribution |
| * may not make sense, just use the original @nr_pages in those |
| * cases. In general, we wanna err on the side of writing more. |
| */ |
| if (!tot_bw || this_bw >= tot_bw) |
| return nr_pages; |
| else |
| return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw); |
| } |
| |
| /** |
| * wb_clone_and_queue_work - clone a wb_writeback_work and issue it to a wb |
| * @wb: target bdi_writeback |
| * @base_work: source wb_writeback_work |
| * |
| * Try to make a clone of @base_work and issue it to @wb. If cloning |
| * succeeds, %true is returned; otherwise, @base_work is issued directly |
| * and %false is returned. In the latter case, the caller is required to |
| * wait for @base_work's completion using wb_wait_for_single_work(). |
| * |
| * A clone is auto-freed on completion. @base_work never is. |
| */ |
| static bool wb_clone_and_queue_work(struct bdi_writeback *wb, |
| struct wb_writeback_work *base_work) |
| { |
| struct wb_writeback_work *work; |
| |
| work = kmalloc(sizeof(*work), GFP_ATOMIC); |
| if (work) { |
| *work = *base_work; |
| work->auto_free = 1; |
| work->single_wait = 0; |
| } else { |
| work = base_work; |
| work->auto_free = 0; |
| work->single_wait = 1; |
| } |
| work->single_done = 0; |
| wb_queue_work(wb, work); |
| return work != base_work; |
| } |
| |
| /** |
| * bdi_split_work_to_wbs - split a wb_writeback_work to all wb's of a bdi |
| * @bdi: target backing_dev_info |
| * @base_work: wb_writeback_work to issue |
| * @skip_if_busy: skip wb's which already have writeback in progress |
| * |
| * Split and issue @base_work to all wb's (bdi_writeback's) of @bdi which |
| * have dirty inodes. If @base_work->nr_page isn't %LONG_MAX, it's |
| * distributed to the busy wbs according to each wb's proportion in the |
| * total active write bandwidth of @bdi. |
| */ |
| static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, |
| struct wb_writeback_work *base_work, |
| bool skip_if_busy) |
| { |
| long nr_pages = base_work->nr_pages; |
| int next_blkcg_id = 0; |
| struct bdi_writeback *wb; |
| struct wb_iter iter; |
| |
| might_sleep(); |
| |
| if (!bdi_has_dirty_io(bdi)) |
| return; |
| restart: |
| rcu_read_lock(); |
| bdi_for_each_wb(wb, bdi, &iter, next_blkcg_id) { |
| if (!wb_has_dirty_io(wb) || |
| (skip_if_busy && writeback_in_progress(wb))) |
| continue; |
| |
| base_work->nr_pages = wb_split_bdi_pages(wb, nr_pages); |
| if (!wb_clone_and_queue_work(wb, base_work)) { |
| next_blkcg_id = wb->blkcg_css->id + 1; |
| rcu_read_unlock(); |
| wb_wait_for_single_work(bdi, base_work); |
| goto restart; |
| } |
| } |
| rcu_read_unlock(); |
| } |
| |
| /** |
| * iwbl_test_sync - test whether writeback is in progress on an inode_wb_link |
| * @iwbl: target inode_wb_link |
| * |
| * Test whether writeback is in progress for the inode on the bdi_writeback |
| * specified by @iwbl. The caller is responsible for synchornization. |
| */ |
| static bool iwbl_test_sync(struct inode_wb_link *iwbl) |
| { |
| return test_bit(IWBL_SYNC, &iwbl->data); |
| } |
| |
| /** |
| * iwbl_set_sync - mark an inode_wb_link that writeback is in progress |
| * @iwbl: target inode_wb_link |
| * @inode: inode @iwbl is associated with |
| * |
| * Mark that writeback is in progress for @inode on the bdi_writeback |
| * specified by @iwbl. iwbl_test_sync() will return %true on @iwbl and |
| * %I_SYNC is set on @inode while there's any writeback in progress on it. |
| */ |
| static void iwbl_set_sync(struct inode_wb_link *iwbl, struct inode *inode) |
| { |
| lockdep_assert_held(&inode->i_lock); |
| WARN_ON_ONCE(iwbl_test_sync(iwbl)); |
| |
| set_bit(IWBL_SYNC, &iwbl->data); |
| inode->i_nr_syncs++; |
| inode->i_state |= I_SYNC; |
| } |
| |
| /** |
| * iwbl_clear_sync - undo iwbl_set_sync() |
| * @iwbl: target inode_wb_link |
| * @inode: inode @iwbl is associated with |
| * |
| * Returns %true if this was the last writeback in progress on @inode; |
| * %false, otherwise. |
| */ |
| static bool iwbl_clear_sync(struct inode_wb_link *iwbl, struct inode *inode) |
| { |
| bool sync_complete; |
| |
| lockdep_assert_held(&inode->i_lock); |
| WARN_ON_ONCE(!iwbl_test_sync(iwbl)); |
| |
| clear_bit(IWBL_SYNC, &iwbl->data); |
| sync_complete = !--inode->i_nr_syncs; |
| if (sync_complete) |
| inode->i_state &= ~I_SYNC; |
| return sync_complete; |
| } |
| |
| /** |
| * iwbl_wait_for_writeback - wait for writeback in progree on an inode_wb_link |
| * @iwbl: target inode_wb_link |
| * |
| * Wait for the writeback in progress for the inode on the bdi_writeback |
| * specified by @iwbl. |
| */ |
| static void iwbl_wait_for_writeback(struct inode_wb_link *iwbl) |
| __releases(inode->i_lock) |
| __acquires(inode->i_lock) |
| { |
| struct inode *inode = iwbl_to_inode(iwbl); |
| DEFINE_WAIT_BIT(wq, &iwbl->data, IWBL_SYNC); |
| wait_queue_head_t *wqh; |
| |
| lockdep_assert_held(&inode->i_lock); |
| |
| wqh = bit_waitqueue(&iwbl->data, IWBL_SYNC); |
| while (test_bit(IWBL_SYNC, &iwbl->data)) { |
| spin_unlock(&inode->i_lock); |
| __wait_on_bit(wqh, &wq, bit_wait, TASK_UNINTERRUPTIBLE); |
| spin_lock(&inode->i_lock); |
| } |
| } |
| |
| /* |
| * Sleep until I_SYNC is cleared. This function must be called with i_lock |
| * held and drops it. It is aimed for callers not holding any inode reference |
| * so once i_lock is dropped, inode can go away. |
| */ |
| static void iwbl_sleep_on_writeback(struct inode_wb_link *iwbl) |
| { |
| DEFINE_WAIT(wait); |
| struct inode *inode = iwbl_to_inode(iwbl); |
| wait_queue_head_t *wqh = bit_waitqueue(&iwbl->data, IWBL_SYNC); |
| int sleep; |
| |
| prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); |
| sleep = test_bit(IWBL_SYNC, &iwbl->data); |
| spin_unlock(&inode->i_lock); |
| if (sleep) |
| schedule(); |
| finish_wait(wqh, &wait); |
| } |
| |
| /** |
| * iwbl_sync_wakeup - wakeup iwbl_{wait_for|sleep_on}_writeback() waiter |
| * @iwbl: target inode_wb_link |
| */ |
| static void iwbl_sync_wakeup(struct inode_wb_link *iwbl) |
| { |
| wake_up_bit(&iwbl->data, IWBL_SYNC); |
| } |
| |
| static inline struct inode_cgwb_link *icgwbl_first(struct inode *inode) |
| { |
| struct hlist_node *node = |
| rcu_dereference_check(hlist_first_rcu(&inode->i_cgwb_links), |
| lockdep_is_held(&inode_to_bdi(inode)->icgwbls_lock)); |
| |
| return hlist_entry_safe(node, struct inode_cgwb_link, inode_node); |
| } |
| |
| static inline struct inode_cgwb_link *icgwbl_next(struct inode_cgwb_link *pos, |
| struct inode *inode) |
| { |
| struct hlist_node *node = |
| rcu_dereference_check(hlist_next_rcu(&pos->inode_node), |
| lockdep_is_held(&inode_to_bdi(inode)->icgwbls_lock)); |
| |
| return hlist_entry_safe(node, struct inode_cgwb_link, inode_node); |
| } |
| |
| /** |
| * inode_for_each_icgwbl - walk all icgwbl's of an inode |
| * @cur: cursor struct inode_cgwb_link pointer |
| * @nxt: temp struct inode_cgwb_link pointer |
| * @inode: inode to walk icgwbl's of |
| * |
| * Walk @inode's icgwbl's (inode_cgwb_link's). rcu_read_lock() must be |
| * held throughout iteration. |
| */ |
| #define inode_for_each_icgwbl(cur, nxt, inode) \ |
| for ((cur) = icgwbl_first((inode)), \ |
| (nxt) = (cur) ? icgwbl_next((cur), (inode)) : NULL; \ |
| (cur); \ |
| (cur) = (nxt), \ |
| (nxt) = (nxt) ? icgwbl_next((nxt), (inode)) : NULL) |
| |
| static void inode_icgwbls_del(struct inode *inode) |
| { |
| LIST_HEAD(to_free); |
| struct backing_dev_info *bdi = inode_to_bdi(inode); |
| struct inode_cgwb_link *icgwbl, *next; |
| unsigned long flags; |
| |
| spin_lock_irqsave(&bdi->icgwbls_lock, flags); |
| |
| /* I_FREEING must be set here to disallow further iwbl_create() */ |
| WARN_ON_ONCE(!(inode->i_state & I_FREEING)); |
| |
| /* |
| * We don't wanna nest wb->list_lock under bdi->icgwbls_lock as the |
| * latter is irq-safe and the former isn't. Queue icgwbls on |
| * @to_free and perform iwbl_del() and freeing after releasing |
| * bdi->icgwbls_lock. |
| */ |
| inode_for_each_icgwbl(icgwbl, next, inode) { |
| WARN_ON_ONCE(test_bit(IWBL_SYNC, &icgwbl->iwbl.data)); |
| hlist_del_rcu(&icgwbl->inode_node); |
| list_move(&icgwbl->wb_node, &to_free); |
| } |
| |
| spin_unlock_irqrestore(&bdi->icgwbls_lock, flags); |
| |
| list_for_each_entry_safe(icgwbl, next, &to_free, wb_node) { |
| iwbl_del(&icgwbl->iwbl); |
| kfree_rcu(icgwbl, rcu); |
| } |
| } |
| |
| /** |
| * wbc_set_iwbl - associate a wbc with an iwbl |
| * @wbc: target writeback_control |
| * @iwbl: inode_wb_link to associate @wbc with |
| * |
| * Writeback for @iwbl is about to be performed with @wbc as the control |
| * structure. Associate @wbc with @iwbl so that writeback implementation |
| * can retrieve @iwbl from @wbc. |
| */ |
| static inline void wbc_set_iwbl(struct writeback_control *wbc, |
| struct inode_wb_link *iwbl) |
| { |
| wbc->iwbl = iwbl; |
| } |
| |
| /** |
| * iwbl_has_enough_dirty - does an iwbl and its inode have dirty bits already? |
| * @iwbl: inode_wb_link of interest |
| * @inode: inode @iwbl belongs to |
| * @dirty: I_DIRTY_* bits to be set |
| * |
| * @inode is being dirtied with @dirty by @iwbl's cgroup, test whether |
| * @iwbl and @inode already have all the dirty bits set. Each iwbl has |
| * separate %IWBL_DIRTY_PAGES bit which should be also set if @dirty has |
| * %I_DIRTY_PAGES. |
| */ |
| static inline bool iwbl_has_enough_dirty(struct inode_wb_link *iwbl, |
| struct inode *inode, int dirty) |
| { |
| return (inode->i_state & dirty) == dirty && |
| (!(dirty & I_DIRTY_PAGES) || |
| test_bit(IWBL_DIRTY_PAGES, &iwbl->data)); |
| } |
| |
| /** |
| * iwbl_set_dirty - set dirty bits on an iwbl and its inode |
| * @iwbl: ionde_wb_link of interest |
| * @inode: inode @iwbl belongs to |
| * @dirty: I_DIRTY_* bits to be set |
| * |
| * Set @dirty on @iwbl and @inode and return whether @iwbl was already |
| * dirty. @iwbl only carries the data dirty bit through %IWBL_DIRTY_PAGES. |
| */ |
| static inline bool iwbl_set_dirty(struct inode_wb_link *iwbl, |
| struct inode *inode, int dirty) |
| { |
| bool was_dirty = test_bit(IWBL_DIRTY_PAGES, &iwbl->data); |
| |
| /* metadata dirty bit is always attributed to the root */ |
| if (iwbl_is_root(iwbl)) |
| was_dirty |= inode->i_state & (I_DIRTY_SYNC | I_DIRTY_DATASYNC); |
| |
| inode->i_state |= dirty; |
| if (dirty & I_DIRTY_PAGES) |
| set_bit(IWBL_DIRTY_PAGES, &iwbl->data); |
| return was_dirty; |
| } |
| |
| /** |
| * iwbl_still_has_dirty_pages - does an iwbl have dirty pages after writeback? |
| * @iwbl: inode_wb_link of interest |
| * @inode: inode @iwbl belongs to |
| * |
| * Called from requeue_inode() after writing back @inode for @iwbl to |
| * determine whether @iwbl still has dirty pages and should thus be |
| * requeued. This function can update IWBL_DIRTY_PAGES and may also |
| * spuriously return true. |
| * |
| * See IWBL_DIRTY_PAGES definition for more info. |
| */ |
| static inline bool iwbl_still_has_dirty_pages(struct inode_wb_link *iwbl, |
| struct inode *inode) |
| { |
| if (!mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) { |
| clear_bit(IWBL_DIRTY_PAGES, &iwbl->data); |
| return false; |
| } |
| return test_bit(IWBL_DIRTY_PAGES, &iwbl->data); |
| } |
| |
| /** |
| * wbc_skip_metadata - determine whether to skip writing out metadata |
| * @wbc: writeback_control in effect |
| * |
| * Called by __writeback_single_inode() to decide whether to skip writing |
| * out metadata. Metadata is always dirtied against the root cgroup and |
| * should only be written out by the root. |
| */ |
| static inline bool wbc_skip_metadata(struct writeback_control *wbc) |
| { |
| return wbc->iwbl && !iwbl_is_root(wbc->iwbl); |
| } |
| |
| static bool cgwb_do_writepages_split_work(struct inode_wb_link *iwbl, |
| struct wb_writeback_work *base_work) |
| { |
| struct bdi_writeback *wb = iwbl_to_wb(iwbl); |
| |
| /* if DIRTY_PAGES isn't visible yet, neither is the dirty data */ |
| if (!test_bit(IWBL_DIRTY_PAGES, &iwbl->data)) |
| return true; |
| |
| return wb_clone_and_queue_work(wb, base_work); |
| } |
| |
| /** |
| * cgwb_do_writepages - cgroup-aware do_writepages() |
| * @mapping: address_space to write out |
| * @wbc: writeback_control in effect |
| * |
| * Write out pages from @mapping according to @wbc. This function expects |
| * @mapping to be a file backed one. If cgroup writeback is enabled, the |
| * writes are distributed across the cgroups which dirtied the pages; |
| * otherwise, this is equivalent to do_writepages(). Returns 0 on success, |
| * -errno on failre. |
| */ |
| int cgwb_do_writepages(struct address_space *mapping, |
| struct writeback_control *wbc) |
| { |
| DEFINE_WB_COMPLETION_ONSTACK(done); |
| struct inode *inode = mapping->host; |
| struct backing_dev_info *bdi = inode_to_bdi(inode); |
| struct wb_writeback_work base_work = { |
| .mapping = mapping, |
| .mapping_range_start = wbc->range_start, |
| .mapping_range_end = wbc->range_end, |
| .nr_pages = wbc->nr_to_write, |
| .sync_mode = wbc->sync_mode, |
| .tagged_writepages = wbc->tagged_writepages, |
| .for_kupdate = wbc->for_kupdate, |
| .range_cyclic = wbc->range_cyclic, |
| .for_background = wbc->for_background, |
| .for_sync = wbc->for_sync, |
| .done = &done, |
| }; |
| struct cgroup_subsys_state *blkcg_css; |
| struct inode_wb_link *iwbl; |
| struct inode_cgwb_link *icgwbl, *n; |
| int last_blkcg_id = 0, ret; |
| |
| /* |
| * The caller is likely flushing the pages it dirtied. First look |
| * up the current iwbl and perform do_writepages() directly on it. |
| * If no page is skipped due to mismatching cgroup, there's nothing |
| * more to do. |
| */ |
| blkcg_css = task_get_css(current, blkio_cgrp_id); |
| iwbl = iwbl_lookup(inode, blkcg_css); |
| if (iwbl) { |
| wbc_set_iwbl(wbc, iwbl); |
| wbc->iwbl_mismatch = 0; |
| |
| ret = do_writepages(mapping, wbc); |
| |
| css_put(blkcg_css); |
| if (ret || !wbc->iwbl_mismatch) |
| return ret; |
| } else { |
| css_put(blkcg_css); |
| } |
| |
| /* |
| * Split writes to all dirty iwbl's. We don't yet implement |
| * bandwidth-proportional distribution of nr_pages as the only |
| * current caller, __filemap_fdatawrite_range(), always sets it to |
| * LONG_MAX. Implementing proportional distribution would require |
| * a prepatory pass over dirty iwbl's to calculate the total write |
| * bandwidth of the involved wb's. |
| */ |
| WARN_ON_ONCE(base_work.nr_pages != LONG_MAX); |
| |
| if (!cgwb_do_writepages_split_work(&inode->i_wb_link, &base_work)) |
| wb_wait_for_single_work(bdi, &base_work); |
| restart_split: |
| rcu_read_lock(); |
| inode_for_each_icgwbl(icgwbl, n, inode) { |
| struct inode_wb_link *iwbl = &icgwbl->iwbl; |
| int blkcg_id = iwbl_to_wb(iwbl)->blkcg_css->id; |
| |
| if (blkcg_id <= last_blkcg_id) |
| continue; |
| |
| if (!cgwb_do_writepages_split_work(iwbl, &base_work)) { |
| rcu_read_unlock(); |
| wb_wait_for_single_work(bdi, &base_work); |
| goto restart_split; |
| } |
| last_blkcg_id = blkcg_id; |
| } |
| rcu_read_unlock(); |
| |
| wb_wait_for_completion(bdi, &done); |
| return done.mapping_ret; |
| } |
| |
| static bool maybe_writeback_single_mapping(struct wb_writeback_work *work) |
| { |
| struct wb_completion *done = work->done; |
| struct writeback_control wbc = { |
| .range_start = work->mapping_range_start, |
| .range_end = work->mapping_range_end, |
| .nr_to_write = work->nr_pages, |
| .sync_mode = work->sync_mode, |
| .tagged_writepages = work->tagged_writepages, |
| .for_kupdate = work->for_kupdate, |
| .range_cyclic = work->range_cyclic, |
| .for_background = work->for_background, |
| .for_sync = work->for_sync, |
| }; |
| int ret; |
| |
| if (!work->mapping) |
| return false; |
| |
| ret = do_writepages(work->mapping, &wbc); |
| if (done && ret) |
| done->mapping_ret = ret; |
| return true; |
| } |
| |
| #else /* CONFIG_CGROUP_WRITEBACK */ |
| |
| static void init_cgwb_dirty_page_context(struct dirty_context *dctx) |
| { |
| dctx->wb = &dctx->mapping->backing_dev_info->wb; |
| dctx->iwbl = dctx->inode ? &dctx->inode->i_wb_link : NULL; |
| } |
| |
| static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) |
| { |
| return nr_pages; |
| } |
| |
| static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, |
| struct wb_writeback_work *base_work, |
| bool skip_if_busy) |
| { |
| might_sleep(); |
| |
| if (bdi_has_dirty_io(bdi) && |
| (!skip_if_busy || !writeback_in_progress(&bdi->wb))) { |
| base_work->auto_free = 0; |
| base_work->single_wait = 0; |
| base_work->single_done = 0; |
| wb_queue_work(&bdi->wb, base_work); |
| } |
| } |
| |
| static bool iwbl_test_sync(struct inode_wb_link *iwbl) |
| { |
| struct inode *inode = iwbl_to_inode(iwbl); |
| |
| return inode->i_state & I_SYNC; |
| } |
| |
| static void iwbl_set_sync(struct inode_wb_link *iwbl, struct inode *inode) |
| { |
| inode->i_state |= I_SYNC; |
| } |
| |
| static bool iwbl_clear_sync(struct inode_wb_link *iwbl, struct inode *inode) |
| { |
| inode->i_state &= ~I_SYNC; |
| return true; |
| } |
| |
| static void iwbl_wait_for_writeback(struct inode_wb_link *iwbl) |
| { |
| __inode_wait_for_writeback(iwbl_to_inode(iwbl)); |
| } |
| |
| /* |
| * Sleep until I_SYNC is cleared. This function must be called with i_lock |
| * held and drops it. It is aimed for callers not holding any inode reference |
| * so once i_lock is dropped, inode can go away. |
| */ |
| static void iwbl_sleep_on_writeback(struct inode_wb_link *iwbl) |
| __releases(inode->i_lock) |
| { |
| DEFINE_WAIT(wait); |
| struct inode *inode = iwbl_to_inode(iwbl); |
| wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC); |
| int sleep; |
| |
| prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); |
| sleep = inode->i_state & I_SYNC; |
| spin_unlock(&inode->i_lock); |
| if (sleep) |
| schedule(); |
| finish_wait(wqh, &wait); |
| } |
| |
| static void iwbl_sync_wakeup(struct inode_wb_link *iwbl) |
| { |
| /* noop, __I_SYNC wakeup is enough */ |
| } |
| |
| static void inode_icgwbls_del(struct inode *inode) |
| { |
| } |
| |
| static inline void wbc_set_iwbl(struct writeback_control *wbc, |
| struct inode_wb_link *iwbl) |
| { |
| } |
| |
| static inline bool iwbl_has_enough_dirty(struct inode_wb_link *iwbl, |
| struct inode *inode, int dirty) |
| { |
| return (inode->i_state & dirty) == dirty; |
| } |
| |
| static inline bool iwbl_set_dirty(struct inode_wb_link *iwbl, |
| struct inode *inode, int dirty) |
| { |
| bool was_dirty = inode->i_state & I_DIRTY; |
| |
| inode->i_state |= dirty; |
| return was_dirty; |
| } |
| |
| static inline bool iwbl_still_has_dirty_pages(struct inode_wb_link *iwbl, |
| struct inode *inode) |
| { |
| return mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY); |
| } |
| |
| static inline bool wbc_skip_metadata(struct writeback_control *wbc) |
| { |
| return false; |
| } |
| |
| static bool maybe_writeback_single_mapping(struct wb_writeback_work *work) |
| { |
| return false; |
| } |
| |
| #endif /* CONFIG_CGROUP_WRITEBACK */ |
| |
| /** |
| * init_dirty_page_context - init dirty_context for page dirtying |
| * @dctx: dirty_context to initialize |
| * @page: page to be dirtied |
| * |
| * @page is about to be dirtied, prepare @dctx accordingly. Must be called |
| * with @mapping->tree_lock held. The inode dirtying due to @page dirtying |
| * should use the same @dctx. |
| * |
| * @mapping may have been obtained before the lock was acquired and |
| * @dctx->mapping can be set to NULL even if @mapping isn't if truncate |
| * took place in-between. @dctx->inode is always set to @mapping->inode. |
| */ |
| void init_dirty_page_context(struct dirty_context *dctx, struct page *page, |
| struct address_space *mapping) |
| { |
| lockdep_assert_held(&mapping->tree_lock); |
| |
| dctx->page = page; |
| dctx->inode = mapping->host; |
| dctx->mapping = page_mapping(page); |
| |
| BUG_ON(dctx->mapping != mapping); |
| |
| init_cgwb_dirty_page_context(dctx); |
| } |
| EXPORT_SYMBOL_GPL(init_dirty_page_context); |
| |
| /** |
| * init_dirty_inode_context - init dirty_context for inode dirtying |
| * @dctx: dirty_context to initialize |
| * @inode: inode to be dirtied |
| * |
| * @inode is about to be dirtied w/o a page belonging to it being dirtied, |
| * prepare @dctx accordingly. |
| */ |
| void init_dirty_inode_context(struct dirty_context *dctx, struct inode *inode) |
| { |
| memset(dctx, 0, sizeof(*dctx)); |
| dctx->inode = inode; |
| dctx->wb = &inode_to_bdi(inode)->wb; |
| dctx->iwbl = &inode->i_wb_link; |
| } |
| |
| void wb_start_writeback(struct bdi_writeback *wb, long nr_pages, |
| bool range_cyclic, enum wb_reason reason) |
| { |
| struct wb_writeback_work *work; |
| |
| if (!wb_has_dirty_io(wb)) |
| return; |
| |
| /* |
| * This is WB_SYNC_NONE writeback, so if allocation fails just |
| * wakeup the thread for old dirty data writeback |
| */ |
| work = kzalloc(sizeof(*work), GFP_ATOMIC); |
| if (!work) { |
| trace_writeback_nowork(wb->bdi); |
| wb_wakeup(wb); |
| return; |
| } |
| |
| work->sync_mode = WB_SYNC_NONE; |
| work->nr_pages = nr_pages; |
| work->range_cyclic = range_cyclic; |
| work->reason = reason; |
| work->auto_free = 1; |
| |
| wb_queue_work(wb, work); |
| } |
| |
| /** |
| * wb_start_background_writeback - start background writeback |
| * @wb: bdi_writback to write from |
| * |
| * Description: |
| * This makes sure WB_SYNC_NONE background writeback happens. When |
| * this function returns, it is only guaranteed that for given wb |
| * some IO is happening if we are over background dirty threshold. |
| * Caller need not hold sb s_umount semaphore. |
| */ |
| void wb_start_background_writeback(struct bdi_writeback *wb) |
| { |
| /* |
| * We just wake up the flusher thread. It will perform background |
| * writeback as soon as there is no other work to do. |
| */ |
| trace_writeback_wake_background(wb->bdi); |
| wb_wakeup(wb); |
| } |
| |
| /* |
| * Remove the inode from the writeback list it is on. |
| */ |
| void inode_wb_list_del(struct inode *inode) |
| { |
| iwbl_del(&inode->i_wb_link); |
| inode_icgwbls_del(inode); |
| } |
| |
| /* |
| * Redirty an inode: set its when-it-was dirtied timestamp and move it to the |
| * furthest end of its superblock's dirty-inode list. |
| * |
| * Before stamping the iwbl's ->dirtied_when, we check to see whether it is |
| * already the most-recently-dirtied inode on the b_dirty list. If that is |
| * the case then the inode must have been redirtied while it was being written |
| * out and we don't reset its dirtied_when. |
| */ |
| static void redirty_tail(struct inode_wb_link *iwbl, struct bdi_writeback *wb) |
| { |
| if (!list_empty(&wb->b_dirty)) { |
| struct inode_wb_link *tail; |
| |
| tail = dirty_list_to_iwbl(wb->b_dirty.next); |
| if (time_before(iwbl->dirtied_when, tail->dirtied_when)) |
| iwbl->dirtied_when = jiffies; |
| } |
| iwbl_move_locked(iwbl, wb, &wb->b_dirty); |
| } |
| |
| /* |
| * requeue inode for re-scanning after bdi->b_io list is exhausted. |
| */ |
| static void requeue_io(struct inode_wb_link *iwbl, struct bdi_writeback *wb) |
| { |
| iwbl_move_locked(iwbl, wb, &wb->b_more_io); |
| } |
| |
| static void iwbl_sync_complete(struct inode_wb_link *iwbl) |
| { |
| struct inode *inode = iwbl_to_inode(iwbl); |
| bool sync_complete; |
| |
| sync_complete = iwbl_clear_sync(iwbl, inode); |
| /* If inode is clean an unused, put it into LRU now... */ |
| if (sync_complete) |
| inode_add_lru(inode); |
| |
| /* Waiters must see I_SYNC cleared before being woken up */ |
| smp_mb(); |
| |
| iwbl_sync_wakeup(iwbl); |
| if (sync_complete) |
| wake_up_bit(&inode->i_state, __I_SYNC); |
| } |
| |
| static bool iwbl_dirtied_after(struct inode_wb_link *iwbl, unsigned long t) |
| { |
| bool ret = time_after(iwbl->dirtied_when, t); |
| #ifndef CONFIG_64BIT |
| /* |
| * For inodes being constantly redirtied, dirtied_when can get stuck. |
| * It _appears_ to be in the future, but is actually in distant past. |
| * This test is necessary to prevent such wrapped-around relative times |
| * from permanently stopping the whole bdi writeback. |
| */ |
| ret = ret && time_before_eq(iwbl->dirtied_when, jiffies); |
| #endif |
| return ret; |
| } |
| |
| /* |
| * Move expired (dirtied before work->older_than_this) dirty inodes from |
| * @delaying_queue to @dispatch_queue. |
| */ |
| static int move_expired_inodes(struct list_head *delaying_queue, |
| struct list_head *dispatch_queue, |
| struct wb_writeback_work *work) |
| { |
| LIST_HEAD(tmp); |
| struct list_head *pos, *node; |
| struct super_block *sb = NULL; |
| struct inode_wb_link *iwbl; |
| struct inode *inode; |
| int do_sb_sort = 0; |
| int moved = 0; |
| |
| while (!list_empty(delaying_queue)) { |
| iwbl = dirty_list_to_iwbl(delaying_queue->prev); |
| inode = iwbl_to_inode(iwbl); |
| |
| if (work->older_than_this && |
| iwbl_dirtied_after(iwbl, *work->older_than_this)) |
| break; |
| list_move(&iwbl->dirty_list, &tmp); |
| moved++; |
| if (sb_is_blkdev_sb(inode->i_sb)) |
| continue; |
| if (sb && sb != inode->i_sb) |
| do_sb_sort = 1; |
| sb = inode->i_sb; |
| } |
| |
| /* just one sb in list, splice to dispatch_queue and we're done */ |
| if (!do_sb_sort) { |
| list_splice(&tmp, dispatch_queue); |
| goto out; |
| } |
| |
| /* Move inodes from one superblock together */ |
| while (!list_empty(&tmp)) { |
| sb = iwbl_to_inode(dirty_list_to_iwbl(tmp.prev))->i_sb; |
| list_for_each_prev_safe(pos, node, &tmp) { |
| iwbl = dirty_list_to_iwbl(pos); |
| inode = iwbl_to_inode(iwbl); |
| if (inode->i_sb == sb) |
| list_move(&iwbl->dirty_list, dispatch_queue); |
| } |
| } |
| out: |
| return moved; |
| } |
| |
| /* |
| * Queue all expired dirty inodes for io, eldest first. |
| * Before |
| * newly dirtied b_dirty b_io b_more_io |
| * =============> gf edc BA |
| * After |
| * newly dirtied b_dirty b_io b_more_io |
| * =============> g fBAedc |
| * | |
| * +--> dequeue for IO |
| */ |
| static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work) |
| { |
| int moved; |
| assert_spin_locked(&wb->list_lock); |
| list_splice_init(&wb->b_more_io, &wb->b_io); |
| moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, work); |
| trace_writeback_queue_io(wb, work, moved); |
| } |
| |
| static int write_inode(struct inode *inode, struct writeback_control *wbc) |
| { |
| int ret; |
| |
| if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) { |
| trace_writeback_write_inode_start(inode, wbc); |
| ret = inode->i_sb->s_op->write_inode(inode, wbc); |
| trace_writeback_write_inode(inode, wbc); |
| return ret; |
| } |
| return 0; |
| } |
| |
| /* |
| * Wait for writeback on an inode to complete. Caller must have inode pinned. |
| */ |
| void inode_wait_for_writeback(struct inode *inode) |
| { |
| spin_lock(&inode->i_lock); |
| __inode_wait_for_writeback(inode); |
| spin_unlock(&inode->i_lock); |
| } |
| |
| /* |
| * Find proper writeback list for the inode depending on its current state and |
| * possibly also change of its state while we were doing writeback. Here we |
| * handle things such as livelock prevention or fairness of writeback among |
| * inodes. This function can be called only by flusher thread - noone else |
| * processes all inodes in writeback lists and requeueing inodes behind flusher |
| * thread's back can have unexpected consequences. |
| */ |
| static void requeue_inode(struct inode_wb_link *iwbl, struct bdi_writeback *wb, |
| struct writeback_control *wbc) |
| { |
| struct inode *inode = iwbl_to_inode(iwbl); |
| |
| if (inode->i_state & I_FREEING) |
| return; |
| |
| /* |
| * Sync livelock prevention. Each inode is tagged and synced in one |
| * shot. If still dirty, it will be redirty_tail()'ed below. Update |
| * the dirty time to prevent enqueue and sync it again. |
| */ |
| if ((inode->i_state & I_DIRTY) && |
| (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)) |
| iwbl->dirtied_when = jiffies; |
| |
| if (wbc->pages_skipped) { |
| /* |
| * writeback is not making progress due to locked |
| * buffers. Skip this inode for now. |
| */ |
| redirty_tail(iwbl, wb); |
| return; |
| } |
| |
| if (iwbl_still_has_dirty_pages(iwbl, inode)) { |
| /* |
| * We didn't write back all the pages. nfs_writepages() |
| * sometimes bales out without doing anything. |
| */ |
| if (wbc->nr_to_write <= 0) { |
| /* Slice used up. Queue for next turn. */ |
| requeue_io(iwbl, wb); |
| } else { |
| /* |
| * Writeback blocked by something other than |
| * congestion. Delay the inode for some time to |
| * avoid spinning on the CPU (100% iowait) |
| * retrying writeback of the dirty page/inode |
| * that cannot be performed immediately. |
| */ |
| redirty_tail(iwbl, wb); |
| } |
| } else if (iwbl_is_root(iwbl) && |
| (inode->i_state & (I_DIRTY_SYNC | I_DIRTY_DATASYNC))) { |
| /* |
| * Filesystems can dirty the inode during writeback operations, |
| * such as delayed allocation during submission or metadata |
| * updates after data IO completion. |
| */ |
| redirty_tail(iwbl, wb); |
| } else { |
| /* The inode is clean. Remove from writeback lists. */ |
| iwbl_del_locked(iwbl, wb); |
| } |
| } |
| |
| /* |
| * Write out an inode and its dirty pages. Do not update the writeback list |
| * linkage. That is left to the caller. The caller is also responsible for |
| * setting I_SYNC flag and calling iwbl_sync_complete() to clear it. |
| */ |
| static int |
| __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) |
| { |
| struct address_space *mapping = inode->i_mapping; |
| struct inode_wb_link *iwbl = inode_writeback_iwbl(inode, wbc); |
| long nr_to_write = wbc->nr_to_write; |
| bool skip_metadata = wbc_skip_metadata(wbc); |
| unsigned dirty; |
| int ret; |
| |
| WARN_ON(!iwbl_test_sync(iwbl)); |
| |
| trace_writeback_single_inode_start(inode, wbc, nr_to_write); |
| |
| ret = do_writepages(mapping, wbc); |
| |
| /* |
| * Make sure to wait on the data before writing out the metadata. |
| * This is important for filesystems that modify metadata on data |
| * I/O completion. We don't do it for sync(2) writeback because it has a |
| * separate, external IO completion path and ->sync_fs for guaranteeing |
| * inode metadata is written back correctly. |
| */ |
| if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync && !skip_metadata) { |
| int err = filemap_fdatawait(mapping); |
| if (ret == 0) |
| ret = err; |
| } |
| |
| /* |
| * Some filesystems may redirty the inode during the writeback |
| * due to delalloc, clear dirty metadata flags right before |
| * write_inode() |
| */ |
| spin_lock(&inode->i_lock); |
| |
| if (skip_metadata) |
| dirty = inode->i_state & I_DIRTY_PAGES; |
| else |
| dirty = inode->i_state & I_DIRTY; |
| |
| inode->i_state &= ~dirty; |
| |
| /* |
| * Paired with smp_mb() in __mark_inode_dirty_dctx(). This allows |
| * the function to perform iwbl_has_enough_dirty() test without |
| * grabbing i_lock - either they see the dirty bits cleared or we |
| * see the dirtied inode. |
| * |
| * I_DIRTY_PAGES is always cleared together above even if @mapping |
| * still has dirty pages. The flag is reinstated after smp_mb() if |
| * necessary to guarantee that either __mark_inode_dirty_dctx() |
| * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY. |
| */ |
| smp_mb(); |
| |
| if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
| inode->i_state |= I_DIRTY_PAGES; |
| |
| spin_unlock(&inode->i_lock); |
| |
| /* Don't write the inode if only I_DIRTY_PAGES was set */ |
| if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { |
| int err = write_inode(inode, wbc); |
| if (ret == 0) |
| ret = err; |
| } |
| trace_writeback_single_inode(inode, wbc, nr_to_write); |
| return ret; |
| } |
| |
| /* |
| * Write out an inode's dirty pages. Either the caller has an active reference |
| * on the inode or the inode has I_WILL_FREE set. |
| * |
| * This function is designed to be called for writing back one inode which |
| * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode() |
| * and does more profound writeback list handling in writeback_sb_inodes(). |
| */ |
| static int |
| writeback_single_inode(struct inode *inode, struct writeback_control *wbc) |
| { |
| struct inode_wb_link *iwbl = inode_writeback_iwbl(inode, wbc); |
| struct bdi_writeback *wb = iwbl_to_wb(iwbl); |
| int ret = 0; |
| |
| spin_lock(&inode->i_lock); |
| if (!atomic_read(&inode->i_count)) |
| WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); |
| else |
| WARN_ON(inode->i_state & I_WILL_FREE); |
| |
| if (iwbl_test_sync(iwbl)) { |
| if (wbc->sync_mode != WB_SYNC_ALL) |
| goto out; |
| /* |
| * It's a data-integrity sync. We must wait. Since callers hold |
| * inode reference or inode has I_WILL_FREE set, it cannot go |
| * away under us. |
| */ |
| iwbl_wait_for_writeback(iwbl); |
| } |
| WARN_ON(iwbl_test_sync(iwbl)); |
| /* |
| * Skip inode if it is clean and we have no outstanding writeback in |
| * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this |
| * function since flusher thread may be doing for example sync in |
| * parallel and if we move the inode, it could get skipped. So here we |
| * make sure inode is on some writeback list and leave it there unless |
| * we have completely cleaned the inode. |
| */ |
| if (!(inode->i_state & I_DIRTY) && |
| (wbc->sync_mode != WB_SYNC_ALL || |
| !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))) |
| goto out; |
| iwbl_set_sync(iwbl, inode); |
| spin_unlock(&inode->i_lock); |
| |
| ret = __writeback_single_inode(inode, wbc); |
| |
| spin_lock(&wb->list_lock); |
| spin_lock(&inode->i_lock); |
| /* |
| * If inode is clean, remove it from writeback lists. Otherwise don't |
| * touch it. See comment above for explanation. |
| */ |
| if (!(inode->i_state & I_DIRTY)) |
| iwbl_del_locked(iwbl, wb); |
| spin_unlock(&wb->list_lock); |
| iwbl_sync_complete(iwbl); |
| out: |
| spin_unlock(&inode->i_lock); |
| return ret; |
| } |
| |
| static long writeback_chunk_size(struct bdi_writeback *wb, |
| struct wb_writeback_work *work) |
| { |
| long pages; |
| |
| /* |
| * WB_SYNC_ALL mode does livelock avoidance by syncing dirty |
| * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX |
| * here avoids calling into writeback_inodes_wb() more than once. |
| * |
| * The intended call sequence for WB_SYNC_ALL writeback is: |
| * |
| * wb_writeback() |
| * writeback_sb_inodes() <== called only once |
| * write_cache_pages() <== called once for each inode |
| * (quickly) tag currently dirty pages |
| * (maybe slowly) sync all tagged pages |
| */ |
| if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages) |
| pages = LONG_MAX; |
| else { |
| pages = min(wb->avg_write_bandwidth / 2, |
| global_dirty_limit / DIRTY_SCOPE); |
| pages = min(pages, work->nr_pages); |
| pages = round_down(pages + MIN_WRITEBACK_PAGES, |
| MIN_WRITEBACK_PAGES); |
| } |
| |
| return pages; |
| } |
| |
| /* |
| * Write a portion of b_io inodes which belong to @sb. |
| * |
| * Return the number of pages and/or inodes written. |
| */ |
| static long writeback_sb_inodes(struct super_block *sb, |
| struct bdi_writeback *wb, |
| struct wb_writeback_work *work) |
| { |
| struct writeback_control wbc = { |
| .sync_mode = work->sync_mode, |
| .tagged_writepages = work->tagged_writepages, |
| .for_kupdate = work->for_kupdate, |
| .for_background = work->for_background, |
| .for_sync = work->for_sync, |
| .range_cyclic = work->range_cyclic, |
| .range_start = 0, |
| .range_end = LLONG_MAX, |
| }; |
| unsigned long start_time = jiffies; |
| long write_chunk; |
| long wrote = 0; /* count both pages and inodes */ |
| |
| while (!list_empty(&wb->b_io)) { |
| struct inode_wb_link *iwbl = dirty_list_to_iwbl(wb->b_io.prev); |
| struct inode *inode = iwbl_to_inode(iwbl); |
| |
| if (inode->i_sb != sb) { |
| if (work->sb) { |
| /* |
| * We only want to write back data for this |
| * superblock, move all inodes not belonging |
| * to it back onto the dirty list. |
| */ |
| redirty_tail(iwbl, wb); |
| continue; |
| } |
| |
| /* |
| * The inode belongs to a different superblock. |
| * Bounce back to the caller to unpin this and |
| * pin the next superblock. |
| */ |
| break; |
| } |
| |
| wbc_set_iwbl(&wbc, iwbl); |
| |
| /* |
| * Don't bother with new inodes or inodes being freed, first |
| * kind does not need periodic writeout yet, and for the latter |
| * kind writeout is handled by the freer. |
| */ |
| spin_lock(&inode->i_lock); |
| if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { |
| spin_unlock(&inode->i_lock); |
| redirty_tail(iwbl, wb); |
| continue; |
| } |
| if (iwbl_test_sync(iwbl) && wbc.sync_mode != WB_SYNC_ALL) { |
| /* |
| * If this inode is locked for writeback and we are not |
| * doing writeback-for-data-integrity, move it to |
| * b_more_io so that writeback can proceed with the |
| * other inodes on s_io. |
| * |
| * We'll have another go at writing back this inode |
| * when we completed a full scan of b_io. |
| */ |
| spin_unlock(&inode->i_lock); |
| requeue_io(iwbl, wb); |
| trace_writeback_sb_inodes_requeue(inode); |
| continue; |
| } |
| spin_unlock(&wb->list_lock); |
| |
| /* |
| * We already requeued the inode if it had I_SYNC set and we |
| * are doing WB_SYNC_NONE writeback. So this catches only the |
| * WB_SYNC_ALL case. |
| */ |
| if (iwbl_test_sync(iwbl)) { |
| /* Wait for I_SYNC. This function drops i_lock... */ |
| iwbl_sleep_on_writeback(iwbl); |
| /* Inode may be gone, start again */ |
| spin_lock(&wb->list_lock); |
| continue; |
| } |
| iwbl_set_sync(iwbl, inode); |
| spin_unlock(&inode->i_lock); |
| |
| write_chunk = writeback_chunk_size(wb, work); |
| wbc.nr_to_write = write_chunk; |
| wbc.pages_skipped = 0; |
| |
| /* |
| * We use I_SYNC to pin the inode in memory. While it is set |
| * evict_inode() will wait so the inode cannot be freed. |
| */ |
| __writeback_single_inode(inode, &wbc); |
| |
| work->nr_pages -= write_chunk - wbc.nr_to_write; |
| wrote += write_chunk - wbc.nr_to_write; |
| spin_lock(&wb->list_lock); |
| spin_lock(&inode->i_lock); |
| if (!(inode->i_state & I_DIRTY)) |
| wrote++; |
| requeue_inode(iwbl, wb, &wbc); |
| iwbl_sync_complete(iwbl); |
| spin_unlock(&inode->i_lock); |
| cond_resched_lock(&wb->list_lock); |
| /* |
| * bail out to wb_writeback() often enough to check |
| * background threshold and other termination conditions. |
| */ |
| if (wrote) { |
| if (time_is_before_jiffies(start_time + HZ / 10UL)) |
| break; |
| if (work->nr_pages <= 0) |
| break; |
| } |
| } |
| return wrote; |
| } |
| |
| static long __writeback_inodes_wb(struct bdi_writeback *wb, |
| struct wb_writeback_work *work) |
| { |
| unsigned long start_time = jiffies; |
| long wrote = 0; |
| |
| while (!list_empty(&wb->b_io)) { |
| struct inode_wb_link *iwbl = dirty_list_to_iwbl(wb->b_io.prev); |
| struct inode *inode = iwbl_to_inode(iwbl); |
| struct super_block *sb = inode->i_sb; |
| |
| if (!grab_super_passive(sb)) { |
| /* |
| * grab_super_passive() may fail consistently due to |
| * s_umount being grabbed by someone else. Don't use |
| * requeue_io() to avoid busy retrying the inode/sb. |
| */ |
| redirty_tail(iwbl, wb); |
| continue; |
| } |
| wrote += writeback_sb_inodes(sb, wb, work); |
| drop_super(sb); |
| |
| /* refer to the same tests at the end of writeback_sb_inodes */ |
| if (wrote) { |
| if (time_is_before_jiffies(start_time + HZ / 10UL)) |
| break; |
| if (work->nr_pages <= 0) |
| break; |
| } |
| } |
| /* Leave any unwritten inodes on b_io */ |
| return wrote; |
| } |
| |
| static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages, |
| enum wb_reason reason) |
| { |
| struct wb_writeback_work work = { |
| .nr_pages = nr_pages, |
| .sync_mode = WB_SYNC_NONE, |
| .range_cyclic = 1, |
| .reason = reason, |
| }; |
| |
| spin_lock(&wb->list_lock); |
| if (list_empty(&wb->b_io)) |
| queue_io(wb, &work); |
| __writeback_inodes_wb(wb, &work); |
| spin_unlock(&wb->list_lock); |
| |
| return nr_pages - work.nr_pages; |
| } |
| |
| static bool over_bground_thresh(struct bdi_writeback *wb) |
| { |
| unsigned long background_thresh, dirty_thresh; |
| |
| global_dirty_limits(&background_thresh, &dirty_thresh); |
| |
| if (global_page_state(NR_FILE_DIRTY) + |
| global_page_state(NR_UNSTABLE_NFS) > background_thresh) |
| return true; |
| |
| if (wb_stat(wb, WB_RECLAIMABLE) > wb_dirty_limit(wb, background_thresh)) |
| return true; |
| |
| return false; |
| } |
| |
| /* |
| * Called under wb->list_lock. If there are multiple wb per bdi, |
| * only the flusher working on the first wb should do it. |
| */ |
| static void wb_update_bandwidth(struct bdi_writeback *wb, |
| unsigned long start_time) |
| { |
| __wb_update_bandwidth(wb, 0, 0, 0, 0, 0, start_time); |
| } |
| |
| /* |
| * Explicit flushing or periodic writeback of "old" data. |
| * |
| * Define "old": the first time one of an inode's pages is dirtied, we mark the |
| * dirtying-time in the inode's address_space. So this periodic writeback code |
| * just walks the superblock inode list, writing back any inodes which are |
| * older than a specific point in time. |
| * |
| * Try to run once per dirty_writeback_interval. But if a writeback event |
| * takes longer than a dirty_writeback_interval interval, then leave a |
| * one-second gap. |
| * |
| * older_than_this takes precedence over nr_to_write. So we'll only write back |
| * all dirty pages if they are all attached to "old" mappings. |
| */ |
| static long wb_writeback(struct bdi_writeback *wb, |
| struct wb_writeback_work *work) |
| { |
| unsigned long wb_start = jiffies; |
| long nr_pages = work->nr_pages; |
| unsigned long oldest_jif; |
| struct inode_wb_link *iwbl; |
| struct inode *inode; |
| long progress; |
| |
| oldest_jif = jiffies; |
| work->older_than_this = &oldest_jif; |
| |
| spin_lock(&wb->list_lock); |
| for (;;) { |
| /* |
| * Stop writeback when nr_pages has been consumed |
| */ |
| if (work->nr_pages <= 0) |
| break; |
| |
| /* |
| * Background writeout and kupdate-style writeback may |
| * run forever. Stop them if there is other work to do |
| * so that e.g. sync can proceed. They'll be restarted |
| * after the other works are all done. |
| */ |
| if ((work->for_background || work->for_kupdate) && |
| !list_empty(&wb->work_list)) |
| break; |
| |
| /* |
| * For background writeout, stop when we are below the |
| * background dirty threshold |
| */ |
| if (work->for_background && !over_bground_thresh(wb)) |
| break; |
| |
| /* |
| * Kupdate and background works are special and we want to |
| * include all inodes that need writing. Livelock avoidance is |
| * handled by these works yielding to any other work so we are |
| * safe. |
| */ |
| if (work->for_kupdate) { |
| oldest_jif = jiffies - |
| msecs_to_jiffies(dirty_expire_interval * 10); |
| } else if (work->for_background) |
| oldest_jif = jiffies; |
| |
| trace_writeback_start(wb->bdi, work); |
| if (list_empty(&wb->b_io)) |
| queue_io(wb, work); |
| if (work->sb) |
| progress = writeback_sb_inodes(work->sb, wb, work); |
| else |
| progress = __writeback_inodes_wb(wb, work); |
| trace_writeback_written(wb->bdi, work); |
| |
| wb_update_bandwidth(wb, wb_start); |
| |
| /* |
| * Did we write something? Try for more |
| * |
| * Dirty inodes are moved to b_io for writeback in batches. |
| * The completion of the current batch does not necessarily |
| * mean the overall work is done. So we keep looping as long |
| * as made some progress on cleaning pages or inodes. |
| */ |
| if (progress) |
| continue; |
| /* |
| * No more inodes for IO, bail |
| */ |
| if (list_empty(&wb->b_more_io)) |
| break; |
| /* |
| * Nothing written. Wait for some inode to |
| * become available for writeback. Otherwise |
| * we'll just busyloop. |
| */ |
| if (!list_empty(&wb->b_more_io)) { |
| trace_writeback_wait(wb->bdi, work); |
| iwbl = dirty_list_to_iwbl(wb->b_more_io.prev); |
| inode = iwbl_to_inode(iwbl); |
| spin_lock(&inode->i_lock); |
| spin_unlock(&wb->list_lock); |
| /* This function drops i_lock... */ |
| iwbl_sleep_on_writeback(iwbl); |
| spin_lock(&wb->list_lock); |
| } |
| } |
| spin_unlock(&wb->list_lock); |
| |
| return nr_pages - work->nr_pages; |
| } |
| |
| /* |
| * Return the next wb_writeback_work struct that hasn't been processed yet. |
| */ |
| static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb) |
| { |
| struct wb_writeback_work *work = NULL; |
| |
| spin_lock_bh(&wb->work_lock); |
| if (!list_empty(&wb->work_list)) { |
| work = list_entry(wb->work_list.next, |
| struct wb_writeback_work, list); |
| list_del_init(&work->list); |
| } |
| spin_unlock_bh(&wb->work_lock); |
| return work; |
| } |
| |
| /* |
| * Add in the number of potentially dirty inodes, because each inode |
| * write can dirty pagecache in the underlying blockdev. |
| */ |
| static unsigned long get_nr_dirty_pages(void) |
| { |
| return global_page_state(NR_FILE_DIRTY) + |
| global_page_state(NR_UNSTABLE_NFS) + |
| get_nr_dirty_inodes(); |
| } |
| |
| static long wb_check_background_flush(struct bdi_writeback *wb) |
| { |
| if (over_bground_thresh(wb)) { |
| |
| struct wb_writeback_work work = { |
| .nr_pages = LONG_MAX, |
| .sync_mode = WB_SYNC_NONE, |
| .for_background = 1, |
| .range_cyclic = 1, |
| .reason = WB_REASON_BACKGROUND, |
| }; |
| |
| return wb_writeback(wb, &work); |
| } |
| |
| return 0; |
| } |
| |
| static long wb_check_old_data_flush(struct bdi_writeback *wb) |
| { |
| unsigned long expired; |
| long nr_pages; |
| |
| /* |
| * When set to zero, disable periodic writeback |
| */ |
| if (!dirty_writeback_interval) |
| return 0; |
| |
| expired = wb->last_old_flush + |
| msecs_to_jiffies(dirty_writeback_interval * 10); |
| if (time_before(jiffies, expired)) |
| return 0; |
| |
| wb->last_old_flush = jiffies; |
| nr_pages = get_nr_dirty_pages(); |
| |
| if (nr_pages) { |
| struct wb_writeback_work work = { |
| .nr_pages = nr_pages, |
| .sync_mode = WB_SYNC_NONE, |
| .for_kupdate = 1, |
| .range_cyclic = 1, |
| .reason = WB_REASON_PERIODIC, |
| }; |
| |
| return wb_writeback(wb, &work); |
| } |
| |
| return 0; |
| } |
| |
| /* |
| * Retrieve work items and do the writeback they describe |
| */ |
| static long wb_do_writeback(struct bdi_writeback *wb) |
| { |
| struct wb_writeback_work *work; |
| long wrote = 0; |
| |
| set_bit(WB_writeback_running, &wb->state); |
| while ((work = get_next_work_item(wb)) != NULL) { |
| struct wb_completion *done = work->done; |
| bool need_wake_up = false; |
| |
| trace_writeback_exec(wb->bdi, work); |
| |
| if (!maybe_writeback_single_mapping(work)) |
| wrote += wb_writeback(wb, work); |
| |
| if (work->single_wait) { |
| WARN_ON_ONCE(work->auto_free); |
| /* paired w/ rmb in wb_wait_for_single_work() */ |
| smp_wmb(); |
| work->single_done = 1; |
| need_wake_up = true; |
| } else if (work->auto_free) { |
| kfree(work); |
| } |
| |
| if (done && atomic_dec_and_test(&done->cnt)) |
| need_wake_up = true; |
| |
| if (need_wake_up) |
| wake_up_all(&wb->bdi->wb_waitq); |
| } |
| |
| /* |
| * Check for periodic writeback, kupdated() style |
| */ |
| wrote += wb_check_old_data_flush(wb); |
| wrote += wb_check_background_flush(wb); |
| clear_bit(WB_writeback_running, &wb->state); |
| |
| return wrote; |
| } |
| |
| /* |
| * Handle writeback of dirty data for the device backed by this bdi. Also |
| * reschedules periodically and does kupdated style flushing. |
| */ |
| void wb_workfn(struct work_struct *work) |
| { |
| struct bdi_writeback *wb = container_of(to_delayed_work(work), |
| struct bdi_writeback, dwork); |
| long pages_written; |
| |
| set_worker_desc("flush-%s", dev_name(wb->bdi->dev)); |
| current->flags |= PF_SWAPWRITE; |
| |
| if (likely(!current_is_workqueue_rescuer() || |
| !test_bit(WB_registered, &wb->state))) { |
| /* |
| * The normal path. Keep writing back @wb until its |
| * work_list is empty. Note that this path is also taken |
| * if @wb is shutting down even when we're running off the |
| * rescuer as work_list needs to be drained. |
| */ |
| do { |
| pages_written = wb_do_writeback(wb); |
| trace_writeback_pages_written(pages_written); |
| } while (!list_empty(&wb->work_list)); |
| } else { |
| /* |
| * bdi_wq can't get enough workers and we're running off |
| * the emergency worker. Don't hog it. Hopefully, 1024 is |
| * enough for efficient IO. |
| */ |
| pages_written = writeback_inodes_wb(wb, 1024, |
| WB_REASON_FORKER_THREAD); |
| trace_writeback_pages_written(pages_written); |
| } |
| |
| if (!list_empty(&wb->work_list)) |
| mod_delayed_work(bdi_wq, &wb->dwork, 0); |
| else if (wb_has_dirty_io(wb) && dirty_writeback_interval) |
| wb_wakeup_delayed(wb); |
| |
| current->flags &= ~PF_SWAPWRITE; |
| } |
| |
| /* |
| * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back |
| * the whole world. |
| */ |
| void wakeup_flusher_threads(long nr_pages, enum wb_reason reason) |
| { |
| struct backing_dev_info *bdi; |
| |
| if (!nr_pages) |
| nr_pages = get_nr_dirty_pages(); |
| |
| rcu_read_lock(); |
| list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { |
| struct bdi_writeback *wb; |
| struct wb_iter iter; |
| |
| if (!bdi_has_dirty_io(bdi)) |
| continue; |
| |
| bdi_for_each_wb(wb, bdi, &iter, 0) |
| wb_start_writeback(wb, wb_split_bdi_pages(wb, nr_pages), |
| false, reason); |
| } |
| rcu_read_unlock(); |
| } |
| |
| static noinline void block_dump___mark_inode_dirty(struct inode *inode) |
| { |
| if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { |
| struct dentry *dentry; |
| const char *name = "?"; |
| |
| dentry = d_find_alias(inode); |
| if (dentry) { |
| spin_lock(&dentry->d_lock); |
| name = (const char *) dentry->d_name.name; |
| } |
| printk(KERN_DEBUG |
| "%s(%d): dirtied inode %lu (%s) on %s\n", |
| current->comm, task_pid_nr(current), inode->i_ino, |
| name, inode->i_sb->s_id); |
| if (dentry) { |
| spin_unlock(&dentry->d_lock); |
| dput(dentry); |
| } |
| } |
| } |
| |
| static void __mark_inode_dirty_dctx(struct dirty_context *dctx, int flags) |
| { |
| struct inode *inode = dctx->inode; |
| struct inode_wb_link *iwbl = dctx->iwbl; |
| struct bdi_writeback *wb = dctx->wb; |
| struct super_block *sb = inode->i_sb; |
| |
| /* |
| * Don't do this for I_DIRTY_PAGES - that doesn't actually |
| * dirty the inode itself |
| */ |
| if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { |
| trace_writeback_dirty_inode_start(inode, flags); |
| |
| if (sb->s_op->dirty_inode) |
| sb->s_op->dirty_inode(inode, flags); |
| |
| trace_writeback_dirty_inode(inode, flags); |
| } |
| |
| /* |
| * Paired with smp_mb()'s in __writeback_single_inode() and |
| * mapping_writeback_maybe_whole() for the following lockless |
| * iwbl_has_enough_dirty() test. See there for details. |
| */ |
| smp_mb(); |
| |
| if (iwbl_has_enough_dirty(iwbl, inode, flags)) |
| return; |
| |
| if (unlikely(block_dump)) |
| block_dump___mark_inode_dirty(inode); |
| |
| spin_lock(&inode->i_lock); |
| if (!iwbl_has_enough_dirty(iwbl, inode, flags)) { |
| bool was_dirty; |
| |
| was_dirty = iwbl_set_dirty(iwbl, inode, flags); |
| |
| /* |
| * If the inode is being synced, just update its dirty state. |
| * The unlocker will place the inode on the appropriate |
| * superblock list, based upon its state. |
| */ |
| if (iwbl_test_sync(iwbl)) |
| goto out_unlock_inode; |
| |
| /* |
| * Only add valid (hashed) inodes to the superblock's |
| * dirty list. Add blockdev inodes as well. |
| */ |
| if (!S_ISBLK(inode->i_mode)) { |
| if (inode_unhashed(inode)) |
| goto out_unlock_inode; |
| } |
| if (inode->i_state & I_FREEING) |
| goto out_unlock_inode; |
| |
| /* |
| * If the inode was already on b_dirty/b_io/b_more_io, don't |
| * reposition it (that would break b_dirty time-ordering). |
| */ |
| if (!was_dirty) { |
| bool wakeup_bdi = false; |
| |
| spin_unlock(&inode->i_lock); |
| spin_lock(&wb->list_lock); |
| |
| WARN(bdi_cap_writeback_dirty(wb->bdi) && |
| !test_bit(WB_registered, &wb->state), |
| "bdi-%s not registered\n", wb->bdi->name); |
| |
| iwbl->dirtied_when = jiffies; |
| wakeup_bdi = iwbl_move_locked(iwbl, wb, &wb->b_dirty); |
| spin_unlock(&wb->list_lock); |
| |
| /* |
| * If this is the first dirty inode for this bdi, |
| * we have to wake-up the corresponding bdi thread |
| * to make sure background write-back happens |
| * later. |
| */ |
| if (bdi_cap_writeback_dirty(wb->bdi) && wakeup_bdi) |
| wb_wakeup_delayed(wb); |
| return; |
| } |
| } |
| out_unlock_inode: |
| spin_unlock(&inode->i_lock); |
| |
| } |
| EXPORT_SYMBOL(mark_inode_dirty_dctx); |
| |
| /** |
| * mark_inode_dirty_dctx - internal function |
| * @dctx: dirty_context containing the target inode |
| * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) |
| * Mark an inode as dirty. Callers should use mark_inode_dirty or |
| * mark_inode_dirty_sync. |
| * |
| * Put the inode on the super block's dirty list. |
| * |
| * CAREFUL! We mark it dirty unconditionally, but move it onto the |
| * dirty list only if it is hashed or if it refers to a blockdev. |
| * If it was not hashed, it will never be added to the dirty list |
| * even if it is later hashed, as it will have been marked dirty already. |
| * |
| * In short, make sure you hash any inodes _before_ you start marking |
| * them dirty. |
| * |
| * Note that for blockdevs, iwbl->dirtied_when represents the dirtying time of |
| * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of |
| * the kernel-internal blockdev inode represents the dirtying time of the |
| * blockdev's pages. This is why for I_DIRTY_PAGES we always use |
| * page->mapping->host, so the page-dirtying time is recorded in the internal |
| * blockdev inode. |
| */ |
| void mark_inode_dirty_dctx(struct dirty_context *dctx, int flags) |
| { |
| /* |
| * I_DIRTY_PAGES should dirty @dctx->iwbl but I_DIRTY_[DATA]SYNC |
| * should always dirty the root iwbl. If @dctx->iwbl is root, we |
| * can do both at the same time; otherwise, handle the two dirtying |
| * separately. |
| */ |
| if (iwbl_is_root(dctx->iwbl) || |
| !(flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC))) { |
| __mark_inode_dirty_dctx(dctx, flags); |
| return; |
| } |
| |
| if (flags & I_DIRTY_PAGES) |
| __mark_inode_dirty_dctx(dctx, I_DIRTY_PAGES); |
| |
| flags &= ~I_DIRTY_PAGES; |
| if (flags) |
| __mark_inode_dirty(dctx->inode, flags); |
| } |
| |
| void __mark_inode_dirty(struct inode *inode, int flags) |
| { |
| struct dirty_context dctx; |
| |
| init_dirty_inode_context(&dctx, inode); |
| mark_inode_dirty_dctx(&dctx, flags); |
| } |
| EXPORT_SYMBOL(__mark_inode_dirty); |
| |
| static void wait_sb_inodes(struct super_block *sb) |
| { |
| struct inode *inode, *old_inode = NULL; |
| |
| /* |
| * We need to be protected against the filesystem going from |
| * r/o to r/w or vice versa. |
| */ |
| WARN_ON(!rwsem_is_locked(&sb->s_umount)); |
| |
| spin_lock(&inode_sb_list_lock); |
| |
| /* |
| * Data integrity sync. Must wait for all pages under writeback, |
| * because there may have been pages dirtied before our sync |
| * call, but which had writeout started before we write it out. |
| * In which case, the inode may not be on the dirty list, but |
| * we still have to wait for that writeout. |
| */ |
| list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { |
| struct address_space *mapping = inode->i_mapping; |
| |
| spin_lock(&inode->i_lock); |
| if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) || |
| (mapping->nrpages == 0)) { |
| spin_unlock(&inode->i_lock); |
| continue; |
| } |
| __iget(inode); |
| spin_unlock(&inode->i_lock); |
| spin_unlock(&inode_sb_list_lock); |
| |
| /* |
| * We hold a reference to 'inode' so it couldn't have been |
| * removed from s_inodes list while we dropped the |
| * inode_sb_list_lock. We cannot iput the inode now as we can |
| * be holding the last reference and we cannot iput it under |
| * inode_sb_list_lock. So we keep the reference and iput it |
| * later. |
| */ |
| iput(old_inode); |
| old_inode = inode; |
| |
| filemap_fdatawait(mapping); |
| |
| cond_resched(); |
| |
| spin_lock(&inode_sb_list_lock); |
| } |
| spin_unlock(&inode_sb_list_lock); |
| iput(old_inode); |
| } |
| |
| static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr, |
| enum wb_reason reason, bool skip_if_busy) |
| { |
| DEFINE_WB_COMPLETION_ONSTACK(done); |
| struct wb_writeback_work work = { |
| .sb = sb, |
| .sync_mode = WB_SYNC_NONE, |
| .tagged_writepages = 1, |
| .done = &done, |
| .nr_pages = nr, |
| .reason = reason, |
| }; |
| struct backing_dev_info *bdi = sb->s_bdi; |
| |
| if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info) |
| return; |
| WARN_ON(!rwsem_is_locked(&sb->s_umount)); |
| |
| bdi_split_work_to_wbs(sb->s_bdi, &work, skip_if_busy); |
| wb_wait_for_completion(bdi, &done); |
| } |
| |
| /** |
| * writeback_inodes_sb_nr - writeback dirty inodes from given super_block |
| * @sb: the superblock |
| * @nr: the number of pages to write |
| * @reason: reason why some writeback work initiated |
| * |
| * Start writeback on some inodes on this super_block. No guarantees are made |
| * on how many (if any) will be written, and this function does not wait |
| * for IO completion of submitted IO. |
| */ |
| void writeback_inodes_sb_nr(struct super_block *sb, |
| unsigned long nr, |
| enum wb_reason reason) |
| { |
| __writeback_inodes_sb_nr(sb, nr, reason, false); |
| } |
| EXPORT_SYMBOL(writeback_inodes_sb_nr); |
| |
| /** |
| * writeback_inodes_sb - writeback dirty inodes from given super_block |
| * @sb: the superblock |
| * @reason: reason why some writeback work was initiated |
| * |
| * Start writeback on some inodes on this super_block. No guarantees are made |
| * on how many (if any) will be written, and this function does not wait |
| * for IO completion of submitted IO. |
| */ |
| void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) |
| { |
| return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); |
| } |
| EXPORT_SYMBOL(writeback_inodes_sb); |
| |
| /** |
| * try_to_writeback_inodes_sb_nr - try to start writeback if none underway |
| * @sb: the superblock |
| * @nr: the number of pages to write |
| * @reason: the reason of writeback |
| * |
| * Invoke writeback_inodes_sb_nr if no writeback is currently underway. |
| * Returns 1 if writeback was started, 0 if not. |
| */ |
| bool try_to_writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr, |
| enum wb_reason reason) |
| { |
| if (!down_read_trylock(&sb->s_umount)) |
| return false; |
| |
| __writeback_inodes_sb_nr(sb, nr, reason, true); |
| up_read(&sb->s_umount); |
| return true; |
| } |
| EXPORT_SYMBOL(try_to_writeback_inodes_sb_nr); |
| |
| /** |
| * try_to_writeback_inodes_sb - try to start writeback if none underway |
| * @sb: the superblock |
| * @reason: reason why some writeback work was initiated |
| * |
| * Implement by try_to_writeback_inodes_sb_nr() |
| * Returns 1 if writeback was started, 0 if not. |
| */ |
| bool try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) |
| { |
| return try_to_writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); |
| } |
| EXPORT_SYMBOL(try_to_writeback_inodes_sb); |
| |
| /** |
| * sync_inodes_sb - sync sb inode pages |
| * @sb: the superblock |
| * |
| * This function writes and waits on any dirty inode belonging to this |
| * super_block. |
| */ |
| void sync_inodes_sb(struct super_block *sb) |
| { |
| DEFINE_WB_COMPLETION_ONSTACK(done); |
| struct wb_writeback_work work = { |
| .sb = sb, |
| .sync_mode = WB_SYNC_ALL, |
| .nr_pages = LONG_MAX, |
| .range_cyclic = 0, |
| .done = &done, |
| .reason = WB_REASON_SYNC, |
| .for_sync = 1, |
| }; |
| struct backing_dev_info *bdi = sb->s_bdi; |
| |
| /* Nothing to do? */ |
| if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info) |
| return; |
| WARN_ON(!rwsem_is_locked(&sb->s_umount)); |
| |
| bdi_split_work_to_wbs(bdi, &work, false); |
| wb_wait_for_completion(bdi, &done); |
| |
| wait_sb_inodes(sb); |
| } |
| EXPORT_SYMBOL(sync_inodes_sb); |
| |
| /** |
| * write_inode_now - write an inode to disk |
| * @inode: inode to write to disk |
| * @sync: whether the write should be synchronous or not |
| * |
| * This function commits an inode to disk immediately if it is dirty. This is |
| * primarily needed by knfsd. |
| * |
| * The caller must either have a ref on the inode or must have set I_WILL_FREE. |
| */ |
| int write_inode_now(struct inode *inode, int sync) |
| { |
| struct writeback_control wbc = { |
| .nr_to_write = LONG_MAX, |
| .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, |
| .range_start = 0, |
| .range_end = LLONG_MAX, |
| }; |
| |
| if (!mapping_cap_writeback_dirty(inode->i_mapping)) |
| wbc.nr_to_write = 0; |
| |
| might_sleep(); |
| return writeback_single_inode(inode, &wbc); |
| } |
| EXPORT_SYMBOL(write_inode_now); |
| |
| /** |
| * sync_inode - write an inode and its pages to disk. |
| * @inode: the inode to sync |
| * @wbc: controls the writeback mode |
| * |
| * sync_inode() will write an inode and its pages to disk. It will also |
| * correctly update the inode on its superblock's dirty inode lists and will |
| * update inode->i_state. |
| * |
| * The caller must have a ref on the inode. |
| */ |
| int sync_inode(struct inode *inode, struct writeback_control *wbc) |
| { |
| return writeback_single_inode(inode, wbc); |
| } |
| EXPORT_SYMBOL(sync_inode); |
| |
| /** |
| * sync_inode_metadata - write an inode to disk |
| * @inode: the inode to sync |
| * @wait: wait for I/O to complete. |
| * |
| * Write an inode to disk and adjust its dirty state after completion. |
| * |
| * Note: only writes the actual inode, no associated data or other metadata. |
| */ |
| int sync_inode_metadata(struct inode *inode, int wait) |
| { |
| struct writeback_control wbc = { |
| .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, |
| .nr_to_write = 0, /* metadata-only */ |
| }; |
| |
| return sync_inode(inode, &wbc); |
| } |
| EXPORT_SYMBOL(sync_inode_metadata); |