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