[PATCH] DVB: Update documentation and credits
[linux-2.6/history.git] / fs / fs-writeback.c
blob627c7959956d06a1fd3e0d557d768afe25e56f85
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;
53 * Don't do this for I_DIRTY_PAGES - that doesn't actually
54 * dirty the inode itself
56 if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
57 if (sb->s_op->dirty_inode)
58 sb->s_op->dirty_inode(inode);
62 * make sure that changes are seen by all cpus before we test i_state
63 * -- mikulas
65 smp_mb();
67 /* avoid the locking if we can */
68 if ((inode->i_state & flags) == flags)
69 return;
71 spin_lock(&inode_lock);
72 if ((inode->i_state & flags) != flags) {
73 const int was_dirty = inode->i_state & I_DIRTY;
74 struct address_space *mapping = inode->i_mapping;
76 inode->i_state |= flags;
79 * If the inode is locked, just update its dirty state.
80 * The unlocker will place the inode on the appropriate
81 * superblock list, based upon its state.
83 if (inode->i_state & I_LOCK)
84 goto out;
87 * Only add valid (hashed) inodes to the superblock's
88 * dirty list. Add blockdev inodes as well.
90 if (!S_ISBLK(inode->i_mode)) {
91 if (hlist_unhashed(&inode->i_hash))
92 goto out;
93 if (inode->i_state & (I_FREEING|I_CLEAR))
94 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 EXPORT_SYMBOL(__mark_inode_dirty);
112 static void write_inode(struct inode *inode, int sync)
114 if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
115 inode->i_sb->s_op->write_inode(inode, sync);
119 * Write a single inode's dirty pages and inode data out to disk.
120 * If `wait' is set, wait on the writeout.
122 * The whole writeout design is quite complex and fragile. We want to avoid
123 * starvation of particular inodes when others are being redirtied, prevent
124 * livelocks, etc.
126 * So what we do is to move all pages which are to be written from dirty_pages
127 * onto io_pages. And keep on writing io_pages until it's empty. Refusing to
128 * move more pages onto io_pages until io_pages is empty. Once that point has
129 * been reached, we are ready to take another pass across the inode's dirty
130 * pages.
132 * Called under inode_lock.
134 static void
135 __sync_single_inode(struct inode *inode, struct writeback_control *wbc)
137 unsigned dirty;
138 struct address_space *mapping = inode->i_mapping;
139 struct super_block *sb = inode->i_sb;
140 int wait = wbc->sync_mode == WB_SYNC_ALL;
142 BUG_ON(inode->i_state & I_LOCK);
144 /* Set I_LOCK, reset I_DIRTY */
145 dirty = inode->i_state & I_DIRTY;
146 inode->i_state |= I_LOCK;
147 inode->i_state &= ~I_DIRTY;
150 * smp_rmb(); note: if you remove write_lock below, you must add this.
151 * mark_inode_dirty doesn't take spinlock, make sure that inode is not
152 * read speculatively by this cpu before &= ~I_DIRTY -- mikulas
155 spin_lock(&mapping->page_lock);
156 if (wait || !wbc->for_kupdate || list_empty(&mapping->io_pages))
157 list_splice_init(&mapping->dirty_pages, &mapping->io_pages);
158 spin_unlock(&mapping->page_lock);
159 spin_unlock(&inode_lock);
161 do_writepages(mapping, wbc);
163 /* Don't write the inode if only I_DIRTY_PAGES was set */
164 if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC))
165 write_inode(inode, wait);
167 if (wait)
168 filemap_fdatawait(mapping);
170 spin_lock(&inode_lock);
171 inode->i_state &= ~I_LOCK;
172 if (!(inode->i_state & I_FREEING)) {
173 if (!list_empty(&mapping->io_pages)) {
174 /* Needs more writeback */
175 inode->i_state |= I_DIRTY_PAGES;
176 } else if (!list_empty(&mapping->dirty_pages)) {
177 /* Redirtied */
178 inode->i_state |= I_DIRTY_PAGES;
179 mapping->dirtied_when = jiffies|1;
180 list_move(&inode->i_list, &sb->s_dirty);
181 } else if (inode->i_state & I_DIRTY) {
182 /* Redirtied */
183 mapping->dirtied_when = jiffies|1;
184 list_move(&inode->i_list, &sb->s_dirty);
185 } else if (atomic_read(&inode->i_count)) {
186 mapping->dirtied_when = 0;
187 list_move(&inode->i_list, &inode_in_use);
188 } else {
189 mapping->dirtied_when = 0;
190 list_move(&inode->i_list, &inode_unused);
193 wake_up_inode(inode);
197 * Write out an inode's dirty pages. Called under inode_lock.
199 static void
200 __writeback_single_inode(struct inode *inode,
201 struct writeback_control *wbc)
203 if ((wbc->sync_mode != WB_SYNC_ALL) && (inode->i_state & I_LOCK)) {
204 list_move(&inode->i_list, &inode->i_sb->s_dirty);
205 return;
209 * It's a data-integrity sync. We must wait.
211 while (inode->i_state & I_LOCK) {
212 __iget(inode);
213 spin_unlock(&inode_lock);
214 __wait_on_inode(inode);
215 iput(inode);
216 spin_lock(&inode_lock);
218 __sync_single_inode(inode, wbc);
222 * Write out a superblock's list of dirty inodes. A wait will be performed
223 * upon no inodes, all inodes or the final one, depending upon sync_mode.
225 * If older_than_this is non-NULL, then only write out mappings which
226 * had their first dirtying at a time earlier than *older_than_this.
228 * If we're a pdlfush thread, then implement pdflush collision avoidance
229 * against the entire list.
231 * WB_SYNC_HOLD is a hack for sys_sync(): reattach the inode to sb->s_dirty so
232 * that it can be located for waiting on in __writeback_single_inode().
234 * Called under inode_lock.
236 * If `bdi' is non-zero then we're being asked to writeback a specific queue.
237 * This function assumes that the blockdev superblock's inodes are backed by
238 * a variety of queues, so all inodes are searched. For other superblocks,
239 * assume that all inodes are backed by the same queue.
241 * FIXME: this linear search could get expensive with many fileystems. But
242 * how to fix? We need to go from an address_space to all inodes which share
243 * a queue with that address_space. (Easy: have a global "dirty superblocks"
244 * list).
246 * The inodes to be written are parked on sb->s_io. They are moved back onto
247 * sb->s_dirty as they are selected for writing. This way, none can be missed
248 * on the writer throttling path, and we get decent balancing between many
249 * throttled threads: we don't want them all piling up on __wait_on_inode.
251 static void
252 sync_sb_inodes(struct super_block *sb, struct writeback_control *wbc)
254 const unsigned long start = jiffies; /* livelock avoidance */
256 if (!wbc->for_kupdate || list_empty(&sb->s_io))
257 list_splice_init(&sb->s_dirty, &sb->s_io);
259 while (!list_empty(&sb->s_io)) {
260 struct inode *inode = list_entry(sb->s_io.prev,
261 struct inode, i_list);
262 struct address_space *mapping = inode->i_mapping;
263 struct backing_dev_info *bdi = mapping->backing_dev_info;
265 if (bdi->memory_backed) {
266 if (sb == blockdev_superblock) {
268 * Dirty memory-backed blockdev: the ramdisk
269 * driver does this.
271 list_move(&inode->i_list, &sb->s_dirty);
272 continue;
275 * Assume that all inodes on this superblock are memory
276 * backed. Skip the superblock.
278 break;
281 if (wbc->nonblocking && bdi_write_congested(bdi)) {
282 wbc->encountered_congestion = 1;
283 if (sb != blockdev_superblock)
284 break; /* Skip a congested fs */
285 list_move(&inode->i_list, &sb->s_dirty);
286 continue; /* Skip a congested blockdev */
289 if (wbc->bdi && bdi != wbc->bdi) {
290 if (sb != blockdev_superblock)
291 break; /* fs has the wrong queue */
292 list_move(&inode->i_list, &sb->s_dirty);
293 continue; /* blockdev has wrong queue */
296 /* Was this inode dirtied after sync_sb_inodes was called? */
297 if (time_after(mapping->dirtied_when, start))
298 break;
300 /* Was this inode dirtied too recently? */
301 if (wbc->older_than_this && time_after(mapping->dirtied_when,
302 *wbc->older_than_this))
303 break;
305 /* Is another pdflush already flushing this queue? */
306 if (current_is_pdflush() && !writeback_acquire(bdi))
307 break;
309 BUG_ON(inode->i_state & I_FREEING);
310 __iget(inode);
311 __writeback_single_inode(inode, wbc);
312 if (wbc->sync_mode == WB_SYNC_HOLD) {
313 mapping->dirtied_when = jiffies|1;
314 list_move(&inode->i_list, &sb->s_dirty);
316 if (current_is_pdflush())
317 writeback_release(bdi);
318 spin_unlock(&inode_lock);
319 iput(inode);
320 spin_lock(&inode_lock);
321 if (wbc->nr_to_write <= 0)
322 break;
324 return; /* Leave any unwritten inodes on s_io */
328 * Start writeback of dirty pagecache data against all unlocked inodes.
330 * Note:
331 * We don't need to grab a reference to superblock here. If it has non-empty
332 * ->s_dirty it's hadn't been killed yet and kill_super() won't proceed
333 * past sync_inodes_sb() until both the ->s_dirty and ->s_io lists are
334 * empty. Since __sync_single_inode() regains inode_lock before it finally moves
335 * inode from superblock lists we are OK.
337 * If `older_than_this' is non-zero then only flush inodes which have a
338 * flushtime older than *older_than_this.
340 * If `bdi' is non-zero then we will scan the first inode against each
341 * superblock until we find the matching ones. One group will be the dirty
342 * inodes against a filesystem. Then when we hit the dummy blockdev superblock,
343 * sync_sb_inodes will seekout the blockdev which matches `bdi'. Maybe not
344 * super-efficient but we're about to do a ton of I/O...
346 void
347 writeback_inodes(struct writeback_control *wbc)
349 struct super_block *sb;
351 spin_lock(&inode_lock);
352 spin_lock(&sb_lock);
353 sb = sb_entry(super_blocks.prev);
354 for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.prev)) {
355 if (!list_empty(&sb->s_dirty) || !list_empty(&sb->s_io)) {
356 spin_unlock(&sb_lock);
357 sync_sb_inodes(sb, wbc);
358 spin_lock(&sb_lock);
360 if (wbc->nr_to_write <= 0)
361 break;
363 spin_unlock(&sb_lock);
364 spin_unlock(&inode_lock);
368 * writeback and wait upon the filesystem's dirty inodes. The caller will
369 * do this in two passes - one to write, and one to wait. WB_SYNC_HOLD is
370 * used to park the written inodes on sb->s_dirty for the wait pass.
372 * A finite limit is set on the number of pages which will be written.
373 * To prevent infinite livelock of sys_sync().
375 * We add in the number of potentially dirty inodes, because each inode write
376 * can dirty pagecache in the underlying blockdev.
378 void sync_inodes_sb(struct super_block *sb, int wait)
380 struct page_state ps;
381 struct writeback_control wbc = {
382 .bdi = NULL,
383 .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_HOLD,
384 .older_than_this = NULL,
385 .nr_to_write = 0,
388 get_page_state(&ps);
389 wbc.nr_to_write = ps.nr_dirty + ps.nr_unstable +
390 (inodes_stat.nr_inodes - inodes_stat.nr_unused) +
391 ps.nr_dirty + ps.nr_unstable;
392 wbc.nr_to_write += wbc.nr_to_write / 2; /* Bit more for luck */
393 spin_lock(&inode_lock);
394 sync_sb_inodes(sb, &wbc);
395 spin_unlock(&inode_lock);
399 * Rather lame livelock avoidance.
401 static void set_sb_syncing(int val)
403 struct super_block *sb;
404 spin_lock(&sb_lock);
405 sb = sb_entry(super_blocks.prev);
406 for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.prev)) {
407 sb->s_syncing = val;
409 spin_unlock(&sb_lock);
413 * Find a superblock with inodes that need to be synced
415 static struct super_block *get_super_to_sync(void)
417 struct super_block *sb;
418 restart:
419 spin_lock(&sb_lock);
420 sb = sb_entry(super_blocks.prev);
421 for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.prev)) {
422 if (sb->s_syncing)
423 continue;
424 sb->s_syncing = 1;
425 sb->s_count++;
426 spin_unlock(&sb_lock);
427 down_read(&sb->s_umount);
428 if (!sb->s_root) {
429 drop_super(sb);
430 goto restart;
432 return sb;
434 spin_unlock(&sb_lock);
435 return NULL;
439 * sync_inodes
441 * sync_inodes() goes through each super block's dirty inode list, writes the
442 * inodes out, waits on the writeout and puts the inodes back on the normal
443 * list.
445 * This is for sys_sync(). fsync_dev() uses the same algorithm. The subtle
446 * part of the sync functions is that the blockdev "superblock" is processed
447 * last. This is because the write_inode() function of a typical fs will
448 * perform no I/O, but will mark buffers in the blockdev mapping as dirty.
449 * What we want to do is to perform all that dirtying first, and then write
450 * back all those inode blocks via the blockdev mapping in one sweep. So the
451 * additional (somewhat redundant) sync_blockdev() calls here are to make
452 * sure that really happens. Because if we call sync_inodes_sb(wait=1) with
453 * outstanding dirty inodes, the writeback goes block-at-a-time within the
454 * filesystem's write_inode(). This is extremely slow.
456 void sync_inodes(int wait)
458 struct super_block *sb;
460 set_sb_syncing(0);
461 while ((sb = get_super_to_sync()) != NULL) {
462 sync_inodes_sb(sb, 0);
463 sync_blockdev(sb->s_bdev);
464 drop_super(sb);
466 if (wait) {
467 set_sb_syncing(0);
468 while ((sb = get_super_to_sync()) != NULL) {
469 sync_inodes_sb(sb, 1);
470 sync_blockdev(sb->s_bdev);
471 drop_super(sb);
477 * write_inode_now - write an inode to disk
478 * @inode: inode to write to disk
479 * @sync: whether the write should be synchronous or not
481 * This function commits an inode to disk immediately if it is
482 * dirty. This is primarily needed by knfsd.
485 void write_inode_now(struct inode *inode, int sync)
487 struct writeback_control wbc = {
488 .nr_to_write = LONG_MAX,
489 .sync_mode = WB_SYNC_ALL,
492 spin_lock(&inode_lock);
493 __writeback_single_inode(inode, &wbc);
494 spin_unlock(&inode_lock);
495 if (sync)
496 wait_on_inode(inode);
499 EXPORT_SYMBOL(write_inode_now);
502 * generic_osync_inode - flush all dirty data for a given inode to disk
503 * @inode: inode to write
504 * @what: what to write and wait upon
506 * This can be called by file_write functions for files which have the
507 * O_SYNC flag set, to flush dirty writes to disk.
509 * @what is a bitmask, specifying which part of the inode's data should be
510 * written and waited upon:
512 * OSYNC_DATA: i_mapping's dirty data
513 * OSYNC_METADATA: the buffers at i_mapping->private_list
514 * OSYNC_INODE: the inode itself
517 int generic_osync_inode(struct inode *inode, int what)
519 int err = 0;
520 int need_write_inode_now = 0;
521 int err2;
523 current->flags |= PF_SYNCWRITE;
524 if (what & OSYNC_DATA)
525 err = filemap_fdatawrite(inode->i_mapping);
526 if (what & (OSYNC_METADATA|OSYNC_DATA)) {
527 err2 = sync_mapping_buffers(inode->i_mapping);
528 if (!err)
529 err = err2;
531 if (what & OSYNC_DATA) {
532 err2 = filemap_fdatawait(inode->i_mapping);
533 if (!err)
534 err = err2;
536 current->flags &= ~PF_SYNCWRITE;
538 spin_lock(&inode_lock);
539 if ((inode->i_state & I_DIRTY) &&
540 ((what & OSYNC_INODE) || (inode->i_state & I_DIRTY_DATASYNC)))
541 need_write_inode_now = 1;
542 spin_unlock(&inode_lock);
544 if (need_write_inode_now)
545 write_inode_now(inode, 1);
546 else
547 wait_on_inode(inode);
549 return err;
552 EXPORT_SYMBOL(generic_osync_inode);
555 * writeback_acquire: attempt to get exclusive writeback access to a device
556 * @bdi: the device's backing_dev_info structure
558 * It is a waste of resources to have more than one pdflush thread blocked on
559 * a single request queue. Exclusion at the request_queue level is obtained
560 * via a flag in the request_queue's backing_dev_info.state.
562 * Non-request_queue-backed address_spaces will share default_backing_dev_info,
563 * unless they implement their own. Which is somewhat inefficient, as this
564 * may prevent concurrent writeback against multiple devices.
566 int writeback_acquire(struct backing_dev_info *bdi)
568 return !test_and_set_bit(BDI_pdflush, &bdi->state);
572 * writeback_in_progress: determine whether there is writeback in progress
573 * against a backing device.
574 * @bdi: the device's backing_dev_info structure.
576 int writeback_in_progress(struct backing_dev_info *bdi)
578 return test_bit(BDI_pdflush, &bdi->state);
582 * writeback_release: relinquish exclusive writeback access against a device.
583 * @bdi: the device's backing_dev_info structure
585 void writeback_release(struct backing_dev_info *bdi)
587 BUG_ON(!writeback_in_progress(bdi));
588 clear_bit(BDI_pdflush, &bdi->state);