Fix prototype of SMP version of synchronize_irq.
[linux-2.6/linux-mips.git] / fs / fs-writeback.c
blob74e717cb5f37f327bef84420b2bf7fdbad59aa56
1 /*
2 * fs/fs-writeback.c
4 * Copyright (C) 2002, Linus Torvalds.
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.
11 * 10Apr2002 akpm@zip.com.au
12 * Split out of fs/inode.c
13 * Additions for address_space-based writeback
16 #include <linux/kernel.h>
17 #include <linux/spinlock.h>
18 #include <linux/sched.h>
19 #include <linux/fs.h>
20 #include <linux/mm.h>
21 #include <linux/writeback.h>
22 #include <linux/blkdev.h>
23 #include <linux/backing-dev.h>
24 #include <linux/buffer_head.h>
26 extern struct super_block *blockdev_superblock;
28 /**
29 * __mark_inode_dirty - internal function
30 * @inode: inode to mark
31 * @flags: what kind of dirty (i.e. I_DIRTY_SYNC)
32 * Mark an inode as dirty. Callers should use mark_inode_dirty or
33 * mark_inode_dirty_sync.
35 * Put the inode on the super block's dirty list.
37 * CAREFUL! We mark it dirty unconditionally, but move it onto the
38 * dirty list only if it is hashed or if it refers to a blockdev.
39 * If it was not hashed, it will never be added to the dirty list
40 * even if it is later hashed, as it will have been marked dirty already.
42 * In short, make sure you hash any inodes _before_ you start marking
43 * them dirty.
45 * This function *must* be atomic for the I_DIRTY_PAGES case -
46 * set_page_dirty() is called under spinlock in several places.
48 void __mark_inode_dirty(struct inode *inode, int flags)
50 struct super_block *sb = inode->i_sb;
52 if (!sb)
53 return; /* swapper_space */
56 * Don't do this for I_DIRTY_PAGES - that doesn't actually
57 * dirty the inode itself
59 if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
60 if (sb->s_op->dirty_inode)
61 sb->s_op->dirty_inode(inode);
65 * make sure that changes are seen by all cpus before we test i_state
66 * -- mikulas
68 smp_mb();
70 /* avoid the locking if we can */
71 if ((inode->i_state & flags) == flags)
72 return;
74 spin_lock(&inode_lock);
75 if ((inode->i_state & flags) != flags) {
76 const int was_dirty = inode->i_state & I_DIRTY;
77 struct address_space *mapping = inode->i_mapping;
79 inode->i_state |= flags;
82 * If the inode is locked, just update its dirty state.
83 * The unlocker will place the inode on the appropriate
84 * superblock list, based upon its state.
86 if (inode->i_state & I_LOCK)
87 goto out;
90 * Only add valid (hashed) inodes to the superblock's
91 * dirty list. Add blockdev inodes as well.
93 if ((hlist_unhashed(&inode->i_hash) || (inode->i_state & (I_FREEING|I_CLEAR)))
94 && !S_ISBLK(inode->i_mode))
95 goto out;
98 * If the inode was already on s_dirty or s_io, don't
99 * reposition it (that would break s_dirty time-ordering).
101 if (!was_dirty) {
102 mapping->dirtied_when = jiffies|1; /* 0 is special */
103 list_move(&inode->i_list, &sb->s_dirty);
106 out:
107 spin_unlock(&inode_lock);
110 static void write_inode(struct inode *inode, int sync)
112 if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
113 inode->i_sb->s_op->write_inode(inode, sync);
117 * Write a single inode's dirty pages and inode data out to disk.
118 * If `wait' is set, wait on the writeout.
120 * The whole writeout design is quite complex and fragile. We want to avoid
121 * starvation of particular inodes when others are being redirtied, prevent
122 * livelocks, etc.
124 * So what we do is to move all pages which are to be written from dirty_pages
125 * onto io_pages. And keep on writing io_pages until it's empty. Refusing to
126 * move more pages onto io_pages until io_pages is empty. Once that point has
127 * been reached, we are ready to take another pass across the inode's dirty
128 * pages.
130 * Called under inode_lock.
132 static void
133 __sync_single_inode(struct inode *inode, struct writeback_control *wbc)
135 unsigned dirty;
136 struct address_space *mapping = inode->i_mapping;
137 struct super_block *sb = inode->i_sb;
138 int wait = wbc->sync_mode == WB_SYNC_ALL;
140 BUG_ON(inode->i_state & I_LOCK);
142 /* Set I_LOCK, reset I_DIRTY */
143 dirty = inode->i_state & I_DIRTY;
144 inode->i_state |= I_LOCK;
145 inode->i_state &= ~I_DIRTY;
148 * smp_rmb(); note: if you remove write_lock below, you must add this.
149 * mark_inode_dirty doesn't take spinlock, make sure that inode is not
150 * read speculatively by this cpu before &= ~I_DIRTY -- mikulas
153 spin_lock(&mapping->page_lock);
154 if (wait || !wbc->for_kupdate || list_empty(&mapping->io_pages))
155 list_splice_init(&mapping->dirty_pages, &mapping->io_pages);
156 spin_unlock(&mapping->page_lock);
157 spin_unlock(&inode_lock);
159 do_writepages(mapping, wbc);
161 /* Don't write the inode if only I_DIRTY_PAGES was set */
162 if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC))
163 write_inode(inode, wait);
165 if (wait)
166 filemap_fdatawait(mapping);
168 spin_lock(&inode_lock);
169 inode->i_state &= ~I_LOCK;
170 if (!(inode->i_state & I_FREEING)) {
171 if (!list_empty(&mapping->io_pages)) {
172 /* Needs more writeback */
173 inode->i_state |= I_DIRTY_PAGES;
174 } else if (!list_empty(&mapping->dirty_pages)) {
175 /* Redirtied */
176 inode->i_state |= I_DIRTY_PAGES;
177 mapping->dirtied_when = jiffies|1;
178 list_move(&inode->i_list, &sb->s_dirty);
179 } else if (inode->i_state & I_DIRTY) {
180 /* Redirtied */
181 mapping->dirtied_when = jiffies|1;
182 list_move(&inode->i_list, &sb->s_dirty);
183 } else if (atomic_read(&inode->i_count)) {
184 mapping->dirtied_when = 0;
185 list_move(&inode->i_list, &inode_in_use);
186 } else {
187 mapping->dirtied_when = 0;
188 list_move(&inode->i_list, &inode_unused);
191 wake_up_inode(inode);
195 * Write out an inode's dirty pages. Called under inode_lock.
197 static void
198 __writeback_single_inode(struct inode *inode,
199 struct writeback_control *wbc)
201 if ((wbc->sync_mode != WB_SYNC_ALL) && (inode->i_state & I_LOCK)) {
202 list_move(&inode->i_list, &inode->i_sb->s_dirty);
203 return;
207 * It's a data-integrity sync. We must wait.
209 while (inode->i_state & I_LOCK) {
210 __iget(inode);
211 spin_unlock(&inode_lock);
212 __wait_on_inode(inode);
213 iput(inode);
214 spin_lock(&inode_lock);
216 __sync_single_inode(inode, wbc);
220 * Write out a superblock's list of dirty inodes. A wait will be performed
221 * upon no inodes, all inodes or the final one, depending upon sync_mode.
223 * If older_than_this is non-NULL, then only write out mappings which
224 * had their first dirtying at a time earlier than *older_than_this.
226 * If we're a pdlfush thread, then implement pdflush collision avoidance
227 * against the entire list.
229 * WB_SYNC_HOLD is a hack for sys_sync(): reattach the inode to sb->s_dirty so
230 * that it can be located for waiting on in __writeback_single_inode().
232 * Called under inode_lock.
234 * If `bdi' is non-zero then we're being asked to writeback a specific queue.
235 * This function assumes that the blockdev superblock's inodes are backed by
236 * a variety of queues, so all inodes are searched. For other superblocks,
237 * assume that all inodes are backed by the same queue.
239 * FIXME: this linear search could get expensive with many fileystems. But
240 * how to fix? We need to go from an address_space to all inodes which share
241 * a queue with that address_space. (Easy: have a global "dirty superblocks"
242 * list).
244 * The inodes to be written are parked on sb->s_io. They are moved back onto
245 * sb->s_dirty as they are selected for writing. This way, none can be missed
246 * on the writer throttling path, and we get decent balancing between many
247 * throttled threads: we don't want them all piling up on __wait_on_inode.
249 static void
250 sync_sb_inodes(struct super_block *sb, struct writeback_control *wbc)
252 const unsigned long start = jiffies; /* livelock avoidance */
254 if (!wbc->for_kupdate || list_empty(&sb->s_io))
255 list_splice_init(&sb->s_dirty, &sb->s_io);
257 while (!list_empty(&sb->s_io)) {
258 struct inode *inode = list_entry(sb->s_io.prev,
259 struct inode, i_list);
260 struct address_space *mapping = inode->i_mapping;
261 struct backing_dev_info *bdi = mapping->backing_dev_info;
263 if (bdi->memory_backed)
264 break;
266 if (wbc->nonblocking && bdi_write_congested(bdi)) {
267 wbc->encountered_congestion = 1;
268 if (sb != blockdev_superblock)
269 break; /* Skip a congested fs */
270 list_move(&inode->i_list, &sb->s_dirty);
271 continue; /* Skip a congested blockdev */
274 if (wbc->bdi && bdi != wbc->bdi) {
275 if (sb != blockdev_superblock)
276 break; /* fs has the wrong queue */
277 list_move(&inode->i_list, &sb->s_dirty);
278 continue; /* blockdev has wrong queue */
281 /* Was this inode dirtied after sync_sb_inodes was called? */
282 if (time_after(mapping->dirtied_when, start))
283 break;
285 /* Was this inode dirtied too recently? */
286 if (wbc->older_than_this && time_after(mapping->dirtied_when,
287 *wbc->older_than_this))
288 break;
290 /* Is another pdflush already flushing this queue? */
291 if (current_is_pdflush() && !writeback_acquire(bdi))
292 break;
294 BUG_ON(inode->i_state & I_FREEING);
295 __iget(inode);
296 __writeback_single_inode(inode, wbc);
297 if (wbc->sync_mode == WB_SYNC_HOLD) {
298 mapping->dirtied_when = jiffies|1;
299 list_move(&inode->i_list, &sb->s_dirty);
301 if (current_is_pdflush())
302 writeback_release(bdi);
303 spin_unlock(&inode_lock);
304 iput(inode);
305 spin_lock(&inode_lock);
306 if (wbc->nr_to_write <= 0)
307 break;
309 return; /* Leave any unwritten inodes on s_io */
313 * Start writeback of dirty pagecache data against all unlocked inodes.
315 * Note:
316 * We don't need to grab a reference to superblock here. If it has non-empty
317 * ->s_dirty it's hadn't been killed yet and kill_super() won't proceed
318 * past sync_inodes_sb() until both the ->s_dirty and ->s_io lists are
319 * empty. Since __sync_single_inode() regains inode_lock before it finally moves
320 * inode from superblock lists we are OK.
322 * If `older_than_this' is non-zero then only flush inodes which have a
323 * flushtime older than *older_than_this.
325 * If `bdi' is non-zero then we will scan the first inode against each
326 * superblock until we find the matching ones. One group will be the dirty
327 * inodes against a filesystem. Then when we hit the dummy blockdev superblock,
328 * sync_sb_inodes will seekout the blockdev which matches `bdi'. Maybe not
329 * super-efficient but we're about to do a ton of I/O...
331 void
332 writeback_inodes(struct writeback_control *wbc)
334 struct super_block *sb;
336 spin_lock(&inode_lock);
337 spin_lock(&sb_lock);
338 sb = sb_entry(super_blocks.prev);
339 for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.prev)) {
340 if (!list_empty(&sb->s_dirty) || !list_empty(&sb->s_io)) {
341 spin_unlock(&sb_lock);
342 sync_sb_inodes(sb, wbc);
343 spin_lock(&sb_lock);
345 if (wbc->nr_to_write <= 0)
346 break;
348 spin_unlock(&sb_lock);
349 spin_unlock(&inode_lock);
353 * writeback and wait upon the filesystem's dirty inodes. The caller will
354 * do this in two passes - one to write, and one to wait. WB_SYNC_HOLD is
355 * used to park the written inodes on sb->s_dirty for the wait pass.
357 * A finite limit is set on the number of pages which will be written.
358 * To prevent infinite livelock of sys_sync().
360 void sync_inodes_sb(struct super_block *sb, int wait)
362 struct page_state ps;
363 struct writeback_control wbc = {
364 .bdi = NULL,
365 .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_HOLD,
366 .older_than_this = NULL,
367 .nr_to_write = 0,
370 get_page_state(&ps);
371 wbc.nr_to_write = ps.nr_dirty + ps.nr_unstable +
372 (ps.nr_dirty + ps.nr_unstable) / 4;
373 spin_lock(&inode_lock);
374 sync_sb_inodes(sb, &wbc);
375 spin_unlock(&inode_lock);
379 * Rather lame livelock avoidance.
381 static void set_sb_syncing(int val)
383 struct super_block *sb;
384 spin_lock(&sb_lock);
385 sb = sb_entry(super_blocks.prev);
386 for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.prev)) {
387 sb->s_syncing = val;
389 spin_unlock(&sb_lock);
393 * Find a superblock with inodes that need to be synced
395 static struct super_block *get_super_to_sync(void)
397 struct super_block *sb;
398 restart:
399 spin_lock(&sb_lock);
400 sb = sb_entry(super_blocks.prev);
401 for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.prev)) {
402 if (sb->s_syncing)
403 continue;
404 sb->s_syncing = 1;
405 sb->s_count++;
406 spin_unlock(&sb_lock);
407 down_read(&sb->s_umount);
408 if (!sb->s_root) {
409 drop_super(sb);
410 goto restart;
412 return sb;
414 spin_unlock(&sb_lock);
415 return NULL;
419 * sync_inodes
421 * sync_inodes() goes through each super block's dirty inode list, writes the
422 * inodes out, waits on the writeout and puts the inodes back on the normal
423 * list.
425 * This is for sys_sync(). fsync_dev() uses the same algorithm. The subtle
426 * part of the sync functions is that the blockdev "superblock" is processed
427 * last. This is because the write_inode() function of a typical fs will
428 * perform no I/O, but will mark buffers in the blockdev mapping as dirty.
429 * What we want to do is to perform all that dirtying first, and then write
430 * back all those inode blocks via the blockdev mapping in one sweep. So the
431 * additional (somewhat redundant) sync_blockdev() calls here are to make
432 * sure that really happens. Because if we call sync_inodes_sb(wait=1) with
433 * outstanding dirty inodes, the writeback goes block-at-a-time within the
434 * filesystem's write_inode(). This is extremely slow.
436 void sync_inodes(int wait)
438 struct super_block *sb;
440 set_sb_syncing(0);
441 while ((sb = get_super_to_sync()) != NULL) {
442 sync_inodes_sb(sb, 0);
443 sync_blockdev(sb->s_bdev);
444 drop_super(sb);
446 if (wait) {
447 set_sb_syncing(0);
448 while ((sb = get_super_to_sync()) != NULL) {
449 sync_inodes_sb(sb, 1);
450 sync_blockdev(sb->s_bdev);
451 drop_super(sb);
457 * write_inode_now - write an inode to disk
458 * @inode: inode to write to disk
459 * @sync: whether the write should be synchronous or not
461 * This function commits an inode to disk immediately if it is
462 * dirty. This is primarily needed by knfsd.
465 void write_inode_now(struct inode *inode, int sync)
467 struct writeback_control wbc = {
468 .nr_to_write = LONG_MAX,
469 .sync_mode = WB_SYNC_ALL,
472 spin_lock(&inode_lock);
473 __writeback_single_inode(inode, &wbc);
474 spin_unlock(&inode_lock);
475 if (sync)
476 wait_on_inode(inode);
480 * generic_osync_inode - flush all dirty data for a given inode to disk
481 * @inode: inode to write
482 * @what: what to write and wait upon
484 * This can be called by file_write functions for files which have the
485 * O_SYNC flag set, to flush dirty writes to disk.
487 * @what is a bitmask, specifying which part of the inode's data should be
488 * written and waited upon:
490 * OSYNC_DATA: i_mapping's dirty data
491 * OSYNC_METADATA: the buffers at i_mapping->private_list
492 * OSYNC_INODE: the inode itself
495 int generic_osync_inode(struct inode *inode, int what)
497 int err = 0;
498 int need_write_inode_now = 0;
499 int err2;
501 if (what & OSYNC_DATA)
502 err = filemap_fdatawrite(inode->i_mapping);
503 if (what & (OSYNC_METADATA|OSYNC_DATA)) {
504 err2 = sync_mapping_buffers(inode->i_mapping);
505 if (!err)
506 err = err2;
508 if (what & OSYNC_DATA) {
509 err2 = filemap_fdatawait(inode->i_mapping);
510 if (!err)
511 err = err2;
514 spin_lock(&inode_lock);
515 if ((inode->i_state & I_DIRTY) &&
516 ((what & OSYNC_INODE) || (inode->i_state & I_DIRTY_DATASYNC)))
517 need_write_inode_now = 1;
518 spin_unlock(&inode_lock);
520 if (need_write_inode_now)
521 write_inode_now(inode, 1);
522 else
523 wait_on_inode(inode);
525 return err;
529 * writeback_acquire: attempt to get exclusive writeback access to a device
530 * @bdi: the device's backing_dev_info structure
532 * It is a waste of resources to have more than one pdflush thread blocked on
533 * a single request queue. Exclusion at the request_queue level is obtained
534 * via a flag in the request_queue's backing_dev_info.state.
536 * Non-request_queue-backed address_spaces will share default_backing_dev_info,
537 * unless they implement their own. Which is somewhat inefficient, as this
538 * may prevent concurrent writeback against multiple devices.
540 int writeback_acquire(struct backing_dev_info *bdi)
542 return !test_and_set_bit(BDI_pdflush, &bdi->state);
546 * writeback_in_progress: determine whether there is writeback in progress
547 * against a backing device.
548 * @bdi: the device's backing_dev_info structure.
550 int writeback_in_progress(struct backing_dev_info *bdi)
552 return test_bit(BDI_pdflush, &bdi->state);
556 * writeback_release: relinquish exclusive writeback access against a device.
557 * @bdi: the device's backing_dev_info structure
559 void writeback_release(struct backing_dev_info *bdi)
561 BUG_ON(!writeback_in_progress(bdi));
562 clear_bit(BDI_pdflush, &bdi->state);