blob: 783ed44c7cfec59cf9f52e0d2b8c1b45313aa41c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Camie1f8e872008-10-15 22:01:59 -070011 * 10Apr2002 Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Split out of fs/inode.c
13 * Additions for address_space-based writeback
14 */
15
16#include <linux/kernel.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020017#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/spinlock.h>
19#include <linux/sched.h>
20#include <linux/fs.h>
21#include <linux/mm.h>
Jens Axboe03ba3782009-09-09 09:08:54 +020022#include <linux/kthread.h>
23#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/writeback.h>
25#include <linux/blkdev.h>
26#include <linux/backing-dev.h>
27#include <linux/buffer_head.h>
David Howells07f3f052006-09-30 20:52:18 +020028#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Jens Axboe66f3b8e2009-09-02 09:19:46 +020030#define inode_to_bdi(inode) ((inode)->i_mapping->backing_dev_info)
Adrian Bunkf11b00f2008-04-29 00:58:56 -070031
Jens Axboe03ba3782009-09-09 09:08:54 +020032/*
Jens Axboed0bceac2009-05-18 08:20:32 +020033 * We don't actually have pdflush, but this one is exported though /proc...
34 */
35int nr_pdflush_threads;
36
37/*
Jens Axboe03ba3782009-09-09 09:08:54 +020038 * Work items for the bdi_writeback threads
Adrian Bunkf11b00f2008-04-29 00:58:56 -070039 */
Jens Axboe03ba3782009-09-09 09:08:54 +020040struct bdi_work {
41 struct list_head list;
42 struct list_head wait_list;
43 struct rcu_head rcu_head;
44
45 unsigned long seen;
46 atomic_t pending;
47
48 struct super_block *sb;
49 unsigned long nr_pages;
50 enum writeback_sync_modes sync_mode;
51
52 unsigned long state;
53};
54
55enum {
56 WS_USED_B = 0,
57 WS_ONSTACK_B,
58};
59
60#define WS_USED (1 << WS_USED_B)
61#define WS_ONSTACK (1 << WS_ONSTACK_B)
62
63static inline bool bdi_work_on_stack(struct bdi_work *work)
Adrian Bunkf11b00f2008-04-29 00:58:56 -070064{
Jens Axboe03ba3782009-09-09 09:08:54 +020065 return test_bit(WS_ONSTACK_B, &work->state);
66}
67
68static inline void bdi_work_init(struct bdi_work *work,
69 struct writeback_control *wbc)
70{
71 INIT_RCU_HEAD(&work->rcu_head);
72 work->sb = wbc->sb;
73 work->nr_pages = wbc->nr_to_write;
74 work->sync_mode = wbc->sync_mode;
75 work->state = WS_USED;
76}
77
Adrian Bunkf11b00f2008-04-29 00:58:56 -070078/**
79 * writeback_in_progress - determine whether there is writeback in progress
80 * @bdi: the device's backing_dev_info structure.
81 *
Jens Axboe03ba3782009-09-09 09:08:54 +020082 * Determine whether there is writeback waiting to be handled against a
83 * backing device.
Adrian Bunkf11b00f2008-04-29 00:58:56 -070084 */
85int writeback_in_progress(struct backing_dev_info *bdi)
86{
Jens Axboe03ba3782009-09-09 09:08:54 +020087 return !list_empty(&bdi->work_list);
Adrian Bunkf11b00f2008-04-29 00:58:56 -070088}
89
Jens Axboe03ba3782009-09-09 09:08:54 +020090static void bdi_work_clear(struct bdi_work *work)
Adrian Bunkf11b00f2008-04-29 00:58:56 -070091{
Jens Axboe03ba3782009-09-09 09:08:54 +020092 clear_bit(WS_USED_B, &work->state);
93 smp_mb__after_clear_bit();
94 wake_up_bit(&work->state, WS_USED_B);
Adrian Bunkf11b00f2008-04-29 00:58:56 -070095}
96
Jens Axboe03ba3782009-09-09 09:08:54 +020097static void bdi_work_free(struct rcu_head *head)
Nick Piggin4195f732009-05-28 09:01:15 +020098{
Jens Axboe03ba3782009-09-09 09:08:54 +020099 struct bdi_work *work = container_of(head, struct bdi_work, rcu_head);
Nick Piggin4195f732009-05-28 09:01:15 +0200100
Jens Axboe03ba3782009-09-09 09:08:54 +0200101 if (!bdi_work_on_stack(work))
102 kfree(work);
103 else
104 bdi_work_clear(work);
105}
106
107static void wb_work_complete(struct bdi_work *work)
108{
109 const enum writeback_sync_modes sync_mode = work->sync_mode;
110
111 /*
112 * For allocated work, we can clear the done/seen bit right here.
113 * For on-stack work, we need to postpone both the clear and free
114 * to after the RCU grace period, since the stack could be invalidated
115 * as soon as bdi_work_clear() has done the wakeup.
116 */
117 if (!bdi_work_on_stack(work))
118 bdi_work_clear(work);
119 if (sync_mode == WB_SYNC_NONE || bdi_work_on_stack(work))
120 call_rcu(&work->rcu_head, bdi_work_free);
121}
122
123static void wb_clear_pending(struct bdi_writeback *wb, struct bdi_work *work)
124{
125 /*
126 * The caller has retrieved the work arguments from this work,
127 * drop our reference. If this is the last ref, delete and free it
128 */
129 if (atomic_dec_and_test(&work->pending)) {
130 struct backing_dev_info *bdi = wb->bdi;
131
132 spin_lock(&bdi->wb_lock);
133 list_del_rcu(&work->list);
134 spin_unlock(&bdi->wb_lock);
135
136 wb_work_complete(work);
Nick Piggin4195f732009-05-28 09:01:15 +0200137 }
138}
139
Jens Axboe03ba3782009-09-09 09:08:54 +0200140static void bdi_queue_work(struct backing_dev_info *bdi, struct bdi_work *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Jens Axboe03ba3782009-09-09 09:08:54 +0200142 if (work) {
143 work->seen = bdi->wb_mask;
144 BUG_ON(!work->seen);
145 atomic_set(&work->pending, bdi->wb_cnt);
146 BUG_ON(!bdi->wb_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Jens Axboe03ba3782009-09-09 09:08:54 +0200148 /*
149 * Make sure stores are seen before it appears on the list
150 */
151 smp_mb();
152
153 spin_lock(&bdi->wb_lock);
154 list_add_tail_rcu(&work->list, &bdi->work_list);
155 spin_unlock(&bdi->wb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157
158 /*
Jens Axboe03ba3782009-09-09 09:08:54 +0200159 * If the default thread isn't there, make sure we add it. When
160 * it gets created and wakes up, we'll run this work.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
Jens Axboe03ba3782009-09-09 09:08:54 +0200162 if (unlikely(list_empty_careful(&bdi->wb_list)))
163 wake_up_process(default_backing_dev_info.wb.task);
164 else {
165 struct bdi_writeback *wb = &bdi->wb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /*
Jens Axboe03ba3782009-09-09 09:08:54 +0200168 * If we failed allocating the bdi work item, wake up the wb
169 * thread always. As a safety precaution, it'll flush out
170 * everything
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
Jens Axboe03ba3782009-09-09 09:08:54 +0200172 if (!wb_has_dirty_io(wb)) {
173 if (work)
174 wb_clear_pending(wb, work);
175 } else if (wb->task)
176 wake_up_process(wb->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Jens Axboe03ba3782009-09-09 09:08:54 +0200180/*
181 * Used for on-stack allocated work items. The caller needs to wait until
182 * the wb threads have acked the work before it's safe to continue.
183 */
184static void bdi_wait_on_work_clear(struct bdi_work *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Jens Axboe03ba3782009-09-09 09:08:54 +0200186 wait_on_bit(&work->state, WS_USED_B, bdi_sched_wait,
187 TASK_UNINTERRUPTIBLE);
188}
189
190static struct bdi_work *bdi_alloc_work(struct writeback_control *wbc)
191{
192 struct bdi_work *work;
193
194 work = kmalloc(sizeof(*work), GFP_ATOMIC);
195 if (work)
196 bdi_work_init(work, wbc);
197
198 return work;
199}
200
201void bdi_start_writeback(struct writeback_control *wbc)
202{
Jens Axboe03ba3782009-09-09 09:08:54 +0200203 /*
Christoph Hellwigf0fad8a2009-09-11 09:47:56 +0200204 * WB_SYNC_NONE is opportunistic writeback. If this allocation fails,
205 * bdi_queue_work() will wake up the thread and flush old data. This
206 * should ensure some amount of progress in freeing memory.
Jens Axboe03ba3782009-09-09 09:08:54 +0200207 */
Christoph Hellwigf0fad8a2009-09-11 09:47:56 +0200208 if (wbc->sync_mode != WB_SYNC_ALL) {
209 struct bdi_work *w = bdi_alloc_work(wbc);
210
211 bdi_queue_work(wbc->bdi, w);
212 } else {
213 struct bdi_work work;
214
215 bdi_work_init(&work, wbc);
216 work.state |= WS_ONSTACK;
217
218 bdi_queue_work(wbc->bdi, &work);
219 bdi_wait_on_work_clear(&work);
Jens Axboe03ba3782009-09-09 09:08:54 +0200220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223/*
Andrew Morton6610a0b2007-10-16 23:30:32 -0700224 * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
225 * furthest end of its superblock's dirty-inode list.
226 *
227 * Before stamping the inode's ->dirtied_when, we check to see whether it is
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200228 * already the most-recently-dirtied inode on the b_dirty list. If that is
Andrew Morton6610a0b2007-10-16 23:30:32 -0700229 * the case then the inode must have been redirtied while it was being written
230 * out and we don't reset its dirtied_when.
231 */
232static void redirty_tail(struct inode *inode)
233{
Jens Axboe03ba3782009-09-09 09:08:54 +0200234 struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
Andrew Morton6610a0b2007-10-16 23:30:32 -0700235
Jens Axboe03ba3782009-09-09 09:08:54 +0200236 if (!list_empty(&wb->b_dirty)) {
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200237 struct inode *tail;
Andrew Morton6610a0b2007-10-16 23:30:32 -0700238
Jens Axboe03ba3782009-09-09 09:08:54 +0200239 tail = list_entry(wb->b_dirty.next, struct inode, i_list);
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200240 if (time_before(inode->dirtied_when, tail->dirtied_when))
Andrew Morton6610a0b2007-10-16 23:30:32 -0700241 inode->dirtied_when = jiffies;
242 }
Jens Axboe03ba3782009-09-09 09:08:54 +0200243 list_move(&inode->i_list, &wb->b_dirty);
Andrew Morton6610a0b2007-10-16 23:30:32 -0700244}
245
246/*
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200247 * requeue inode for re-scanning after bdi->b_io list is exhausted.
Andrew Mortonc986d1e2007-10-16 23:30:34 -0700248 */
Ken Chen0e0f4fc2007-10-16 23:30:38 -0700249static void requeue_io(struct inode *inode)
Andrew Mortonc986d1e2007-10-16 23:30:34 -0700250{
Jens Axboe03ba3782009-09-09 09:08:54 +0200251 struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
252
253 list_move(&inode->i_list, &wb->b_more_io);
Andrew Mortonc986d1e2007-10-16 23:30:34 -0700254}
255
Joern Engel1c0eeaf2007-10-16 23:30:44 -0700256static void inode_sync_complete(struct inode *inode)
257{
258 /*
259 * Prevent speculative execution through spin_unlock(&inode_lock);
260 */
261 smp_mb();
262 wake_up_bit(&inode->i_state, __I_SYNC);
263}
264
Jeff Laytond2caa3c52009-04-02 16:56:37 -0700265static bool inode_dirtied_after(struct inode *inode, unsigned long t)
266{
267 bool ret = time_after(inode->dirtied_when, t);
268#ifndef CONFIG_64BIT
269 /*
270 * For inodes being constantly redirtied, dirtied_when can get stuck.
271 * It _appears_ to be in the future, but is actually in distant past.
272 * This test is necessary to prevent such wrapped-around relative times
273 * from permanently stopping the whole pdflush writeback.
274 */
275 ret = ret && time_before_eq(inode->dirtied_when, jiffies);
276#endif
277 return ret;
278}
279
Andrew Mortonc986d1e2007-10-16 23:30:34 -0700280/*
Fengguang Wu2c136572007-10-16 23:30:39 -0700281 * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
282 */
283static void move_expired_inodes(struct list_head *delaying_queue,
284 struct list_head *dispatch_queue,
285 unsigned long *older_than_this)
286{
287 while (!list_empty(delaying_queue)) {
288 struct inode *inode = list_entry(delaying_queue->prev,
289 struct inode, i_list);
290 if (older_than_this &&
Jeff Laytond2caa3c52009-04-02 16:56:37 -0700291 inode_dirtied_after(inode, *older_than_this))
Fengguang Wu2c136572007-10-16 23:30:39 -0700292 break;
293 list_move(&inode->i_list, dispatch_queue);
294 }
295}
296
297/*
298 * Queue all expired dirty inodes for io, eldest first.
299 */
Jens Axboe03ba3782009-09-09 09:08:54 +0200300static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this)
Fengguang Wu2c136572007-10-16 23:30:39 -0700301{
Jens Axboe03ba3782009-09-09 09:08:54 +0200302 list_splice_init(&wb->b_more_io, wb->b_io.prev);
303 move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200304}
305
Jens Axboe03ba3782009-09-09 09:08:54 +0200306static int write_inode(struct inode *inode, int sync)
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200307{
Jens Axboe03ba3782009-09-09 09:08:54 +0200308 if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
309 return inode->i_sb->s_op->write_inode(inode, sync);
310 return 0;
Fengguang Wu2c136572007-10-16 23:30:39 -0700311}
312
313/*
Christoph Hellwig01c03192009-06-08 13:35:40 +0200314 * Wait for writeback on an inode to complete.
315 */
316static void inode_wait_for_writeback(struct inode *inode)
317{
318 DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
319 wait_queue_head_t *wqh;
320
321 wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
322 do {
323 spin_unlock(&inode_lock);
324 __wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
325 spin_lock(&inode_lock);
326 } while (inode->i_state & I_SYNC);
327}
328
329/*
330 * Write out an inode's dirty pages. Called under inode_lock. Either the
331 * caller has ref on the inode (either via __iget or via syscall against an fd)
332 * or the inode has I_WILL_FREE set (via generic_forget_inode)
333 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * If `wait' is set, wait on the writeout.
335 *
336 * The whole writeout design is quite complex and fragile. We want to avoid
337 * starvation of particular inodes when others are being redirtied, prevent
338 * livelocks, etc.
339 *
340 * Called under inode_lock.
341 */
342static int
Christoph Hellwig01c03192009-06-08 13:35:40 +0200343writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int wait = wbc->sync_mode == WB_SYNC_ALL;
Christoph Hellwig01c03192009-06-08 13:35:40 +0200347 unsigned dirty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int ret;
349
Christoph Hellwig01c03192009-06-08 13:35:40 +0200350 if (!atomic_read(&inode->i_count))
351 WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
352 else
353 WARN_ON(inode->i_state & I_WILL_FREE);
354
355 if (inode->i_state & I_SYNC) {
356 /*
357 * If this inode is locked for writeback and we are not doing
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200358 * writeback-for-data-integrity, move it to b_more_io so that
Christoph Hellwig01c03192009-06-08 13:35:40 +0200359 * writeback can proceed with the other inodes on s_io.
360 *
361 * We'll have another go at writing back this inode when we
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200362 * completed a full scan of b_io.
Christoph Hellwig01c03192009-06-08 13:35:40 +0200363 */
364 if (!wait) {
365 requeue_io(inode);
366 return 0;
367 }
368
369 /*
370 * It's a data-integrity sync. We must wait.
371 */
372 inode_wait_for_writeback(inode);
373 }
374
Joern Engel1c0eeaf2007-10-16 23:30:44 -0700375 BUG_ON(inode->i_state & I_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Joern Engel1c0eeaf2007-10-16 23:30:44 -0700377 /* Set I_SYNC, reset I_DIRTY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 dirty = inode->i_state & I_DIRTY;
Joern Engel1c0eeaf2007-10-16 23:30:44 -0700379 inode->i_state |= I_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 inode->i_state &= ~I_DIRTY;
381
382 spin_unlock(&inode_lock);
383
384 ret = do_writepages(mapping, wbc);
385
386 /* Don't write the inode if only I_DIRTY_PAGES was set */
387 if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
388 int err = write_inode(inode, wait);
389 if (ret == 0)
390 ret = err;
391 }
392
393 if (wait) {
394 int err = filemap_fdatawait(mapping);
395 if (ret == 0)
396 ret = err;
397 }
398
399 spin_lock(&inode_lock);
Joern Engel1c0eeaf2007-10-16 23:30:44 -0700400 inode->i_state &= ~I_SYNC;
Wu Fengguang84a89242009-06-16 15:33:17 -0700401 if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!(inode->i_state & I_DIRTY) &&
403 mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
404 /*
405 * We didn't write back all the pages. nfs_writepages()
406 * sometimes bales out without doing anything. Redirty
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200407 * the inode; Move it from b_io onto b_more_io/b_dirty.
Andrew Morton1b43ef92007-10-16 23:30:35 -0700408 */
409 /*
410 * akpm: if the caller was the kupdate function we put
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200411 * this inode at the head of b_dirty so it gets first
Andrew Morton1b43ef92007-10-16 23:30:35 -0700412 * consideration. Otherwise, move it to the tail, for
413 * the reasons described there. I'm not really sure
414 * how much sense this makes. Presumably I had a good
415 * reasons for doing it this way, and I'd rather not
416 * muck with it at present.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
418 if (wbc->for_kupdate) {
419 /*
Fengguang Wu2c136572007-10-16 23:30:39 -0700420 * For the kupdate function we move the inode
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200421 * to b_more_io so it will get more writeout as
Fengguang Wu2c136572007-10-16 23:30:39 -0700422 * soon as the queue becomes uncongested.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
424 inode->i_state |= I_DIRTY_PAGES;
Fengguang Wu8bc3be22008-02-04 22:29:36 -0800425 if (wbc->nr_to_write <= 0) {
426 /*
427 * slice used up: queue for next turn
428 */
429 requeue_io(inode);
430 } else {
431 /*
432 * somehow blocked: retry later
433 */
434 redirty_tail(inode);
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 } else {
437 /*
438 * Otherwise fully redirty the inode so that
439 * other inodes on this superblock will get some
440 * writeout. Otherwise heavy writing to one
441 * file would indefinitely suspend writeout of
442 * all the other files.
443 */
444 inode->i_state |= I_DIRTY_PAGES;
Andrew Morton1b43ef92007-10-16 23:30:35 -0700445 redirty_tail(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447 } else if (inode->i_state & I_DIRTY) {
448 /*
449 * Someone redirtied the inode while were writing back
450 * the pages.
451 */
Andrew Morton6610a0b2007-10-16 23:30:32 -0700452 redirty_tail(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 } else if (atomic_read(&inode->i_count)) {
454 /*
455 * The inode is clean, inuse
456 */
457 list_move(&inode->i_list, &inode_in_use);
458 } else {
459 /*
460 * The inode is clean, unused
461 */
462 list_move(&inode->i_list, &inode_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464 }
Joern Engel1c0eeaf2007-10-16 23:30:44 -0700465 inode_sync_complete(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return ret;
467}
468
Jens Axboe03ba3782009-09-09 09:08:54 +0200469/*
470 * For WB_SYNC_NONE writeback, the caller does not have the sb pinned
471 * before calling writeback. So make sure that we do pin it, so it doesn't
472 * go away while we are writing inodes from it.
473 *
474 * Returns 0 if the super was successfully pinned (or pinning wasn't needed),
475 * 1 if we failed.
476 */
477static int pin_sb_for_writeback(struct writeback_control *wbc,
478 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Jens Axboe03ba3782009-09-09 09:08:54 +0200480 struct super_block *sb = inode->i_sb;
481
482 /*
483 * Caller must already hold the ref for this
484 */
485 if (wbc->sync_mode == WB_SYNC_ALL) {
486 WARN_ON(!rwsem_is_locked(&sb->s_umount));
487 return 0;
488 }
489
490 spin_lock(&sb_lock);
491 sb->s_count++;
492 if (down_read_trylock(&sb->s_umount)) {
493 if (sb->s_root) {
494 spin_unlock(&sb_lock);
495 return 0;
496 }
497 /*
498 * umounted, drop rwsem again and fall through to failure
499 */
500 up_read(&sb->s_umount);
501 }
502
503 sb->s_count--;
504 spin_unlock(&sb_lock);
505 return 1;
506}
507
508static void unpin_sb_for_writeback(struct writeback_control *wbc,
509 struct inode *inode)
510{
511 struct super_block *sb = inode->i_sb;
512
513 if (wbc->sync_mode == WB_SYNC_ALL)
514 return;
515
516 up_read(&sb->s_umount);
517 put_super(sb);
518}
519
520static void writeback_inodes_wb(struct bdi_writeback *wb,
521 struct writeback_control *wbc)
522{
523 struct super_block *sb = wbc->sb;
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200524 const int is_blkdev_sb = sb_is_blkdev_sb(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 const unsigned long start = jiffies; /* livelock avoidance */
526
Hans Reiserae8547b2008-05-07 15:48:57 +0300527 spin_lock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Jens Axboe03ba3782009-09-09 09:08:54 +0200529 if (!wbc->for_kupdate || list_empty(&wb->b_io))
530 queue_io(wb, wbc->older_than_this);
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200531
Jens Axboe03ba3782009-09-09 09:08:54 +0200532 while (!list_empty(&wb->b_io)) {
533 struct inode *inode = list_entry(wb->b_io.prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct inode, i_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 long pages_skipped;
536
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200537 /*
538 * super block given and doesn't match, skip this inode
539 */
540 if (sb && sb != inode->i_sb) {
541 redirty_tail(inode);
542 continue;
543 }
544
Jens Axboe03ba3782009-09-09 09:08:54 +0200545 if (!bdi_cap_writeback_dirty(wb->bdi)) {
Andrew Morton9852a0e72007-10-16 23:30:33 -0700546 redirty_tail(inode);
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200547 if (is_blkdev_sb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /*
549 * Dirty memory-backed blockdev: the ramdisk
550 * driver does this. Skip just this inode
551 */
552 continue;
553 }
554 /*
555 * Dirty memory-backed inode against a filesystem other
556 * than the kernel-internal bdev filesystem. Skip the
557 * entire superblock.
558 */
559 break;
560 }
561
Wu Fengguang84a89242009-06-16 15:33:17 -0700562 if (inode->i_state & (I_NEW | I_WILL_FREE)) {
Nick Piggin7ef0d732009-03-12 14:31:38 -0700563 requeue_io(inode);
564 continue;
565 }
566
Jens Axboe03ba3782009-09-09 09:08:54 +0200567 if (wbc->nonblocking && bdi_write_congested(wb->bdi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 wbc->encountered_congestion = 1;
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200569 if (!is_blkdev_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 break; /* Skip a congested fs */
Ken Chen0e0f4fc2007-10-16 23:30:38 -0700571 requeue_io(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 continue; /* Skip a congested blockdev */
573 }
574
Jeff Laytond2caa3c52009-04-02 16:56:37 -0700575 /*
576 * Was this inode dirtied after sync_sb_inodes was called?
577 * This keeps sync from extra jobs and livelock.
578 */
579 if (inode_dirtied_after(inode, start))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 break;
581
Jens Axboe03ba3782009-09-09 09:08:54 +0200582 if (pin_sb_for_writeback(wbc, inode)) {
583 requeue_io(inode);
584 continue;
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Wu Fengguang84a89242009-06-16 15:33:17 -0700587 BUG_ON(inode->i_state & (I_FREEING | I_CLEAR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 __iget(inode);
589 pages_skipped = wbc->pages_skipped;
Christoph Hellwig01c03192009-06-08 13:35:40 +0200590 writeback_single_inode(inode, wbc);
Jens Axboe03ba3782009-09-09 09:08:54 +0200591 unpin_sb_for_writeback(wbc, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (wbc->pages_skipped != pages_skipped) {
593 /*
594 * writeback is not making progress due to locked
595 * buffers. Skip this inode for now.
596 */
Andrew Mortonf57b9b72007-10-16 23:30:34 -0700597 redirty_tail(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599 spin_unlock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 iput(inode);
OGAWA Hirofumi4ffc8442006-03-25 03:07:44 -0800601 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 spin_lock(&inode_lock);
Fengguang Wu8bc3be22008-02-04 22:29:36 -0800603 if (wbc->nr_to_write <= 0) {
604 wbc->more_io = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 break;
Fengguang Wu8bc3be22008-02-04 22:29:36 -0800606 }
Jens Axboe03ba3782009-09-09 09:08:54 +0200607 if (!list_empty(&wb->b_more_io))
Fengguang Wu8bc3be22008-02-04 22:29:36 -0800608 wbc->more_io = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Nick Piggin38f21972009-01-06 14:40:25 -0800610
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200611 spin_unlock(&inode_lock);
612 /* Leave any unwritten inodes on b_io */
613}
614
Jens Axboe03ba3782009-09-09 09:08:54 +0200615void writeback_inodes_wbc(struct writeback_control *wbc)
616{
617 struct backing_dev_info *bdi = wbc->bdi;
618
619 writeback_inodes_wb(&bdi->wb, wbc);
620}
621
622/*
623 * The maximum number of pages to writeout in a single bdi flush/kupdate
624 * operation. We do this so we don't hold I_SYNC against an inode for
625 * enormous amounts of time, which would block a userspace task which has
626 * been forced to throttle against that inode. Also, the code reevaluates
627 * the dirty each time it has written this many pages.
628 */
629#define MAX_WRITEBACK_PAGES 1024
630
631static inline bool over_bground_thresh(void)
632{
633 unsigned long background_thresh, dirty_thresh;
634
635 get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
636
637 return (global_page_state(NR_FILE_DIRTY) +
638 global_page_state(NR_UNSTABLE_NFS) >= background_thresh);
639}
640
641/*
642 * Explicit flushing or periodic writeback of "old" data.
643 *
644 * Define "old": the first time one of an inode's pages is dirtied, we mark the
645 * dirtying-time in the inode's address_space. So this periodic writeback code
646 * just walks the superblock inode list, writing back any inodes which are
647 * older than a specific point in time.
648 *
649 * Try to run once per dirty_writeback_interval. But if a writeback event
650 * takes longer than a dirty_writeback_interval interval, then leave a
651 * one-second gap.
652 *
653 * older_than_this takes precedence over nr_to_write. So we'll only write back
654 * all dirty pages if they are all attached to "old" mappings.
655 */
656static long wb_writeback(struct bdi_writeback *wb, long nr_pages,
657 struct super_block *sb,
658 enum writeback_sync_modes sync_mode, int for_kupdate)
659{
660 struct writeback_control wbc = {
661 .bdi = wb->bdi,
662 .sb = sb,
663 .sync_mode = sync_mode,
664 .older_than_this = NULL,
665 .for_kupdate = for_kupdate,
666 .range_cyclic = 1,
667 };
668 unsigned long oldest_jif;
669 long wrote = 0;
670
671 if (wbc.for_kupdate) {
672 wbc.older_than_this = &oldest_jif;
673 oldest_jif = jiffies -
674 msecs_to_jiffies(dirty_expire_interval * 10);
675 }
676
677 for (;;) {
678 /*
679 * Don't flush anything for non-integrity writeback where
680 * no nr_pages was given
681 */
682 if (!for_kupdate && nr_pages <= 0 && sync_mode == WB_SYNC_NONE)
683 break;
684
685 /*
686 * If no specific pages were given and this is just a
687 * periodic background writeout and we are below the
688 * background dirty threshold, don't do anything
689 */
690 if (for_kupdate && nr_pages <= 0 && !over_bground_thresh())
691 break;
692
693 wbc.more_io = 0;
694 wbc.encountered_congestion = 0;
695 wbc.nr_to_write = MAX_WRITEBACK_PAGES;
696 wbc.pages_skipped = 0;
697 writeback_inodes_wb(wb, &wbc);
698 nr_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
699 wrote += MAX_WRITEBACK_PAGES - wbc.nr_to_write;
700
701 /*
702 * If we ran out of stuff to write, bail unless more_io got set
703 */
704 if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
705 if (wbc.more_io && !wbc.for_kupdate)
706 continue;
707 break;
708 }
709 }
710
711 return wrote;
712}
713
714/*
715 * Return the next bdi_work struct that hasn't been processed by this
716 * wb thread yet
717 */
718static struct bdi_work *get_next_work_item(struct backing_dev_info *bdi,
719 struct bdi_writeback *wb)
720{
721 struct bdi_work *work, *ret = NULL;
722
723 rcu_read_lock();
724
725 list_for_each_entry_rcu(work, &bdi->work_list, list) {
726 if (!test_and_clear_bit(wb->nr, &work->seen))
727 continue;
728
729 ret = work;
730 break;
731 }
732
733 rcu_read_unlock();
734 return ret;
735}
736
737static long wb_check_old_data_flush(struct bdi_writeback *wb)
738{
739 unsigned long expired;
740 long nr_pages;
741
742 expired = wb->last_old_flush +
743 msecs_to_jiffies(dirty_writeback_interval * 10);
744 if (time_before(jiffies, expired))
745 return 0;
746
747 wb->last_old_flush = jiffies;
748 nr_pages = global_page_state(NR_FILE_DIRTY) +
749 global_page_state(NR_UNSTABLE_NFS) +
750 (inodes_stat.nr_inodes - inodes_stat.nr_unused);
751
752 if (nr_pages)
753 return wb_writeback(wb, nr_pages, NULL, WB_SYNC_NONE, 1);
754
755 return 0;
756}
757
758/*
759 * Retrieve work items and do the writeback they describe
760 */
761long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
762{
763 struct backing_dev_info *bdi = wb->bdi;
764 struct bdi_work *work;
765 long nr_pages, wrote = 0;
766
767 while ((work = get_next_work_item(bdi, wb)) != NULL) {
768 enum writeback_sync_modes sync_mode;
769
770 nr_pages = work->nr_pages;
771
772 /*
773 * Override sync mode, in case we must wait for completion
774 */
775 if (force_wait)
776 work->sync_mode = sync_mode = WB_SYNC_ALL;
777 else
778 sync_mode = work->sync_mode;
779
780 /*
781 * If this isn't a data integrity operation, just notify
782 * that we have seen this work and we are now starting it.
783 */
784 if (sync_mode == WB_SYNC_NONE)
785 wb_clear_pending(wb, work);
786
787 wrote += wb_writeback(wb, nr_pages, work->sb, sync_mode, 0);
788
789 /*
790 * This is a data integrity writeback, so only do the
791 * notification when we have completed the work.
792 */
793 if (sync_mode == WB_SYNC_ALL)
794 wb_clear_pending(wb, work);
795 }
796
797 /*
798 * Check for periodic writeback, kupdated() style
799 */
800 wrote += wb_check_old_data_flush(wb);
801
802 return wrote;
803}
804
805/*
806 * Handle writeback of dirty data for the device backed by this bdi. Also
807 * wakes up periodically and does kupdated style flushing.
808 */
809int bdi_writeback_task(struct bdi_writeback *wb)
810{
811 unsigned long last_active = jiffies;
812 unsigned long wait_jiffies = -1UL;
813 long pages_written;
814
815 while (!kthread_should_stop()) {
816 pages_written = wb_do_writeback(wb, 0);
817
818 if (pages_written)
819 last_active = jiffies;
820 else if (wait_jiffies != -1UL) {
821 unsigned long max_idle;
822
823 /*
824 * Longest period of inactivity that we tolerate. If we
825 * see dirty data again later, the task will get
826 * recreated automatically.
827 */
828 max_idle = max(5UL * 60 * HZ, wait_jiffies);
829 if (time_after(jiffies, max_idle + last_active))
830 break;
831 }
832
833 wait_jiffies = msecs_to_jiffies(dirty_writeback_interval * 10);
834 set_current_state(TASK_INTERRUPTIBLE);
835 schedule_timeout(wait_jiffies);
836 try_to_freeze();
837 }
838
839 return 0;
840}
841
842/*
843 * Schedule writeback for all backing devices. Expensive! If this is a data
844 * integrity operation, writeback will be complete when this returns. If
845 * we are simply called for WB_SYNC_NONE, then writeback will merely be
846 * scheduled to run.
847 */
848static void bdi_writeback_all(struct writeback_control *wbc)
849{
850 const bool must_wait = wbc->sync_mode == WB_SYNC_ALL;
851 struct backing_dev_info *bdi;
852 struct bdi_work *work;
853 LIST_HEAD(list);
854
855restart:
856 spin_lock(&bdi_lock);
857
858 list_for_each_entry(bdi, &bdi_list, bdi_list) {
859 struct bdi_work *work;
860
861 if (!bdi_has_dirty_io(bdi))
862 continue;
863
864 /*
865 * If work allocation fails, do the writes inline. We drop
866 * the lock and restart the list writeout. This should be OK,
867 * since this happens rarely and because the writeout should
868 * eventually make more free memory available.
869 */
870 work = bdi_alloc_work(wbc);
871 if (!work) {
872 struct writeback_control __wbc;
873
874 /*
875 * Not a data integrity writeout, just continue
876 */
877 if (!must_wait)
878 continue;
879
880 spin_unlock(&bdi_lock);
881 __wbc = *wbc;
882 __wbc.bdi = bdi;
883 writeback_inodes_wbc(&__wbc);
884 goto restart;
885 }
886 if (must_wait)
887 list_add_tail(&work->wait_list, &list);
888
889 bdi_queue_work(bdi, work);
890 }
891
892 spin_unlock(&bdi_lock);
893
894 /*
895 * If this is for WB_SYNC_ALL, wait for pending work to complete
896 * before returning.
897 */
898 while (!list_empty(&list)) {
899 work = list_entry(list.next, struct bdi_work, wait_list);
900 list_del(&work->wait_list);
901 bdi_wait_on_work_clear(work);
902 call_rcu(&work->rcu_head, bdi_work_free);
903 }
904}
905
906/*
907 * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
908 * the whole world.
909 */
910void wakeup_flusher_threads(long nr_pages)
911{
912 struct writeback_control wbc = {
913 .sync_mode = WB_SYNC_NONE,
914 .older_than_this = NULL,
915 .range_cyclic = 1,
916 };
917
918 if (nr_pages == 0)
919 nr_pages = global_page_state(NR_FILE_DIRTY) +
920 global_page_state(NR_UNSTABLE_NFS);
921 wbc.nr_to_write = nr_pages;
922 bdi_writeback_all(&wbc);
923}
924
925static noinline void block_dump___mark_inode_dirty(struct inode *inode)
926{
927 if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
928 struct dentry *dentry;
929 const char *name = "?";
930
931 dentry = d_find_alias(inode);
932 if (dentry) {
933 spin_lock(&dentry->d_lock);
934 name = (const char *) dentry->d_name.name;
935 }
936 printk(KERN_DEBUG
937 "%s(%d): dirtied inode %lu (%s) on %s\n",
938 current->comm, task_pid_nr(current), inode->i_ino,
939 name, inode->i_sb->s_id);
940 if (dentry) {
941 spin_unlock(&dentry->d_lock);
942 dput(dentry);
943 }
944 }
945}
946
947/**
948 * __mark_inode_dirty - internal function
949 * @inode: inode to mark
950 * @flags: what kind of dirty (i.e. I_DIRTY_SYNC)
951 * Mark an inode as dirty. Callers should use mark_inode_dirty or
952 * mark_inode_dirty_sync.
953 *
954 * Put the inode on the super block's dirty list.
955 *
956 * CAREFUL! We mark it dirty unconditionally, but move it onto the
957 * dirty list only if it is hashed or if it refers to a blockdev.
958 * If it was not hashed, it will never be added to the dirty list
959 * even if it is later hashed, as it will have been marked dirty already.
960 *
961 * In short, make sure you hash any inodes _before_ you start marking
962 * them dirty.
963 *
964 * This function *must* be atomic for the I_DIRTY_PAGES case -
965 * set_page_dirty() is called under spinlock in several places.
966 *
967 * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
968 * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of
969 * the kernel-internal blockdev inode represents the dirtying time of the
970 * blockdev's pages. This is why for I_DIRTY_PAGES we always use
971 * page->mapping->host, so the page-dirtying time is recorded in the internal
972 * blockdev inode.
973 */
974void __mark_inode_dirty(struct inode *inode, int flags)
975{
976 struct super_block *sb = inode->i_sb;
977
978 /*
979 * Don't do this for I_DIRTY_PAGES - that doesn't actually
980 * dirty the inode itself
981 */
982 if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
983 if (sb->s_op->dirty_inode)
984 sb->s_op->dirty_inode(inode);
985 }
986
987 /*
988 * make sure that changes are seen by all cpus before we test i_state
989 * -- mikulas
990 */
991 smp_mb();
992
993 /* avoid the locking if we can */
994 if ((inode->i_state & flags) == flags)
995 return;
996
997 if (unlikely(block_dump))
998 block_dump___mark_inode_dirty(inode);
999
1000 spin_lock(&inode_lock);
1001 if ((inode->i_state & flags) != flags) {
1002 const int was_dirty = inode->i_state & I_DIRTY;
1003
1004 inode->i_state |= flags;
1005
1006 /*
1007 * If the inode is being synced, just update its dirty state.
1008 * The unlocker will place the inode on the appropriate
1009 * superblock list, based upon its state.
1010 */
1011 if (inode->i_state & I_SYNC)
1012 goto out;
1013
1014 /*
1015 * Only add valid (hashed) inodes to the superblock's
1016 * dirty list. Add blockdev inodes as well.
1017 */
1018 if (!S_ISBLK(inode->i_mode)) {
1019 if (hlist_unhashed(&inode->i_hash))
1020 goto out;
1021 }
1022 if (inode->i_state & (I_FREEING|I_CLEAR))
1023 goto out;
1024
1025 /*
1026 * If the inode was already on b_dirty/b_io/b_more_io, don't
1027 * reposition it (that would break b_dirty time-ordering).
1028 */
1029 if (!was_dirty) {
1030 struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
Jens Axboe500b0672009-09-09 09:10:25 +02001031 struct backing_dev_info *bdi = wb->bdi;
1032
1033 if (bdi_cap_writeback_dirty(bdi) &&
1034 !test_bit(BDI_registered, &bdi->state)) {
1035 WARN_ON(1);
1036 printk(KERN_ERR "bdi-%s not registered\n",
1037 bdi->name);
1038 }
Jens Axboe03ba3782009-09-09 09:08:54 +02001039
1040 inode->dirtied_when = jiffies;
1041 list_move(&inode->i_list, &wb->b_dirty);
1042 }
1043 }
1044out:
1045 spin_unlock(&inode_lock);
1046}
1047EXPORT_SYMBOL(__mark_inode_dirty);
1048
Jens Axboe66f3b8e2009-09-02 09:19:46 +02001049/*
1050 * Write out a superblock's list of dirty inodes. A wait will be performed
1051 * upon no inodes, all inodes or the final one, depending upon sync_mode.
1052 *
1053 * If older_than_this is non-NULL, then only write out inodes which
1054 * had their first dirtying at a time earlier than *older_than_this.
1055 *
1056 * If we're a pdlfush thread, then implement pdflush collision avoidance
1057 * against the entire list.
1058 *
1059 * If `bdi' is non-zero then we're being asked to writeback a specific queue.
1060 * This function assumes that the blockdev superblock's inodes are backed by
1061 * a variety of queues, so all inodes are searched. For other superblocks,
1062 * assume that all inodes are backed by the same queue.
1063 *
Jens Axboe66f3b8e2009-09-02 09:19:46 +02001064 * The inodes to be written are parked on bdi->b_io. They are moved back onto
1065 * bdi->b_dirty as they are selected for writing. This way, none can be missed
1066 * on the writer throttling path, and we get decent balancing between many
1067 * throttled threads: we don't want them all piling up on inode_sync_wait.
1068 */
Jens Axboe03ba3782009-09-09 09:08:54 +02001069static void wait_sb_inodes(struct writeback_control *wbc)
Jens Axboe66f3b8e2009-09-02 09:19:46 +02001070{
Jens Axboe03ba3782009-09-09 09:08:54 +02001071 struct inode *inode, *old_inode = NULL;
Jens Axboe66f3b8e2009-09-02 09:19:46 +02001072
Jens Axboe03ba3782009-09-09 09:08:54 +02001073 /*
1074 * We need to be protected against the filesystem going from
1075 * r/o to r/w or vice versa.
1076 */
1077 WARN_ON(!rwsem_is_locked(&wbc->sb->s_umount));
Jens Axboe66f3b8e2009-09-02 09:19:46 +02001078
Jens Axboe03ba3782009-09-09 09:08:54 +02001079 spin_lock(&inode_lock);
1080
1081 /*
1082 * Data integrity sync. Must wait for all pages under writeback,
1083 * because there may have been pages dirtied before our sync
1084 * call, but which had writeout started before we write it out.
1085 * In which case, the inode may not be on the dirty list, but
1086 * we still have to wait for that writeout.
1087 */
1088 list_for_each_entry(inode, &wbc->sb->s_inodes, i_sb_list) {
1089 struct address_space *mapping;
1090
1091 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
1092 continue;
1093 mapping = inode->i_mapping;
1094 if (mapping->nrpages == 0)
1095 continue;
1096 __iget(inode);
1097 spin_unlock(&inode_lock);
1098 /*
1099 * We hold a reference to 'inode' so it couldn't have
1100 * been removed from s_inodes list while we dropped the
1101 * inode_lock. We cannot iput the inode now as we can
1102 * be holding the last reference and we cannot iput it
1103 * under inode_lock. So we keep the reference and iput
1104 * it later.
1105 */
1106 iput(old_inode);
1107 old_inode = inode;
1108
1109 filemap_fdatawait(mapping);
1110
1111 cond_resched();
Nick Piggin38f21972009-01-06 14:40:25 -08001112
Jens Axboe66f3b8e2009-09-02 09:19:46 +02001113 spin_lock(&inode_lock);
Jens Axboe66f3b8e2009-09-02 09:19:46 +02001114 }
Jens Axboe03ba3782009-09-09 09:08:54 +02001115 spin_unlock(&inode_lock);
1116 iput(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
Jens Axboed8a85592009-09-02 12:34:32 +02001119/**
1120 * writeback_inodes_sb - writeback dirty inodes from given super_block
1121 * @sb: the superblock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 *
Jens Axboed8a85592009-09-02 12:34:32 +02001123 * Start writeback on some inodes on this super_block. No guarantees are made
1124 * on how many (if any) will be written, and this function does not wait
1125 * for IO completion of submitted IO. The number of pages submitted is
1126 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 */
Jens Axboed8a85592009-09-02 12:34:32 +02001128long writeback_inodes_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 struct writeback_control wbc = {
Jens Axboe03ba3782009-09-09 09:08:54 +02001131 .sb = sb,
Jens Axboed8a85592009-09-02 12:34:32 +02001132 .sync_mode = WB_SYNC_NONE,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -07001133 .range_start = 0,
1134 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 };
Jens Axboed8a85592009-09-02 12:34:32 +02001136 unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
1137 unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
1138 long nr_to_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Jens Axboed8a85592009-09-02 12:34:32 +02001140 nr_to_write = nr_dirty + nr_unstable +
Nick Piggin38f21972009-01-06 14:40:25 -08001141 (inodes_stat.nr_inodes - inodes_stat.nr_unused);
Nick Piggin38f21972009-01-06 14:40:25 -08001142
Jens Axboed8a85592009-09-02 12:34:32 +02001143 wbc.nr_to_write = nr_to_write;
Jens Axboe03ba3782009-09-09 09:08:54 +02001144 bdi_writeback_all(&wbc);
Jens Axboed8a85592009-09-02 12:34:32 +02001145 return nr_to_write - wbc.nr_to_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
Jens Axboed8a85592009-09-02 12:34:32 +02001147EXPORT_SYMBOL(writeback_inodes_sb);
1148
1149/**
1150 * sync_inodes_sb - sync sb inode pages
1151 * @sb: the superblock
1152 *
1153 * This function writes and waits on any dirty inode belonging to this
1154 * super_block. The number of pages synced is returned.
1155 */
1156long sync_inodes_sb(struct super_block *sb)
1157{
1158 struct writeback_control wbc = {
Jens Axboe03ba3782009-09-09 09:08:54 +02001159 .sb = sb,
Jens Axboed8a85592009-09-02 12:34:32 +02001160 .sync_mode = WB_SYNC_ALL,
1161 .range_start = 0,
1162 .range_end = LLONG_MAX,
1163 };
1164 long nr_to_write = LONG_MAX; /* doesn't actually matter */
1165
1166 wbc.nr_to_write = nr_to_write;
Jens Axboe03ba3782009-09-09 09:08:54 +02001167 bdi_writeback_all(&wbc);
1168 wait_sb_inodes(&wbc);
Jens Axboed8a85592009-09-02 12:34:32 +02001169 return nr_to_write - wbc.nr_to_write;
1170}
1171EXPORT_SYMBOL(sync_inodes_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173/**
Andrea Arcangeli7f04c262005-10-30 15:03:05 -08001174 * write_inode_now - write an inode to disk
1175 * @inode: inode to write to disk
1176 * @sync: whether the write should be synchronous or not
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 *
Andrea Arcangeli7f04c262005-10-30 15:03:05 -08001178 * This function commits an inode to disk immediately if it is dirty. This is
1179 * primarily needed by knfsd.
1180 *
1181 * The caller must either have a ref on the inode or must have set I_WILL_FREE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183int write_inode_now(struct inode *inode, int sync)
1184{
1185 int ret;
1186 struct writeback_control wbc = {
1187 .nr_to_write = LONG_MAX,
Mike Galbraith18914b12008-02-08 04:20:23 -08001188 .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -07001189 .range_start = 0,
1190 .range_end = LLONG_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 };
1192
1193 if (!mapping_cap_writeback_dirty(inode->i_mapping))
Andrew Morton49364ce2005-11-07 00:59:15 -08001194 wbc.nr_to_write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 might_sleep();
1197 spin_lock(&inode_lock);
Christoph Hellwig01c03192009-06-08 13:35:40 +02001198 ret = writeback_single_inode(inode, &wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 spin_unlock(&inode_lock);
1200 if (sync)
Joern Engel1c0eeaf2007-10-16 23:30:44 -07001201 inode_sync_wait(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return ret;
1203}
1204EXPORT_SYMBOL(write_inode_now);
1205
1206/**
1207 * sync_inode - write an inode and its pages to disk.
1208 * @inode: the inode to sync
1209 * @wbc: controls the writeback mode
1210 *
1211 * sync_inode() will write an inode and its pages to disk. It will also
1212 * correctly update the inode on its superblock's dirty inode lists and will
1213 * update inode->i_state.
1214 *
1215 * The caller must have a ref on the inode.
1216 */
1217int sync_inode(struct inode *inode, struct writeback_control *wbc)
1218{
1219 int ret;
1220
1221 spin_lock(&inode_lock);
Christoph Hellwig01c03192009-06-08 13:35:40 +02001222 ret = writeback_single_inode(inode, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 spin_unlock(&inode_lock);
1224 return ret;
1225}
1226EXPORT_SYMBOL(sync_inode);