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>
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
;
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
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
67 /* avoid the locking if we can */
68 if ((inode
->i_state
& flags
) == flags
)
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
)
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
))
93 if (inode
->i_state
& (I_FREEING
|I_CLEAR
))
98 * If the inode was already on s_dirty or s_io, don't
99 * reposition it (that would break s_dirty time-ordering).
102 mapping
->dirtied_when
= jiffies
|1; /* 0 is special */
103 list_move(&inode
->i_list
, &sb
->s_dirty
);
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
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
130 * Called under inode_lock.
133 __sync_single_inode(struct inode
*inode
, struct writeback_control
*wbc
)
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
);
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
)) {
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
) {
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
);
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.
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
);
207 * It's a data-integrity sync. We must wait.
209 while (inode
->i_state
& I_LOCK
) {
211 spin_unlock(&inode_lock
);
212 __wait_on_inode(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"
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.
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 if (sb
== blockdev_superblock
) {
266 * Dirty memory-backed blockdev: the ramdisk
269 list_move(&inode
->i_list
, &sb
->s_dirty
);
273 * Assume that all inodes on this superblock are memory
274 * backed. Skip the superblock.
279 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
280 wbc
->encountered_congestion
= 1;
281 if (sb
!= blockdev_superblock
)
282 break; /* Skip a congested fs */
283 list_move(&inode
->i_list
, &sb
->s_dirty
);
284 continue; /* Skip a congested blockdev */
287 if (wbc
->bdi
&& bdi
!= wbc
->bdi
) {
288 if (sb
!= blockdev_superblock
)
289 break; /* fs has the wrong queue */
290 list_move(&inode
->i_list
, &sb
->s_dirty
);
291 continue; /* blockdev has wrong queue */
294 /* Was this inode dirtied after sync_sb_inodes was called? */
295 if (time_after(mapping
->dirtied_when
, start
))
298 /* Was this inode dirtied too recently? */
299 if (wbc
->older_than_this
&& time_after(mapping
->dirtied_when
,
300 *wbc
->older_than_this
))
303 /* Is another pdflush already flushing this queue? */
304 if (current_is_pdflush() && !writeback_acquire(bdi
))
307 BUG_ON(inode
->i_state
& I_FREEING
);
309 __writeback_single_inode(inode
, wbc
);
310 if (wbc
->sync_mode
== WB_SYNC_HOLD
) {
311 mapping
->dirtied_when
= jiffies
|1;
312 list_move(&inode
->i_list
, &sb
->s_dirty
);
314 if (current_is_pdflush())
315 writeback_release(bdi
);
316 spin_unlock(&inode_lock
);
318 spin_lock(&inode_lock
);
319 if (wbc
->nr_to_write
<= 0)
322 return; /* Leave any unwritten inodes on s_io */
326 * Start writeback of dirty pagecache data against all unlocked inodes.
329 * We don't need to grab a reference to superblock here. If it has non-empty
330 * ->s_dirty it's hadn't been killed yet and kill_super() won't proceed
331 * past sync_inodes_sb() until both the ->s_dirty and ->s_io lists are
332 * empty. Since __sync_single_inode() regains inode_lock before it finally moves
333 * inode from superblock lists we are OK.
335 * If `older_than_this' is non-zero then only flush inodes which have a
336 * flushtime older than *older_than_this.
338 * If `bdi' is non-zero then we will scan the first inode against each
339 * superblock until we find the matching ones. One group will be the dirty
340 * inodes against a filesystem. Then when we hit the dummy blockdev superblock,
341 * sync_sb_inodes will seekout the blockdev which matches `bdi'. Maybe not
342 * super-efficient but we're about to do a ton of I/O...
345 writeback_inodes(struct writeback_control
*wbc
)
347 struct super_block
*sb
;
349 spin_lock(&inode_lock
);
351 sb
= sb_entry(super_blocks
.prev
);
352 for (; sb
!= sb_entry(&super_blocks
); sb
= sb_entry(sb
->s_list
.prev
)) {
353 if (!list_empty(&sb
->s_dirty
) || !list_empty(&sb
->s_io
)) {
354 spin_unlock(&sb_lock
);
355 sync_sb_inodes(sb
, wbc
);
358 if (wbc
->nr_to_write
<= 0)
361 spin_unlock(&sb_lock
);
362 spin_unlock(&inode_lock
);
366 * writeback and wait upon the filesystem's dirty inodes. The caller will
367 * do this in two passes - one to write, and one to wait. WB_SYNC_HOLD is
368 * used to park the written inodes on sb->s_dirty for the wait pass.
370 * A finite limit is set on the number of pages which will be written.
371 * To prevent infinite livelock of sys_sync().
373 * We add in the number of potentially dirty inodes, because each inode write
374 * can dirty pagecache in the underlying blockdev.
376 void sync_inodes_sb(struct super_block
*sb
, int wait
)
378 struct page_state ps
;
379 struct writeback_control wbc
= {
381 .sync_mode
= wait
? WB_SYNC_ALL
: WB_SYNC_HOLD
,
382 .older_than_this
= NULL
,
387 wbc
.nr_to_write
= ps
.nr_dirty
+ ps
.nr_unstable
+
388 (inodes_stat
.nr_inodes
- inodes_stat
.nr_unused
) +
389 ps
.nr_dirty
+ ps
.nr_unstable
;
390 wbc
.nr_to_write
+= wbc
.nr_to_write
/ 2; /* Bit more for luck */
391 spin_lock(&inode_lock
);
392 sync_sb_inodes(sb
, &wbc
);
393 spin_unlock(&inode_lock
);
397 * Rather lame livelock avoidance.
399 static void set_sb_syncing(int val
)
401 struct super_block
*sb
;
403 sb
= sb_entry(super_blocks
.prev
);
404 for (; sb
!= sb_entry(&super_blocks
); sb
= sb_entry(sb
->s_list
.prev
)) {
407 spin_unlock(&sb_lock
);
411 * Find a superblock with inodes that need to be synced
413 static struct super_block
*get_super_to_sync(void)
415 struct super_block
*sb
;
418 sb
= sb_entry(super_blocks
.prev
);
419 for (; sb
!= sb_entry(&super_blocks
); sb
= sb_entry(sb
->s_list
.prev
)) {
424 spin_unlock(&sb_lock
);
425 down_read(&sb
->s_umount
);
432 spin_unlock(&sb_lock
);
439 * sync_inodes() goes through each super block's dirty inode list, writes the
440 * inodes out, waits on the writeout and puts the inodes back on the normal
443 * This is for sys_sync(). fsync_dev() uses the same algorithm. The subtle
444 * part of the sync functions is that the blockdev "superblock" is processed
445 * last. This is because the write_inode() function of a typical fs will
446 * perform no I/O, but will mark buffers in the blockdev mapping as dirty.
447 * What we want to do is to perform all that dirtying first, and then write
448 * back all those inode blocks via the blockdev mapping in one sweep. So the
449 * additional (somewhat redundant) sync_blockdev() calls here are to make
450 * sure that really happens. Because if we call sync_inodes_sb(wait=1) with
451 * outstanding dirty inodes, the writeback goes block-at-a-time within the
452 * filesystem's write_inode(). This is extremely slow.
454 void sync_inodes(int wait
)
456 struct super_block
*sb
;
459 while ((sb
= get_super_to_sync()) != NULL
) {
460 sync_inodes_sb(sb
, 0);
461 sync_blockdev(sb
->s_bdev
);
466 while ((sb
= get_super_to_sync()) != NULL
) {
467 sync_inodes_sb(sb
, 1);
468 sync_blockdev(sb
->s_bdev
);
475 * write_inode_now - write an inode to disk
476 * @inode: inode to write to disk
477 * @sync: whether the write should be synchronous or not
479 * This function commits an inode to disk immediately if it is
480 * dirty. This is primarily needed by knfsd.
483 void write_inode_now(struct inode
*inode
, int sync
)
485 struct writeback_control wbc
= {
486 .nr_to_write
= LONG_MAX
,
487 .sync_mode
= WB_SYNC_ALL
,
490 spin_lock(&inode_lock
);
491 __writeback_single_inode(inode
, &wbc
);
492 spin_unlock(&inode_lock
);
494 wait_on_inode(inode
);
498 * generic_osync_inode - flush all dirty data for a given inode to disk
499 * @inode: inode to write
500 * @what: what to write and wait upon
502 * This can be called by file_write functions for files which have the
503 * O_SYNC flag set, to flush dirty writes to disk.
505 * @what is a bitmask, specifying which part of the inode's data should be
506 * written and waited upon:
508 * OSYNC_DATA: i_mapping's dirty data
509 * OSYNC_METADATA: the buffers at i_mapping->private_list
510 * OSYNC_INODE: the inode itself
513 int generic_osync_inode(struct inode
*inode
, int what
)
516 int need_write_inode_now
= 0;
519 current
->flags
|= PF_SYNCWRITE
;
520 if (what
& OSYNC_DATA
)
521 err
= filemap_fdatawrite(inode
->i_mapping
);
522 if (what
& (OSYNC_METADATA
|OSYNC_DATA
)) {
523 err2
= sync_mapping_buffers(inode
->i_mapping
);
527 if (what
& OSYNC_DATA
) {
528 err2
= filemap_fdatawait(inode
->i_mapping
);
532 current
->flags
&= ~PF_SYNCWRITE
;
534 spin_lock(&inode_lock
);
535 if ((inode
->i_state
& I_DIRTY
) &&
536 ((what
& OSYNC_INODE
) || (inode
->i_state
& I_DIRTY_DATASYNC
)))
537 need_write_inode_now
= 1;
538 spin_unlock(&inode_lock
);
540 if (need_write_inode_now
)
541 write_inode_now(inode
, 1);
543 wait_on_inode(inode
);
549 * writeback_acquire: attempt to get exclusive writeback access to a device
550 * @bdi: the device's backing_dev_info structure
552 * It is a waste of resources to have more than one pdflush thread blocked on
553 * a single request queue. Exclusion at the request_queue level is obtained
554 * via a flag in the request_queue's backing_dev_info.state.
556 * Non-request_queue-backed address_spaces will share default_backing_dev_info,
557 * unless they implement their own. Which is somewhat inefficient, as this
558 * may prevent concurrent writeback against multiple devices.
560 int writeback_acquire(struct backing_dev_info
*bdi
)
562 return !test_and_set_bit(BDI_pdflush
, &bdi
->state
);
566 * writeback_in_progress: determine whether there is writeback in progress
567 * against a backing device.
568 * @bdi: the device's backing_dev_info structure.
570 int writeback_in_progress(struct backing_dev_info
*bdi
)
572 return test_bit(BDI_pdflush
, &bdi
->state
);
576 * writeback_release: relinquish exclusive writeback access against a device.
577 * @bdi: the device's backing_dev_info structure
579 void writeback_release(struct backing_dev_info
*bdi
)
581 BUG_ON(!writeback_in_progress(bdi
));
582 clear_bit(BDI_pdflush
, &bdi
->state
);