Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/fs-writeback.c |
| 3 | * |
| 4 | * Copyright (C) 2002, Linus Torvalds. |
| 5 | * |
| 6 | * Contains all the functions related to writing back and waiting |
| 7 | * upon dirty inodes against superblocks, and writing back dirty |
| 8 | * pages against inodes. ie: data writeback. Writeout of the |
| 9 | * inode itself is not handled here. |
| 10 | * |
Francois Cami | e1f8e87 | 2008-10-15 22:01:59 -0700 | [diff] [blame] | 11 | * 10Apr2002 Andrew Morton |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Split out of fs/inode.c |
| 13 | * Additions for address_space-based writeback |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
Jens Axboe | f5ff842 | 2007-09-21 09:19:54 +0200 | [diff] [blame] | 17 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/mm.h> |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 22 | #include <linux/kthread.h> |
| 23 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/writeback.h> |
| 25 | #include <linux/blkdev.h> |
| 26 | #include <linux/backing-dev.h> |
| 27 | #include <linux/buffer_head.h> |
David Howells | 07f3f05 | 2006-09-30 20:52:18 +0200 | [diff] [blame] | 28 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 30 | #define inode_to_bdi(inode) ((inode)->i_mapping->backing_dev_info) |
Adrian Bunk | f11b00f | 2008-04-29 00:58:56 -0700 | [diff] [blame] | 31 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 32 | /* |
Jens Axboe | d0bceac | 2009-05-18 08:20:32 +0200 | [diff] [blame] | 33 | * We don't actually have pdflush, but this one is exported though /proc... |
| 34 | */ |
| 35 | int nr_pdflush_threads; |
| 36 | |
| 37 | /* |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 38 | * Passed into wb_writeback(), essentially a subset of writeback_control |
| 39 | */ |
| 40 | struct wb_writeback_args { |
| 41 | long nr_pages; |
| 42 | struct super_block *sb; |
| 43 | enum writeback_sync_modes sync_mode; |
| 44 | int for_kupdate; |
| 45 | int range_cyclic; |
| 46 | }; |
| 47 | |
| 48 | /* |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 49 | * Work items for the bdi_writeback threads |
Adrian Bunk | f11b00f | 2008-04-29 00:58:56 -0700 | [diff] [blame] | 50 | */ |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 51 | struct bdi_work { |
| 52 | struct list_head list; |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 53 | struct rcu_head rcu_head; |
| 54 | |
| 55 | unsigned long seen; |
| 56 | atomic_t pending; |
| 57 | |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 58 | struct wb_writeback_args args; |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 59 | |
| 60 | unsigned long state; |
| 61 | }; |
| 62 | |
| 63 | enum { |
| 64 | WS_USED_B = 0, |
| 65 | WS_ONSTACK_B, |
| 66 | }; |
| 67 | |
| 68 | #define WS_USED (1 << WS_USED_B) |
| 69 | #define WS_ONSTACK (1 << WS_ONSTACK_B) |
| 70 | |
| 71 | static inline bool bdi_work_on_stack(struct bdi_work *work) |
Adrian Bunk | f11b00f | 2008-04-29 00:58:56 -0700 | [diff] [blame] | 72 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 73 | return test_bit(WS_ONSTACK_B, &work->state); |
| 74 | } |
| 75 | |
| 76 | static inline void bdi_work_init(struct bdi_work *work, |
| 77 | struct writeback_control *wbc) |
| 78 | { |
| 79 | INIT_RCU_HEAD(&work->rcu_head); |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 80 | work->args.sb = wbc->sb; |
| 81 | work->args.nr_pages = wbc->nr_to_write; |
| 82 | work->args.sync_mode = wbc->sync_mode; |
| 83 | work->args.range_cyclic = wbc->range_cyclic; |
| 84 | work->args.for_kupdate = 0; |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 85 | work->state = WS_USED; |
| 86 | } |
| 87 | |
Adrian Bunk | f11b00f | 2008-04-29 00:58:56 -0700 | [diff] [blame] | 88 | /** |
| 89 | * writeback_in_progress - determine whether there is writeback in progress |
| 90 | * @bdi: the device's backing_dev_info structure. |
| 91 | * |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 92 | * Determine whether there is writeback waiting to be handled against a |
| 93 | * backing device. |
Adrian Bunk | f11b00f | 2008-04-29 00:58:56 -0700 | [diff] [blame] | 94 | */ |
| 95 | int writeback_in_progress(struct backing_dev_info *bdi) |
| 96 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 97 | return !list_empty(&bdi->work_list); |
Adrian Bunk | f11b00f | 2008-04-29 00:58:56 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 100 | static void bdi_work_clear(struct bdi_work *work) |
Adrian Bunk | f11b00f | 2008-04-29 00:58:56 -0700 | [diff] [blame] | 101 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 102 | clear_bit(WS_USED_B, &work->state); |
| 103 | smp_mb__after_clear_bit(); |
| 104 | wake_up_bit(&work->state, WS_USED_B); |
Adrian Bunk | f11b00f | 2008-04-29 00:58:56 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 107 | static void bdi_work_free(struct rcu_head *head) |
Nick Piggin | 4195f73 | 2009-05-28 09:01:15 +0200 | [diff] [blame] | 108 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 109 | struct bdi_work *work = container_of(head, struct bdi_work, rcu_head); |
Nick Piggin | 4195f73 | 2009-05-28 09:01:15 +0200 | [diff] [blame] | 110 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 111 | if (!bdi_work_on_stack(work)) |
| 112 | kfree(work); |
| 113 | else |
| 114 | bdi_work_clear(work); |
| 115 | } |
| 116 | |
| 117 | static void wb_work_complete(struct bdi_work *work) |
| 118 | { |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 119 | const enum writeback_sync_modes sync_mode = work->args.sync_mode; |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * For allocated work, we can clear the done/seen bit right here. |
| 123 | * For on-stack work, we need to postpone both the clear and free |
| 124 | * to after the RCU grace period, since the stack could be invalidated |
| 125 | * as soon as bdi_work_clear() has done the wakeup. |
| 126 | */ |
| 127 | if (!bdi_work_on_stack(work)) |
| 128 | bdi_work_clear(work); |
| 129 | if (sync_mode == WB_SYNC_NONE || bdi_work_on_stack(work)) |
| 130 | call_rcu(&work->rcu_head, bdi_work_free); |
| 131 | } |
| 132 | |
| 133 | static void wb_clear_pending(struct bdi_writeback *wb, struct bdi_work *work) |
| 134 | { |
| 135 | /* |
| 136 | * The caller has retrieved the work arguments from this work, |
| 137 | * drop our reference. If this is the last ref, delete and free it |
| 138 | */ |
| 139 | if (atomic_dec_and_test(&work->pending)) { |
| 140 | struct backing_dev_info *bdi = wb->bdi; |
| 141 | |
| 142 | spin_lock(&bdi->wb_lock); |
| 143 | list_del_rcu(&work->list); |
| 144 | spin_unlock(&bdi->wb_lock); |
| 145 | |
| 146 | wb_work_complete(work); |
Nick Piggin | 4195f73 | 2009-05-28 09:01:15 +0200 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 150 | static void bdi_queue_work(struct backing_dev_info *bdi, struct bdi_work *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 152 | if (work) { |
| 153 | work->seen = bdi->wb_mask; |
| 154 | BUG_ON(!work->seen); |
| 155 | atomic_set(&work->pending, bdi->wb_cnt); |
| 156 | BUG_ON(!bdi->wb_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 158 | /* |
| 159 | * Make sure stores are seen before it appears on the list |
| 160 | */ |
| 161 | smp_mb(); |
| 162 | |
| 163 | spin_lock(&bdi->wb_lock); |
| 164 | list_add_tail_rcu(&work->list, &bdi->work_list); |
| 165 | spin_unlock(&bdi->wb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 169 | * If the default thread isn't there, make sure we add it. When |
| 170 | * it gets created and wakes up, we'll run this work. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | */ |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 172 | if (unlikely(list_empty_careful(&bdi->wb_list))) |
| 173 | wake_up_process(default_backing_dev_info.wb.task); |
| 174 | else { |
| 175 | struct bdi_writeback *wb = &bdi->wb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | /* |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 178 | * If we failed allocating the bdi work item, wake up the wb |
| 179 | * thread always. As a safety precaution, it'll flush out |
| 180 | * everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | */ |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 182 | if (!wb_has_dirty_io(wb)) { |
| 183 | if (work) |
| 184 | wb_clear_pending(wb, work); |
| 185 | } else if (wb->task) |
| 186 | wake_up_process(wb->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 190 | /* |
| 191 | * Used for on-stack allocated work items. The caller needs to wait until |
| 192 | * the wb threads have acked the work before it's safe to continue. |
| 193 | */ |
| 194 | static void bdi_wait_on_work_clear(struct bdi_work *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 196 | wait_on_bit(&work->state, WS_USED_B, bdi_sched_wait, |
| 197 | TASK_UNINTERRUPTIBLE); |
| 198 | } |
| 199 | |
Jens Axboe | f11fcae | 2009-09-15 09:53:35 +0200 | [diff] [blame] | 200 | static void bdi_alloc_queue_work(struct backing_dev_info *bdi, |
| 201 | struct writeback_control *wbc) |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 202 | { |
| 203 | struct bdi_work *work; |
| 204 | |
| 205 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
| 206 | if (work) |
| 207 | bdi_work_init(work, wbc); |
| 208 | |
Jens Axboe | f11fcae | 2009-09-15 09:53:35 +0200 | [diff] [blame] | 209 | bdi_queue_work(bdi, work); |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void bdi_start_writeback(struct writeback_control *wbc) |
| 213 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 214 | /* |
Christoph Hellwig | f0fad8a | 2009-09-11 09:47:56 +0200 | [diff] [blame] | 215 | * WB_SYNC_NONE is opportunistic writeback. If this allocation fails, |
| 216 | * bdi_queue_work() will wake up the thread and flush old data. This |
| 217 | * should ensure some amount of progress in freeing memory. |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 218 | */ |
Jens Axboe | f11fcae | 2009-09-15 09:53:35 +0200 | [diff] [blame] | 219 | if (wbc->sync_mode != WB_SYNC_ALL) |
| 220 | bdi_alloc_queue_work(wbc->bdi, wbc); |
| 221 | else { |
Christoph Hellwig | f0fad8a | 2009-09-11 09:47:56 +0200 | [diff] [blame] | 222 | struct bdi_work work; |
| 223 | |
| 224 | bdi_work_init(&work, wbc); |
| 225 | work.state |= WS_ONSTACK; |
| 226 | |
| 227 | bdi_queue_work(wbc->bdi, &work); |
| 228 | bdi_wait_on_work_clear(&work); |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* |
Andrew Morton | 6610a0b | 2007-10-16 23:30:32 -0700 | [diff] [blame] | 233 | * Redirty an inode: set its when-it-was dirtied timestamp and move it to the |
| 234 | * furthest end of its superblock's dirty-inode list. |
| 235 | * |
| 236 | * Before stamping the inode's ->dirtied_when, we check to see whether it is |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 237 | * already the most-recently-dirtied inode on the b_dirty list. If that is |
Andrew Morton | 6610a0b | 2007-10-16 23:30:32 -0700 | [diff] [blame] | 238 | * the case then the inode must have been redirtied while it was being written |
| 239 | * out and we don't reset its dirtied_when. |
| 240 | */ |
| 241 | static void redirty_tail(struct inode *inode) |
| 242 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 243 | struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; |
Andrew Morton | 6610a0b | 2007-10-16 23:30:32 -0700 | [diff] [blame] | 244 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 245 | if (!list_empty(&wb->b_dirty)) { |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 246 | struct inode *tail; |
Andrew Morton | 6610a0b | 2007-10-16 23:30:32 -0700 | [diff] [blame] | 247 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 248 | tail = list_entry(wb->b_dirty.next, struct inode, i_list); |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 249 | if (time_before(inode->dirtied_when, tail->dirtied_when)) |
Andrew Morton | 6610a0b | 2007-10-16 23:30:32 -0700 | [diff] [blame] | 250 | inode->dirtied_when = jiffies; |
| 251 | } |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 252 | list_move(&inode->i_list, &wb->b_dirty); |
Andrew Morton | 6610a0b | 2007-10-16 23:30:32 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 256 | * requeue inode for re-scanning after bdi->b_io list is exhausted. |
Andrew Morton | c986d1e | 2007-10-16 23:30:34 -0700 | [diff] [blame] | 257 | */ |
Ken Chen | 0e0f4fc | 2007-10-16 23:30:38 -0700 | [diff] [blame] | 258 | static void requeue_io(struct inode *inode) |
Andrew Morton | c986d1e | 2007-10-16 23:30:34 -0700 | [diff] [blame] | 259 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 260 | struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; |
| 261 | |
| 262 | list_move(&inode->i_list, &wb->b_more_io); |
Andrew Morton | c986d1e | 2007-10-16 23:30:34 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Joern Engel | 1c0eeaf | 2007-10-16 23:30:44 -0700 | [diff] [blame] | 265 | static void inode_sync_complete(struct inode *inode) |
| 266 | { |
| 267 | /* |
| 268 | * Prevent speculative execution through spin_unlock(&inode_lock); |
| 269 | */ |
| 270 | smp_mb(); |
| 271 | wake_up_bit(&inode->i_state, __I_SYNC); |
| 272 | } |
| 273 | |
Jeff Layton | d2caa3c5 | 2009-04-02 16:56:37 -0700 | [diff] [blame] | 274 | static bool inode_dirtied_after(struct inode *inode, unsigned long t) |
| 275 | { |
| 276 | bool ret = time_after(inode->dirtied_when, t); |
| 277 | #ifndef CONFIG_64BIT |
| 278 | /* |
| 279 | * For inodes being constantly redirtied, dirtied_when can get stuck. |
| 280 | * It _appears_ to be in the future, but is actually in distant past. |
| 281 | * This test is necessary to prevent such wrapped-around relative times |
| 282 | * from permanently stopping the whole pdflush writeback. |
| 283 | */ |
| 284 | ret = ret && time_before_eq(inode->dirtied_when, jiffies); |
| 285 | #endif |
| 286 | return ret; |
| 287 | } |
| 288 | |
Andrew Morton | c986d1e | 2007-10-16 23:30:34 -0700 | [diff] [blame] | 289 | /* |
Fengguang Wu | 2c13657 | 2007-10-16 23:30:39 -0700 | [diff] [blame] | 290 | * Move expired dirty inodes from @delaying_queue to @dispatch_queue. |
| 291 | */ |
| 292 | static void move_expired_inodes(struct list_head *delaying_queue, |
| 293 | struct list_head *dispatch_queue, |
| 294 | unsigned long *older_than_this) |
| 295 | { |
| 296 | while (!list_empty(delaying_queue)) { |
| 297 | struct inode *inode = list_entry(delaying_queue->prev, |
| 298 | struct inode, i_list); |
| 299 | if (older_than_this && |
Jeff Layton | d2caa3c5 | 2009-04-02 16:56:37 -0700 | [diff] [blame] | 300 | inode_dirtied_after(inode, *older_than_this)) |
Fengguang Wu | 2c13657 | 2007-10-16 23:30:39 -0700 | [diff] [blame] | 301 | break; |
| 302 | list_move(&inode->i_list, dispatch_queue); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Queue all expired dirty inodes for io, eldest first. |
| 308 | */ |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 309 | static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this) |
Fengguang Wu | 2c13657 | 2007-10-16 23:30:39 -0700 | [diff] [blame] | 310 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 311 | list_splice_init(&wb->b_more_io, wb->b_io.prev); |
| 312 | move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this); |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 315 | static int write_inode(struct inode *inode, int sync) |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 316 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 317 | if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) |
| 318 | return inode->i_sb->s_op->write_inode(inode, sync); |
| 319 | return 0; |
Fengguang Wu | 2c13657 | 2007-10-16 23:30:39 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* |
Christoph Hellwig | 01c0319 | 2009-06-08 13:35:40 +0200 | [diff] [blame] | 323 | * Wait for writeback on an inode to complete. |
| 324 | */ |
| 325 | static void inode_wait_for_writeback(struct inode *inode) |
| 326 | { |
| 327 | DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); |
| 328 | wait_queue_head_t *wqh; |
| 329 | |
| 330 | wqh = bit_waitqueue(&inode->i_state, __I_SYNC); |
| 331 | do { |
| 332 | spin_unlock(&inode_lock); |
| 333 | __wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE); |
| 334 | spin_lock(&inode_lock); |
| 335 | } while (inode->i_state & I_SYNC); |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Write out an inode's dirty pages. Called under inode_lock. Either the |
| 340 | * caller has ref on the inode (either via __iget or via syscall against an fd) |
| 341 | * or the inode has I_WILL_FREE set (via generic_forget_inode) |
| 342 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | * If `wait' is set, wait on the writeout. |
| 344 | * |
| 345 | * The whole writeout design is quite complex and fragile. We want to avoid |
| 346 | * starvation of particular inodes when others are being redirtied, prevent |
| 347 | * livelocks, etc. |
| 348 | * |
| 349 | * Called under inode_lock. |
| 350 | */ |
| 351 | static int |
Christoph Hellwig | 01c0319 | 2009-06-08 13:35:40 +0200 | [diff] [blame] | 352 | writeback_single_inode(struct inode *inode, struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | struct address_space *mapping = inode->i_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | int wait = wbc->sync_mode == WB_SYNC_ALL; |
Christoph Hellwig | 01c0319 | 2009-06-08 13:35:40 +0200 | [diff] [blame] | 356 | unsigned dirty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | int ret; |
| 358 | |
Christoph Hellwig | 01c0319 | 2009-06-08 13:35:40 +0200 | [diff] [blame] | 359 | if (!atomic_read(&inode->i_count)) |
| 360 | WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); |
| 361 | else |
| 362 | WARN_ON(inode->i_state & I_WILL_FREE); |
| 363 | |
| 364 | if (inode->i_state & I_SYNC) { |
| 365 | /* |
| 366 | * If this inode is locked for writeback and we are not doing |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 367 | * writeback-for-data-integrity, move it to b_more_io so that |
Christoph Hellwig | 01c0319 | 2009-06-08 13:35:40 +0200 | [diff] [blame] | 368 | * writeback can proceed with the other inodes on s_io. |
| 369 | * |
| 370 | * We'll have another go at writing back this inode when we |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 371 | * completed a full scan of b_io. |
Christoph Hellwig | 01c0319 | 2009-06-08 13:35:40 +0200 | [diff] [blame] | 372 | */ |
| 373 | if (!wait) { |
| 374 | requeue_io(inode); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * It's a data-integrity sync. We must wait. |
| 380 | */ |
| 381 | inode_wait_for_writeback(inode); |
| 382 | } |
| 383 | |
Joern Engel | 1c0eeaf | 2007-10-16 23:30:44 -0700 | [diff] [blame] | 384 | BUG_ON(inode->i_state & I_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
Joern Engel | 1c0eeaf | 2007-10-16 23:30:44 -0700 | [diff] [blame] | 386 | /* Set I_SYNC, reset I_DIRTY */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | dirty = inode->i_state & I_DIRTY; |
Joern Engel | 1c0eeaf | 2007-10-16 23:30:44 -0700 | [diff] [blame] | 388 | inode->i_state |= I_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | inode->i_state &= ~I_DIRTY; |
| 390 | |
| 391 | spin_unlock(&inode_lock); |
| 392 | |
| 393 | ret = do_writepages(mapping, wbc); |
| 394 | |
| 395 | /* Don't write the inode if only I_DIRTY_PAGES was set */ |
| 396 | if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { |
| 397 | int err = write_inode(inode, wait); |
| 398 | if (ret == 0) |
| 399 | ret = err; |
| 400 | } |
| 401 | |
| 402 | if (wait) { |
| 403 | int err = filemap_fdatawait(mapping); |
| 404 | if (ret == 0) |
| 405 | ret = err; |
| 406 | } |
| 407 | |
| 408 | spin_lock(&inode_lock); |
Joern Engel | 1c0eeaf | 2007-10-16 23:30:44 -0700 | [diff] [blame] | 409 | inode->i_state &= ~I_SYNC; |
Wu Fengguang | 84a8924 | 2009-06-16 15:33:17 -0700 | [diff] [blame] | 410 | if (!(inode->i_state & (I_FREEING | I_CLEAR))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | if (!(inode->i_state & I_DIRTY) && |
| 412 | mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { |
| 413 | /* |
| 414 | * We didn't write back all the pages. nfs_writepages() |
| 415 | * sometimes bales out without doing anything. Redirty |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 416 | * the inode; Move it from b_io onto b_more_io/b_dirty. |
Andrew Morton | 1b43ef9 | 2007-10-16 23:30:35 -0700 | [diff] [blame] | 417 | */ |
| 418 | /* |
| 419 | * akpm: if the caller was the kupdate function we put |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 420 | * this inode at the head of b_dirty so it gets first |
Andrew Morton | 1b43ef9 | 2007-10-16 23:30:35 -0700 | [diff] [blame] | 421 | * consideration. Otherwise, move it to the tail, for |
| 422 | * the reasons described there. I'm not really sure |
| 423 | * how much sense this makes. Presumably I had a good |
| 424 | * reasons for doing it this way, and I'd rather not |
| 425 | * muck with it at present. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | */ |
| 427 | if (wbc->for_kupdate) { |
| 428 | /* |
Fengguang Wu | 2c13657 | 2007-10-16 23:30:39 -0700 | [diff] [blame] | 429 | * For the kupdate function we move the inode |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 430 | * to b_more_io so it will get more writeout as |
Fengguang Wu | 2c13657 | 2007-10-16 23:30:39 -0700 | [diff] [blame] | 431 | * soon as the queue becomes uncongested. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | */ |
| 433 | inode->i_state |= I_DIRTY_PAGES; |
Fengguang Wu | 8bc3be2 | 2008-02-04 22:29:36 -0800 | [diff] [blame] | 434 | if (wbc->nr_to_write <= 0) { |
| 435 | /* |
| 436 | * slice used up: queue for next turn |
| 437 | */ |
| 438 | requeue_io(inode); |
| 439 | } else { |
| 440 | /* |
| 441 | * somehow blocked: retry later |
| 442 | */ |
| 443 | redirty_tail(inode); |
| 444 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } else { |
| 446 | /* |
| 447 | * Otherwise fully redirty the inode so that |
| 448 | * other inodes on this superblock will get some |
| 449 | * writeout. Otherwise heavy writing to one |
| 450 | * file would indefinitely suspend writeout of |
| 451 | * all the other files. |
| 452 | */ |
| 453 | inode->i_state |= I_DIRTY_PAGES; |
Andrew Morton | 1b43ef9 | 2007-10-16 23:30:35 -0700 | [diff] [blame] | 454 | redirty_tail(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | } else if (inode->i_state & I_DIRTY) { |
| 457 | /* |
| 458 | * Someone redirtied the inode while were writing back |
| 459 | * the pages. |
| 460 | */ |
Andrew Morton | 6610a0b | 2007-10-16 23:30:32 -0700 | [diff] [blame] | 461 | redirty_tail(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } else if (atomic_read(&inode->i_count)) { |
| 463 | /* |
| 464 | * The inode is clean, inuse |
| 465 | */ |
| 466 | list_move(&inode->i_list, &inode_in_use); |
| 467 | } else { |
| 468 | /* |
| 469 | * The inode is clean, unused |
| 470 | */ |
| 471 | list_move(&inode->i_list, &inode_unused); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
Joern Engel | 1c0eeaf | 2007-10-16 23:30:44 -0700 | [diff] [blame] | 474 | inode_sync_complete(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | return ret; |
| 476 | } |
| 477 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 478 | /* |
| 479 | * For WB_SYNC_NONE writeback, the caller does not have the sb pinned |
| 480 | * before calling writeback. So make sure that we do pin it, so it doesn't |
| 481 | * go away while we are writing inodes from it. |
| 482 | * |
| 483 | * Returns 0 if the super was successfully pinned (or pinning wasn't needed), |
| 484 | * 1 if we failed. |
| 485 | */ |
| 486 | static int pin_sb_for_writeback(struct writeback_control *wbc, |
| 487 | struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 489 | struct super_block *sb = inode->i_sb; |
| 490 | |
| 491 | /* |
| 492 | * Caller must already hold the ref for this |
| 493 | */ |
| 494 | if (wbc->sync_mode == WB_SYNC_ALL) { |
| 495 | WARN_ON(!rwsem_is_locked(&sb->s_umount)); |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | spin_lock(&sb_lock); |
| 500 | sb->s_count++; |
| 501 | if (down_read_trylock(&sb->s_umount)) { |
| 502 | if (sb->s_root) { |
| 503 | spin_unlock(&sb_lock); |
| 504 | return 0; |
| 505 | } |
| 506 | /* |
| 507 | * umounted, drop rwsem again and fall through to failure |
| 508 | */ |
| 509 | up_read(&sb->s_umount); |
| 510 | } |
| 511 | |
| 512 | sb->s_count--; |
| 513 | spin_unlock(&sb_lock); |
| 514 | return 1; |
| 515 | } |
| 516 | |
| 517 | static void unpin_sb_for_writeback(struct writeback_control *wbc, |
| 518 | struct inode *inode) |
| 519 | { |
| 520 | struct super_block *sb = inode->i_sb; |
| 521 | |
| 522 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 523 | return; |
| 524 | |
| 525 | up_read(&sb->s_umount); |
| 526 | put_super(sb); |
| 527 | } |
| 528 | |
| 529 | static void writeback_inodes_wb(struct bdi_writeback *wb, |
| 530 | struct writeback_control *wbc) |
| 531 | { |
| 532 | struct super_block *sb = wbc->sb; |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 533 | const int is_blkdev_sb = sb_is_blkdev_sb(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | const unsigned long start = jiffies; /* livelock avoidance */ |
| 535 | |
Hans Reiser | ae8547b | 2008-05-07 15:48:57 +0300 | [diff] [blame] | 536 | spin_lock(&inode_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 538 | if (!wbc->for_kupdate || list_empty(&wb->b_io)) |
| 539 | queue_io(wb, wbc->older_than_this); |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 540 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 541 | while (!list_empty(&wb->b_io)) { |
| 542 | struct inode *inode = list_entry(wb->b_io.prev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | struct inode, i_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | long pages_skipped; |
| 545 | |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 546 | /* |
| 547 | * super block given and doesn't match, skip this inode |
| 548 | */ |
| 549 | if (sb && sb != inode->i_sb) { |
| 550 | redirty_tail(inode); |
| 551 | continue; |
| 552 | } |
| 553 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 554 | if (!bdi_cap_writeback_dirty(wb->bdi)) { |
Andrew Morton | 9852a0e7 | 2007-10-16 23:30:33 -0700 | [diff] [blame] | 555 | redirty_tail(inode); |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 556 | if (is_blkdev_sb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | /* |
| 558 | * Dirty memory-backed blockdev: the ramdisk |
| 559 | * driver does this. Skip just this inode |
| 560 | */ |
| 561 | continue; |
| 562 | } |
| 563 | /* |
| 564 | * Dirty memory-backed inode against a filesystem other |
| 565 | * than the kernel-internal bdev filesystem. Skip the |
| 566 | * entire superblock. |
| 567 | */ |
| 568 | break; |
| 569 | } |
| 570 | |
Wu Fengguang | 84a8924 | 2009-06-16 15:33:17 -0700 | [diff] [blame] | 571 | if (inode->i_state & (I_NEW | I_WILL_FREE)) { |
Nick Piggin | 7ef0d73 | 2009-03-12 14:31:38 -0700 | [diff] [blame] | 572 | requeue_io(inode); |
| 573 | continue; |
| 574 | } |
| 575 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 576 | if (wbc->nonblocking && bdi_write_congested(wb->bdi)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | wbc->encountered_congestion = 1; |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 578 | if (!is_blkdev_sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | break; /* Skip a congested fs */ |
Ken Chen | 0e0f4fc | 2007-10-16 23:30:38 -0700 | [diff] [blame] | 580 | requeue_io(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | continue; /* Skip a congested blockdev */ |
| 582 | } |
| 583 | |
Jeff Layton | d2caa3c5 | 2009-04-02 16:56:37 -0700 | [diff] [blame] | 584 | /* |
| 585 | * Was this inode dirtied after sync_sb_inodes was called? |
| 586 | * This keeps sync from extra jobs and livelock. |
| 587 | */ |
| 588 | if (inode_dirtied_after(inode, start)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | break; |
| 590 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 591 | if (pin_sb_for_writeback(wbc, inode)) { |
| 592 | requeue_io(inode); |
| 593 | continue; |
| 594 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
Wu Fengguang | 84a8924 | 2009-06-16 15:33:17 -0700 | [diff] [blame] | 596 | BUG_ON(inode->i_state & (I_FREEING | I_CLEAR)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | __iget(inode); |
| 598 | pages_skipped = wbc->pages_skipped; |
Christoph Hellwig | 01c0319 | 2009-06-08 13:35:40 +0200 | [diff] [blame] | 599 | writeback_single_inode(inode, wbc); |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 600 | unpin_sb_for_writeback(wbc, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | if (wbc->pages_skipped != pages_skipped) { |
| 602 | /* |
| 603 | * writeback is not making progress due to locked |
| 604 | * buffers. Skip this inode for now. |
| 605 | */ |
Andrew Morton | f57b9b7 | 2007-10-16 23:30:34 -0700 | [diff] [blame] | 606 | redirty_tail(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | } |
| 608 | spin_unlock(&inode_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | iput(inode); |
OGAWA Hirofumi | 4ffc844 | 2006-03-25 03:07:44 -0800 | [diff] [blame] | 610 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | spin_lock(&inode_lock); |
Fengguang Wu | 8bc3be2 | 2008-02-04 22:29:36 -0800 | [diff] [blame] | 612 | if (wbc->nr_to_write <= 0) { |
| 613 | wbc->more_io = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | break; |
Fengguang Wu | 8bc3be2 | 2008-02-04 22:29:36 -0800 | [diff] [blame] | 615 | } |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 616 | if (!list_empty(&wb->b_more_io)) |
Fengguang Wu | 8bc3be2 | 2008-02-04 22:29:36 -0800 | [diff] [blame] | 617 | wbc->more_io = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
Nick Piggin | 38f2197 | 2009-01-06 14:40:25 -0800 | [diff] [blame] | 619 | |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 620 | spin_unlock(&inode_lock); |
| 621 | /* Leave any unwritten inodes on b_io */ |
| 622 | } |
| 623 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 624 | void writeback_inodes_wbc(struct writeback_control *wbc) |
| 625 | { |
| 626 | struct backing_dev_info *bdi = wbc->bdi; |
| 627 | |
| 628 | writeback_inodes_wb(&bdi->wb, wbc); |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * The maximum number of pages to writeout in a single bdi flush/kupdate |
| 633 | * operation. We do this so we don't hold I_SYNC against an inode for |
| 634 | * enormous amounts of time, which would block a userspace task which has |
| 635 | * been forced to throttle against that inode. Also, the code reevaluates |
| 636 | * the dirty each time it has written this many pages. |
| 637 | */ |
| 638 | #define MAX_WRITEBACK_PAGES 1024 |
| 639 | |
| 640 | static inline bool over_bground_thresh(void) |
| 641 | { |
| 642 | unsigned long background_thresh, dirty_thresh; |
| 643 | |
| 644 | get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL); |
| 645 | |
| 646 | return (global_page_state(NR_FILE_DIRTY) + |
| 647 | global_page_state(NR_UNSTABLE_NFS) >= background_thresh); |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Explicit flushing or periodic writeback of "old" data. |
| 652 | * |
| 653 | * Define "old": the first time one of an inode's pages is dirtied, we mark the |
| 654 | * dirtying-time in the inode's address_space. So this periodic writeback code |
| 655 | * just walks the superblock inode list, writing back any inodes which are |
| 656 | * older than a specific point in time. |
| 657 | * |
| 658 | * Try to run once per dirty_writeback_interval. But if a writeback event |
| 659 | * takes longer than a dirty_writeback_interval interval, then leave a |
| 660 | * one-second gap. |
| 661 | * |
| 662 | * older_than_this takes precedence over nr_to_write. So we'll only write back |
| 663 | * all dirty pages if they are all attached to "old" mappings. |
| 664 | */ |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 665 | static long wb_writeback(struct bdi_writeback *wb, |
| 666 | struct wb_writeback_args *args) |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 667 | { |
| 668 | struct writeback_control wbc = { |
| 669 | .bdi = wb->bdi, |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 670 | .sb = args->sb, |
| 671 | .sync_mode = args->sync_mode, |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 672 | .older_than_this = NULL, |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 673 | .for_kupdate = args->for_kupdate, |
| 674 | .range_cyclic = args->range_cyclic, |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 675 | }; |
| 676 | unsigned long oldest_jif; |
| 677 | long wrote = 0; |
| 678 | |
| 679 | if (wbc.for_kupdate) { |
| 680 | wbc.older_than_this = &oldest_jif; |
| 681 | oldest_jif = jiffies - |
| 682 | msecs_to_jiffies(dirty_expire_interval * 10); |
| 683 | } |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 684 | if (!wbc.range_cyclic) { |
| 685 | wbc.range_start = 0; |
| 686 | wbc.range_end = LLONG_MAX; |
| 687 | } |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 688 | |
| 689 | for (;;) { |
| 690 | /* |
| 691 | * Don't flush anything for non-integrity writeback where |
| 692 | * no nr_pages was given |
| 693 | */ |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 694 | if (!args->for_kupdate && args->nr_pages <= 0 && |
| 695 | args->sync_mode == WB_SYNC_NONE) |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 696 | break; |
| 697 | |
| 698 | /* |
| 699 | * If no specific pages were given and this is just a |
| 700 | * periodic background writeout and we are below the |
| 701 | * background dirty threshold, don't do anything |
| 702 | */ |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 703 | if (args->for_kupdate && args->nr_pages <= 0 && |
| 704 | !over_bground_thresh()) |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 705 | break; |
| 706 | |
| 707 | wbc.more_io = 0; |
| 708 | wbc.encountered_congestion = 0; |
| 709 | wbc.nr_to_write = MAX_WRITEBACK_PAGES; |
| 710 | wbc.pages_skipped = 0; |
| 711 | writeback_inodes_wb(wb, &wbc); |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 712 | args->nr_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write; |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 713 | wrote += MAX_WRITEBACK_PAGES - wbc.nr_to_write; |
| 714 | |
| 715 | /* |
| 716 | * If we ran out of stuff to write, bail unless more_io got set |
| 717 | */ |
| 718 | if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) { |
| 719 | if (wbc.more_io && !wbc.for_kupdate) |
| 720 | continue; |
| 721 | break; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | return wrote; |
| 726 | } |
| 727 | |
| 728 | /* |
| 729 | * Return the next bdi_work struct that hasn't been processed by this |
| 730 | * wb thread yet |
| 731 | */ |
| 732 | static struct bdi_work *get_next_work_item(struct backing_dev_info *bdi, |
| 733 | struct bdi_writeback *wb) |
| 734 | { |
| 735 | struct bdi_work *work, *ret = NULL; |
| 736 | |
| 737 | rcu_read_lock(); |
| 738 | |
| 739 | list_for_each_entry_rcu(work, &bdi->work_list, list) { |
| 740 | if (!test_and_clear_bit(wb->nr, &work->seen)) |
| 741 | continue; |
| 742 | |
| 743 | ret = work; |
| 744 | break; |
| 745 | } |
| 746 | |
| 747 | rcu_read_unlock(); |
| 748 | return ret; |
| 749 | } |
| 750 | |
| 751 | static long wb_check_old_data_flush(struct bdi_writeback *wb) |
| 752 | { |
| 753 | unsigned long expired; |
| 754 | long nr_pages; |
| 755 | |
| 756 | expired = wb->last_old_flush + |
| 757 | msecs_to_jiffies(dirty_writeback_interval * 10); |
| 758 | if (time_before(jiffies, expired)) |
| 759 | return 0; |
| 760 | |
| 761 | wb->last_old_flush = jiffies; |
| 762 | nr_pages = global_page_state(NR_FILE_DIRTY) + |
| 763 | global_page_state(NR_UNSTABLE_NFS) + |
| 764 | (inodes_stat.nr_inodes - inodes_stat.nr_unused); |
| 765 | |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 766 | if (nr_pages) { |
| 767 | struct wb_writeback_args args = { |
| 768 | .nr_pages = nr_pages, |
| 769 | .sync_mode = WB_SYNC_NONE, |
| 770 | .for_kupdate = 1, |
| 771 | .range_cyclic = 1, |
| 772 | }; |
| 773 | |
| 774 | return wb_writeback(wb, &args); |
| 775 | } |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | /* |
| 781 | * Retrieve work items and do the writeback they describe |
| 782 | */ |
| 783 | long wb_do_writeback(struct bdi_writeback *wb, int force_wait) |
| 784 | { |
| 785 | struct backing_dev_info *bdi = wb->bdi; |
| 786 | struct bdi_work *work; |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 787 | long wrote = 0; |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 788 | |
| 789 | while ((work = get_next_work_item(bdi, wb)) != NULL) { |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 790 | struct wb_writeback_args args = work->args; |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 791 | |
| 792 | /* |
| 793 | * Override sync mode, in case we must wait for completion |
| 794 | */ |
| 795 | if (force_wait) |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 796 | work->args.sync_mode = args.sync_mode = WB_SYNC_ALL; |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 797 | |
| 798 | /* |
| 799 | * If this isn't a data integrity operation, just notify |
| 800 | * that we have seen this work and we are now starting it. |
| 801 | */ |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 802 | if (args.sync_mode == WB_SYNC_NONE) |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 803 | wb_clear_pending(wb, work); |
| 804 | |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 805 | wrote += wb_writeback(wb, &args); |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 806 | |
| 807 | /* |
| 808 | * This is a data integrity writeback, so only do the |
| 809 | * notification when we have completed the work. |
| 810 | */ |
Jens Axboe | c4a77a6 | 2009-09-16 15:18:25 +0200 | [diff] [blame] | 811 | if (args.sync_mode == WB_SYNC_ALL) |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 812 | wb_clear_pending(wb, work); |
| 813 | } |
| 814 | |
| 815 | /* |
| 816 | * Check for periodic writeback, kupdated() style |
| 817 | */ |
| 818 | wrote += wb_check_old_data_flush(wb); |
| 819 | |
| 820 | return wrote; |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | * Handle writeback of dirty data for the device backed by this bdi. Also |
| 825 | * wakes up periodically and does kupdated style flushing. |
| 826 | */ |
| 827 | int bdi_writeback_task(struct bdi_writeback *wb) |
| 828 | { |
| 829 | unsigned long last_active = jiffies; |
| 830 | unsigned long wait_jiffies = -1UL; |
| 831 | long pages_written; |
| 832 | |
| 833 | while (!kthread_should_stop()) { |
| 834 | pages_written = wb_do_writeback(wb, 0); |
| 835 | |
| 836 | if (pages_written) |
| 837 | last_active = jiffies; |
| 838 | else if (wait_jiffies != -1UL) { |
| 839 | unsigned long max_idle; |
| 840 | |
| 841 | /* |
| 842 | * Longest period of inactivity that we tolerate. If we |
| 843 | * see dirty data again later, the task will get |
| 844 | * recreated automatically. |
| 845 | */ |
| 846 | max_idle = max(5UL * 60 * HZ, wait_jiffies); |
| 847 | if (time_after(jiffies, max_idle + last_active)) |
| 848 | break; |
| 849 | } |
| 850 | |
| 851 | wait_jiffies = msecs_to_jiffies(dirty_writeback_interval * 10); |
| 852 | set_current_state(TASK_INTERRUPTIBLE); |
| 853 | schedule_timeout(wait_jiffies); |
| 854 | try_to_freeze(); |
| 855 | } |
| 856 | |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | /* |
Jens Axboe | f11fcae | 2009-09-15 09:53:35 +0200 | [diff] [blame] | 861 | * Schedule writeback for all backing devices. Can only be used for |
| 862 | * WB_SYNC_NONE writeback, WB_SYNC_ALL should use bdi_start_writeback() |
| 863 | * and pass in the superblock. |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 864 | */ |
| 865 | static void bdi_writeback_all(struct writeback_control *wbc) |
| 866 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 867 | struct backing_dev_info *bdi; |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 868 | |
Jens Axboe | f11fcae | 2009-09-15 09:53:35 +0200 | [diff] [blame] | 869 | WARN_ON(wbc->sync_mode == WB_SYNC_ALL); |
| 870 | |
Jens Axboe | cfc4ba5 | 2009-09-14 13:12:40 +0200 | [diff] [blame^] | 871 | rcu_read_lock(); |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 872 | |
Jens Axboe | cfc4ba5 | 2009-09-14 13:12:40 +0200 | [diff] [blame^] | 873 | list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 874 | if (!bdi_has_dirty_io(bdi)) |
| 875 | continue; |
| 876 | |
Jens Axboe | f11fcae | 2009-09-15 09:53:35 +0200 | [diff] [blame] | 877 | bdi_alloc_queue_work(bdi, wbc); |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 878 | } |
| 879 | |
Jens Axboe | cfc4ba5 | 2009-09-14 13:12:40 +0200 | [diff] [blame^] | 880 | rcu_read_unlock(); |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | /* |
| 884 | * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back |
| 885 | * the whole world. |
| 886 | */ |
| 887 | void wakeup_flusher_threads(long nr_pages) |
| 888 | { |
| 889 | struct writeback_control wbc = { |
| 890 | .sync_mode = WB_SYNC_NONE, |
| 891 | .older_than_this = NULL, |
| 892 | .range_cyclic = 1, |
| 893 | }; |
| 894 | |
| 895 | if (nr_pages == 0) |
| 896 | nr_pages = global_page_state(NR_FILE_DIRTY) + |
| 897 | global_page_state(NR_UNSTABLE_NFS); |
| 898 | wbc.nr_to_write = nr_pages; |
| 899 | bdi_writeback_all(&wbc); |
| 900 | } |
| 901 | |
| 902 | static noinline void block_dump___mark_inode_dirty(struct inode *inode) |
| 903 | { |
| 904 | if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { |
| 905 | struct dentry *dentry; |
| 906 | const char *name = "?"; |
| 907 | |
| 908 | dentry = d_find_alias(inode); |
| 909 | if (dentry) { |
| 910 | spin_lock(&dentry->d_lock); |
| 911 | name = (const char *) dentry->d_name.name; |
| 912 | } |
| 913 | printk(KERN_DEBUG |
| 914 | "%s(%d): dirtied inode %lu (%s) on %s\n", |
| 915 | current->comm, task_pid_nr(current), inode->i_ino, |
| 916 | name, inode->i_sb->s_id); |
| 917 | if (dentry) { |
| 918 | spin_unlock(&dentry->d_lock); |
| 919 | dput(dentry); |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * __mark_inode_dirty - internal function |
| 926 | * @inode: inode to mark |
| 927 | * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) |
| 928 | * Mark an inode as dirty. Callers should use mark_inode_dirty or |
| 929 | * mark_inode_dirty_sync. |
| 930 | * |
| 931 | * Put the inode on the super block's dirty list. |
| 932 | * |
| 933 | * CAREFUL! We mark it dirty unconditionally, but move it onto the |
| 934 | * dirty list only if it is hashed or if it refers to a blockdev. |
| 935 | * If it was not hashed, it will never be added to the dirty list |
| 936 | * even if it is later hashed, as it will have been marked dirty already. |
| 937 | * |
| 938 | * In short, make sure you hash any inodes _before_ you start marking |
| 939 | * them dirty. |
| 940 | * |
| 941 | * This function *must* be atomic for the I_DIRTY_PAGES case - |
| 942 | * set_page_dirty() is called under spinlock in several places. |
| 943 | * |
| 944 | * Note that for blockdevs, inode->dirtied_when represents the dirtying time of |
| 945 | * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of |
| 946 | * the kernel-internal blockdev inode represents the dirtying time of the |
| 947 | * blockdev's pages. This is why for I_DIRTY_PAGES we always use |
| 948 | * page->mapping->host, so the page-dirtying time is recorded in the internal |
| 949 | * blockdev inode. |
| 950 | */ |
| 951 | void __mark_inode_dirty(struct inode *inode, int flags) |
| 952 | { |
| 953 | struct super_block *sb = inode->i_sb; |
| 954 | |
| 955 | /* |
| 956 | * Don't do this for I_DIRTY_PAGES - that doesn't actually |
| 957 | * dirty the inode itself |
| 958 | */ |
| 959 | if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { |
| 960 | if (sb->s_op->dirty_inode) |
| 961 | sb->s_op->dirty_inode(inode); |
| 962 | } |
| 963 | |
| 964 | /* |
| 965 | * make sure that changes are seen by all cpus before we test i_state |
| 966 | * -- mikulas |
| 967 | */ |
| 968 | smp_mb(); |
| 969 | |
| 970 | /* avoid the locking if we can */ |
| 971 | if ((inode->i_state & flags) == flags) |
| 972 | return; |
| 973 | |
| 974 | if (unlikely(block_dump)) |
| 975 | block_dump___mark_inode_dirty(inode); |
| 976 | |
| 977 | spin_lock(&inode_lock); |
| 978 | if ((inode->i_state & flags) != flags) { |
| 979 | const int was_dirty = inode->i_state & I_DIRTY; |
| 980 | |
| 981 | inode->i_state |= flags; |
| 982 | |
| 983 | /* |
| 984 | * If the inode is being synced, just update its dirty state. |
| 985 | * The unlocker will place the inode on the appropriate |
| 986 | * superblock list, based upon its state. |
| 987 | */ |
| 988 | if (inode->i_state & I_SYNC) |
| 989 | goto out; |
| 990 | |
| 991 | /* |
| 992 | * Only add valid (hashed) inodes to the superblock's |
| 993 | * dirty list. Add blockdev inodes as well. |
| 994 | */ |
| 995 | if (!S_ISBLK(inode->i_mode)) { |
| 996 | if (hlist_unhashed(&inode->i_hash)) |
| 997 | goto out; |
| 998 | } |
| 999 | if (inode->i_state & (I_FREEING|I_CLEAR)) |
| 1000 | goto out; |
| 1001 | |
| 1002 | /* |
| 1003 | * If the inode was already on b_dirty/b_io/b_more_io, don't |
| 1004 | * reposition it (that would break b_dirty time-ordering). |
| 1005 | */ |
| 1006 | if (!was_dirty) { |
| 1007 | struct bdi_writeback *wb = &inode_to_bdi(inode)->wb; |
Jens Axboe | 500b067 | 2009-09-09 09:10:25 +0200 | [diff] [blame] | 1008 | struct backing_dev_info *bdi = wb->bdi; |
| 1009 | |
| 1010 | if (bdi_cap_writeback_dirty(bdi) && |
| 1011 | !test_bit(BDI_registered, &bdi->state)) { |
| 1012 | WARN_ON(1); |
| 1013 | printk(KERN_ERR "bdi-%s not registered\n", |
| 1014 | bdi->name); |
| 1015 | } |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 1016 | |
| 1017 | inode->dirtied_when = jiffies; |
| 1018 | list_move(&inode->i_list, &wb->b_dirty); |
| 1019 | } |
| 1020 | } |
| 1021 | out: |
| 1022 | spin_unlock(&inode_lock); |
| 1023 | } |
| 1024 | EXPORT_SYMBOL(__mark_inode_dirty); |
| 1025 | |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 1026 | /* |
| 1027 | * Write out a superblock's list of dirty inodes. A wait will be performed |
| 1028 | * upon no inodes, all inodes or the final one, depending upon sync_mode. |
| 1029 | * |
| 1030 | * If older_than_this is non-NULL, then only write out inodes which |
| 1031 | * had their first dirtying at a time earlier than *older_than_this. |
| 1032 | * |
| 1033 | * If we're a pdlfush thread, then implement pdflush collision avoidance |
| 1034 | * against the entire list. |
| 1035 | * |
| 1036 | * If `bdi' is non-zero then we're being asked to writeback a specific queue. |
| 1037 | * This function assumes that the blockdev superblock's inodes are backed by |
| 1038 | * a variety of queues, so all inodes are searched. For other superblocks, |
| 1039 | * assume that all inodes are backed by the same queue. |
| 1040 | * |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 1041 | * The inodes to be written are parked on bdi->b_io. They are moved back onto |
| 1042 | * bdi->b_dirty as they are selected for writing. This way, none can be missed |
| 1043 | * on the writer throttling path, and we get decent balancing between many |
| 1044 | * throttled threads: we don't want them all piling up on inode_sync_wait. |
| 1045 | */ |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 1046 | static void wait_sb_inodes(struct writeback_control *wbc) |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 1047 | { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 1048 | struct inode *inode, *old_inode = NULL; |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 1049 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 1050 | /* |
| 1051 | * We need to be protected against the filesystem going from |
| 1052 | * r/o to r/w or vice versa. |
| 1053 | */ |
| 1054 | WARN_ON(!rwsem_is_locked(&wbc->sb->s_umount)); |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 1055 | |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 1056 | spin_lock(&inode_lock); |
| 1057 | |
| 1058 | /* |
| 1059 | * Data integrity sync. Must wait for all pages under writeback, |
| 1060 | * because there may have been pages dirtied before our sync |
| 1061 | * call, but which had writeout started before we write it out. |
| 1062 | * In which case, the inode may not be on the dirty list, but |
| 1063 | * we still have to wait for that writeout. |
| 1064 | */ |
| 1065 | list_for_each_entry(inode, &wbc->sb->s_inodes, i_sb_list) { |
| 1066 | struct address_space *mapping; |
| 1067 | |
| 1068 | if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW)) |
| 1069 | continue; |
| 1070 | mapping = inode->i_mapping; |
| 1071 | if (mapping->nrpages == 0) |
| 1072 | continue; |
| 1073 | __iget(inode); |
| 1074 | spin_unlock(&inode_lock); |
| 1075 | /* |
| 1076 | * We hold a reference to 'inode' so it couldn't have |
| 1077 | * been removed from s_inodes list while we dropped the |
| 1078 | * inode_lock. We cannot iput the inode now as we can |
| 1079 | * be holding the last reference and we cannot iput it |
| 1080 | * under inode_lock. So we keep the reference and iput |
| 1081 | * it later. |
| 1082 | */ |
| 1083 | iput(old_inode); |
| 1084 | old_inode = inode; |
| 1085 | |
| 1086 | filemap_fdatawait(mapping); |
| 1087 | |
| 1088 | cond_resched(); |
Nick Piggin | 38f2197 | 2009-01-06 14:40:25 -0800 | [diff] [blame] | 1089 | |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 1090 | spin_lock(&inode_lock); |
Jens Axboe | 66f3b8e | 2009-09-02 09:19:46 +0200 | [diff] [blame] | 1091 | } |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 1092 | spin_unlock(&inode_lock); |
| 1093 | iput(old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
Jens Axboe | d8a8559 | 2009-09-02 12:34:32 +0200 | [diff] [blame] | 1096 | /** |
| 1097 | * writeback_inodes_sb - writeback dirty inodes from given super_block |
| 1098 | * @sb: the superblock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | * |
Jens Axboe | d8a8559 | 2009-09-02 12:34:32 +0200 | [diff] [blame] | 1100 | * Start writeback on some inodes on this super_block. No guarantees are made |
| 1101 | * on how many (if any) will be written, and this function does not wait |
| 1102 | * for IO completion of submitted IO. The number of pages submitted is |
| 1103 | * returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | */ |
Jens Axboe | d8a8559 | 2009-09-02 12:34:32 +0200 | [diff] [blame] | 1105 | long writeback_inodes_sb(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | { |
| 1107 | struct writeback_control wbc = { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 1108 | .sb = sb, |
Jens Axboe | d8a8559 | 2009-09-02 12:34:32 +0200 | [diff] [blame] | 1109 | .sync_mode = WB_SYNC_NONE, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 1110 | .range_start = 0, |
| 1111 | .range_end = LLONG_MAX, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | }; |
Jens Axboe | d8a8559 | 2009-09-02 12:34:32 +0200 | [diff] [blame] | 1113 | unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY); |
| 1114 | unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS); |
| 1115 | long nr_to_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | |
Jens Axboe | d8a8559 | 2009-09-02 12:34:32 +0200 | [diff] [blame] | 1117 | nr_to_write = nr_dirty + nr_unstable + |
Nick Piggin | 38f2197 | 2009-01-06 14:40:25 -0800 | [diff] [blame] | 1118 | (inodes_stat.nr_inodes - inodes_stat.nr_unused); |
Nick Piggin | 38f2197 | 2009-01-06 14:40:25 -0800 | [diff] [blame] | 1119 | |
Jens Axboe | d8a8559 | 2009-09-02 12:34:32 +0200 | [diff] [blame] | 1120 | wbc.nr_to_write = nr_to_write; |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 1121 | bdi_writeback_all(&wbc); |
Jens Axboe | d8a8559 | 2009-09-02 12:34:32 +0200 | [diff] [blame] | 1122 | return nr_to_write - wbc.nr_to_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | } |
Jens Axboe | d8a8559 | 2009-09-02 12:34:32 +0200 | [diff] [blame] | 1124 | EXPORT_SYMBOL(writeback_inodes_sb); |
| 1125 | |
| 1126 | /** |
| 1127 | * sync_inodes_sb - sync sb inode pages |
| 1128 | * @sb: the superblock |
| 1129 | * |
| 1130 | * This function writes and waits on any dirty inode belonging to this |
| 1131 | * super_block. The number of pages synced is returned. |
| 1132 | */ |
| 1133 | long sync_inodes_sb(struct super_block *sb) |
| 1134 | { |
| 1135 | struct writeback_control wbc = { |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 1136 | .sb = sb, |
Jens Axboe | f11fcae | 2009-09-15 09:53:35 +0200 | [diff] [blame] | 1137 | .bdi = sb->s_bdi, |
Jens Axboe | d8a8559 | 2009-09-02 12:34:32 +0200 | [diff] [blame] | 1138 | .sync_mode = WB_SYNC_ALL, |
| 1139 | .range_start = 0, |
| 1140 | .range_end = LLONG_MAX, |
| 1141 | }; |
| 1142 | long nr_to_write = LONG_MAX; /* doesn't actually matter */ |
| 1143 | |
| 1144 | wbc.nr_to_write = nr_to_write; |
Jens Axboe | f11fcae | 2009-09-15 09:53:35 +0200 | [diff] [blame] | 1145 | bdi_start_writeback(&wbc); |
Jens Axboe | 03ba378 | 2009-09-09 09:08:54 +0200 | [diff] [blame] | 1146 | wait_sb_inodes(&wbc); |
Jens Axboe | d8a8559 | 2009-09-02 12:34:32 +0200 | [diff] [blame] | 1147 | return nr_to_write - wbc.nr_to_write; |
| 1148 | } |
| 1149 | EXPORT_SYMBOL(sync_inodes_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | /** |
Andrea Arcangeli | 7f04c26 | 2005-10-30 15:03:05 -0800 | [diff] [blame] | 1152 | * write_inode_now - write an inode to disk |
| 1153 | * @inode: inode to write to disk |
| 1154 | * @sync: whether the write should be synchronous or not |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | * |
Andrea Arcangeli | 7f04c26 | 2005-10-30 15:03:05 -0800 | [diff] [blame] | 1156 | * This function commits an inode to disk immediately if it is dirty. This is |
| 1157 | * primarily needed by knfsd. |
| 1158 | * |
| 1159 | * The caller must either have a ref on the inode or must have set I_WILL_FREE. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | int write_inode_now(struct inode *inode, int sync) |
| 1162 | { |
| 1163 | int ret; |
| 1164 | struct writeback_control wbc = { |
| 1165 | .nr_to_write = LONG_MAX, |
Mike Galbraith | 18914b1 | 2008-02-08 04:20:23 -0800 | [diff] [blame] | 1166 | .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 1167 | .range_start = 0, |
| 1168 | .range_end = LLONG_MAX, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | }; |
| 1170 | |
| 1171 | if (!mapping_cap_writeback_dirty(inode->i_mapping)) |
Andrew Morton | 49364ce | 2005-11-07 00:59:15 -0800 | [diff] [blame] | 1172 | wbc.nr_to_write = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | |
| 1174 | might_sleep(); |
| 1175 | spin_lock(&inode_lock); |
Christoph Hellwig | 01c0319 | 2009-06-08 13:35:40 +0200 | [diff] [blame] | 1176 | ret = writeback_single_inode(inode, &wbc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | spin_unlock(&inode_lock); |
| 1178 | if (sync) |
Joern Engel | 1c0eeaf | 2007-10-16 23:30:44 -0700 | [diff] [blame] | 1179 | inode_sync_wait(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | return ret; |
| 1181 | } |
| 1182 | EXPORT_SYMBOL(write_inode_now); |
| 1183 | |
| 1184 | /** |
| 1185 | * sync_inode - write an inode and its pages to disk. |
| 1186 | * @inode: the inode to sync |
| 1187 | * @wbc: controls the writeback mode |
| 1188 | * |
| 1189 | * sync_inode() will write an inode and its pages to disk. It will also |
| 1190 | * correctly update the inode on its superblock's dirty inode lists and will |
| 1191 | * update inode->i_state. |
| 1192 | * |
| 1193 | * The caller must have a ref on the inode. |
| 1194 | */ |
| 1195 | int sync_inode(struct inode *inode, struct writeback_control *wbc) |
| 1196 | { |
| 1197 | int ret; |
| 1198 | |
| 1199 | spin_lock(&inode_lock); |
Christoph Hellwig | 01c0319 | 2009-06-08 13:35:40 +0200 | [diff] [blame] | 1200 | ret = writeback_single_inode(inode, wbc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | spin_unlock(&inode_lock); |
| 1202 | return ret; |
| 1203 | } |
| 1204 | EXPORT_SYMBOL(sync_inode); |