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 Andrew Morton
12 * Split out of fs/inode.c
13 * Additions for address_space-based writeback
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
19 #include <linux/sched.h>
22 #include <linux/kthread.h>
23 #include <linux/freezer.h>
24 #include <linux/writeback.h>
25 #include <linux/blkdev.h>
26 #include <linux/backing-dev.h>
27 #include <linux/buffer_head.h>
30 #define inode_to_bdi(inode) ((inode)->i_mapping->backing_dev_info)
33 * We don't actually have pdflush, but this one is exported though /proc...
35 int nr_pdflush_threads;
38 * Work items for the bdi_writeback threads
41 struct list_head list;
42 struct list_head wait_list;
43 struct rcu_head rcu_head;
48 struct super_block *sb;
49 unsigned long nr_pages;
50 enum writeback_sync_modes sync_mode;
60 #define WS_USED (1 << WS_USED_B)
61 #define WS_ONSTACK (1 << WS_ONSTACK_B)
63 static inline bool bdi_work_on_stack(struct bdi_work *work)
65 return test_bit(WS_ONSTACK_B, &work->state);
68 static inline void bdi_work_init(struct bdi_work *work,
69 struct writeback_control *wbc)
71 INIT_RCU_HEAD(&work->rcu_head);
73 work->nr_pages = wbc->nr_to_write;
74 work->sync_mode = wbc->sync_mode;
75 work->state = WS_USED;
79 * writeback_in_progress - determine whether there is writeback in progress
80 * @bdi: the device's backing_dev_info structure.
82 * Determine whether there is writeback waiting to be handled against a
85 int writeback_in_progress(struct backing_dev_info *bdi)
87 return !list_empty(&bdi->work_list);
90 static void bdi_work_clear(struct bdi_work *work)
92 clear_bit(WS_USED_B, &work->state);
93 smp_mb__after_clear_bit();
94 wake_up_bit(&work->state, WS_USED_B);
97 static void bdi_work_free(struct rcu_head *head)
99 struct bdi_work *work = container_of(head, struct bdi_work, rcu_head);
101 if (!bdi_work_on_stack(work))
104 bdi_work_clear(work);
107 static void wb_work_complete(struct bdi_work *work)
109 const enum writeback_sync_modes sync_mode = work->sync_mode;
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.
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);
123 static void wb_clear_pending(struct bdi_writeback *wb, struct bdi_work *work)
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
129 if (atomic_dec_and_test(&work->pending)) {
130 struct backing_dev_info *bdi = wb->bdi;
132 spin_lock(&bdi->wb_lock);
133 list_del_rcu(&work->list);
134 spin_unlock(&bdi->wb_lock);
136 wb_work_complete(work);
140 static void bdi_queue_work(struct backing_dev_info *bdi, struct bdi_work *work)
143 work->seen = bdi->wb_mask;
145 atomic_set(&work->pending, bdi->wb_cnt);
146 BUG_ON(!bdi->wb_cnt);
149 * Make sure stores are seen before it appears on the list
153 spin_lock(&bdi->wb_lock);
154 list_add_tail_rcu(&work->list, &bdi->work_list);
155 spin_unlock(&bdi->wb_lock);
159 * 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.
162 if (unlikely(list_empty_careful(&bdi->wb_list)))
163 wake_up_process(default_backing_dev_info.wb.task);
165 struct bdi_writeback *wb = &bdi->wb;
168 * If we failed allocating the bdi work item, wake up the wb
169 * thread always. As a safety precaution, it'll flush out
172 if (!wb_has_dirty_io(wb)) {
174 wb_clear_pending(wb, work);
176 wake_up_process(wb->task);
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.
184 static void bdi_wait_on_work_clear(struct bdi_work *work)
186 wait_on_bit(&work->state, WS_USED_B, bdi_sched_wait,
187 TASK_UNINTERRUPTIBLE);
190 static struct bdi_work *bdi_alloc_work(struct writeback_control *wbc)
192 struct bdi_work *work;
194 work = kmalloc(sizeof(*work), GFP_ATOMIC);
196 bdi_work_init(work, wbc);
201 void bdi_start_writeback(struct writeback_control *wbc)
204 * 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.
208 if (wbc->sync_mode != WB_SYNC_ALL) {
209 struct bdi_work *w = bdi_alloc_work(wbc);
211 bdi_queue_work(wbc->bdi, w);
213 struct bdi_work work;
215 bdi_work_init(&work, wbc);
216 work.state |= WS_ONSTACK;
218 bdi_queue_work(wbc->bdi, &work);
219 bdi_wait_on_work_clear(&work);
224 * 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.
227 * Before stamping the inode's ->dirtied_when, we check to see whether it is
228 * already the most-recently-dirtied inode on the b_dirty list. If that is
229 * the case then the inode must have been redirtied while it was being written
230 * out and we don't reset its dirtied_when.
232 static void redirty_tail(struct inode *inode)
234 struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
236 if (!list_empty(&wb->b_dirty)) {
239 tail = list_entry(wb->b_dirty.next, struct inode, i_list);
240 if (time_before(inode->dirtied_when, tail->dirtied_when))
241 inode->dirtied_when = jiffies;
243 list_move(&inode->i_list, &wb->b_dirty);
247 * requeue inode for re-scanning after bdi->b_io list is exhausted.
249 static void requeue_io(struct inode *inode)
251 struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
253 list_move(&inode->i_list, &wb->b_more_io);
256 static void inode_sync_complete(struct inode *inode)
259 * Prevent speculative execution through spin_unlock(&inode_lock);
262 wake_up_bit(&inode->i_state, __I_SYNC);
265 static bool inode_dirtied_after(struct inode *inode, unsigned long t)
267 bool ret = time_after(inode->dirtied_when, t);
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.
275 ret = ret && time_before_eq(inode->dirtied_when, jiffies);
281 * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
283 static void move_expired_inodes(struct list_head *delaying_queue,
284 struct list_head *dispatch_queue,
285 unsigned long *older_than_this)
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 &&
291 inode_dirtied_after(inode, *older_than_this))
293 list_move(&inode->i_list, dispatch_queue);
298 * Queue all expired dirty inodes for io, eldest first.
300 static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this)
302 list_splice_init(&wb->b_more_io, wb->b_io.prev);
303 move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
306 static int write_inode(struct inode *inode, int sync)
308 if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
309 return inode->i_sb->s_op->write_inode(inode, sync);
314 * Wait for writeback on an inode to complete.
316 static void inode_wait_for_writeback(struct inode *inode)
318 DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
319 wait_queue_head_t *wqh;
321 wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
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);
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)
334 * If `wait' is set, wait on the writeout.
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
340 * Called under inode_lock.
343 writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
345 struct address_space *mapping = inode->i_mapping;
346 int wait = wbc->sync_mode == WB_SYNC_ALL;
350 if (!atomic_read(&inode->i_count))
351 WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
353 WARN_ON(inode->i_state & I_WILL_FREE);
355 if (inode->i_state & I_SYNC) {
357 * If this inode is locked for writeback and we are not doing
358 * writeback-for-data-integrity, move it to b_more_io so that
359 * writeback can proceed with the other inodes on s_io.
361 * We'll have another go at writing back this inode when we
362 * completed a full scan of b_io.
370 * It's a data-integrity sync. We must wait.
372 inode_wait_for_writeback(inode);
375 BUG_ON(inode->i_state & I_SYNC);
377 /* Set I_SYNC, reset I_DIRTY */
378 dirty = inode->i_state & I_DIRTY;
379 inode->i_state |= I_SYNC;
380 inode->i_state &= ~I_DIRTY;
382 spin_unlock(&inode_lock);
384 ret = do_writepages(mapping, wbc);
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);
394 int err = filemap_fdatawait(mapping);
399 spin_lock(&inode_lock);
400 inode->i_state &= ~I_SYNC;
401 if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
402 if (!(inode->i_state & I_DIRTY) &&
403 mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
405 * We didn't write back all the pages. nfs_writepages()
406 * sometimes bales out without doing anything. Redirty
407 * the inode; Move it from b_io onto b_more_io/b_dirty.
410 * akpm: if the caller was the kupdate function we put
411 * this inode at the head of b_dirty so it gets first
412 * 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.
418 if (wbc->for_kupdate) {
420 * For the kupdate function we move the inode
421 * to b_more_io so it will get more writeout as
422 * soon as the queue becomes uncongested.
424 inode->i_state |= I_DIRTY_PAGES;
425 if (wbc->nr_to_write <= 0) {
427 * slice used up: queue for next turn
432 * somehow blocked: retry later
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.
444 inode->i_state |= I_DIRTY_PAGES;
447 } else if (inode->i_state & I_DIRTY) {
449 * Someone redirtied the inode while were writing back
453 } else if (atomic_read(&inode->i_count)) {
455 * The inode is clean, inuse
457 list_move(&inode->i_list, &inode_in_use);
460 * The inode is clean, unused
462 list_move(&inode->i_list, &inode_unused);
465 inode_sync_complete(inode);
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.
474 * Returns 0 if the super was successfully pinned (or pinning wasn't needed),
477 static int pin_sb_for_writeback(struct writeback_control *wbc,
480 struct super_block *sb = inode->i_sb;
483 * Caller must already hold the ref for this
485 if (wbc->sync_mode == WB_SYNC_ALL) {
486 WARN_ON(!rwsem_is_locked(&sb->s_umount));
492 if (down_read_trylock(&sb->s_umount)) {
494 spin_unlock(&sb_lock);
498 * umounted, drop rwsem again and fall through to failure
500 up_read(&sb->s_umount);
504 spin_unlock(&sb_lock);
508 static void unpin_sb_for_writeback(struct writeback_control *wbc,
511 struct super_block *sb = inode->i_sb;
513 if (wbc->sync_mode == WB_SYNC_ALL)
516 up_read(&sb->s_umount);
520 static void writeback_inodes_wb(struct bdi_writeback *wb,
521 struct writeback_control *wbc)
523 struct super_block *sb = wbc->sb;
524 const int is_blkdev_sb = sb_is_blkdev_sb(sb);
525 const unsigned long start = jiffies; /* livelock avoidance */
527 spin_lock(&inode_lock);
529 if (!wbc->for_kupdate || list_empty(&wb->b_io))
530 queue_io(wb, wbc->older_than_this);
532 while (!list_empty(&wb->b_io)) {
533 struct inode *inode = list_entry(wb->b_io.prev,
534 struct inode, i_list);
538 * super block given and doesn't match, skip this inode
540 if (sb && sb != inode->i_sb) {
545 if (!bdi_cap_writeback_dirty(wb->bdi)) {
549 * Dirty memory-backed blockdev: the ramdisk
550 * driver does this. Skip just this inode
555 * Dirty memory-backed inode against a filesystem other
556 * than the kernel-internal bdev filesystem. Skip the
562 if (inode->i_state & (I_NEW | I_WILL_FREE)) {
567 if (wbc->nonblocking && bdi_write_congested(wb->bdi)) {
568 wbc->encountered_congestion = 1;
570 break; /* Skip a congested fs */
572 continue; /* Skip a congested blockdev */
576 * Was this inode dirtied after sync_sb_inodes was called?
577 * This keeps sync from extra jobs and livelock.
579 if (inode_dirtied_after(inode, start))
582 if (pin_sb_for_writeback(wbc, inode)) {
587 BUG_ON(inode->i_state & (I_FREEING | I_CLEAR));
589 pages_skipped = wbc->pages_skipped;
590 writeback_single_inode(inode, wbc);
591 unpin_sb_for_writeback(wbc, inode);
592 if (wbc->pages_skipped != pages_skipped) {
594 * writeback is not making progress due to locked
595 * buffers. Skip this inode for now.
599 spin_unlock(&inode_lock);
602 spin_lock(&inode_lock);
603 if (wbc->nr_to_write <= 0) {
607 if (!list_empty(&wb->b_more_io))
611 spin_unlock(&inode_lock);
612 /* Leave any unwritten inodes on b_io */
615 void writeback_inodes_wbc(struct writeback_control *wbc)
617 struct backing_dev_info *bdi = wbc->bdi;
619 writeback_inodes_wb(&bdi->wb, wbc);
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.
629 #define MAX_WRITEBACK_PAGES 1024
631 static inline bool over_bground_thresh(void)
633 unsigned long background_thresh, dirty_thresh;
635 get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
637 return (global_page_state(NR_FILE_DIRTY) +
638 global_page_state(NR_UNSTABLE_NFS) >= background_thresh);
642 * Explicit flushing or periodic writeback of "old" data.
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.
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
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.
656 static 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)
660 struct writeback_control wbc = {
663 .sync_mode = sync_mode,
664 .older_than_this = NULL,
665 .for_kupdate = for_kupdate,
668 unsigned long oldest_jif;
671 if (wbc.for_kupdate) {
672 wbc.older_than_this = &oldest_jif;
673 oldest_jif = jiffies -
674 msecs_to_jiffies(dirty_expire_interval * 10);
679 * Don't flush anything for non-integrity writeback where
680 * no nr_pages was given
682 if (!for_kupdate && nr_pages <= 0 && sync_mode == WB_SYNC_NONE)
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
690 if (for_kupdate && nr_pages <= 0 && !over_bground_thresh())
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;
702 * If we ran out of stuff to write, bail unless more_io got set
704 if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
705 if (wbc.more_io && !wbc.for_kupdate)
715 * Return the next bdi_work struct that hasn't been processed by this
718 static struct bdi_work *get_next_work_item(struct backing_dev_info *bdi,
719 struct bdi_writeback *wb)
721 struct bdi_work *work, *ret = NULL;
725 list_for_each_entry_rcu(work, &bdi->work_list, list) {
726 if (!test_and_clear_bit(wb->nr, &work->seen))
737 static long wb_check_old_data_flush(struct bdi_writeback *wb)
739 unsigned long expired;
742 expired = wb->last_old_flush +
743 msecs_to_jiffies(dirty_writeback_interval * 10);
744 if (time_before(jiffies, expired))
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);
753 return wb_writeback(wb, nr_pages, NULL, WB_SYNC_NONE, 1);
759 * Retrieve work items and do the writeback they describe
761 long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
763 struct backing_dev_info *bdi = wb->bdi;
764 struct bdi_work *work;
765 long nr_pages, wrote = 0;
767 while ((work = get_next_work_item(bdi, wb)) != NULL) {
768 enum writeback_sync_modes sync_mode;
770 nr_pages = work->nr_pages;
773 * Override sync mode, in case we must wait for completion
776 work->sync_mode = sync_mode = WB_SYNC_ALL;
778 sync_mode = work->sync_mode;
781 * If this isn't a data integrity operation, just notify
782 * that we have seen this work and we are now starting it.
784 if (sync_mode == WB_SYNC_NONE)
785 wb_clear_pending(wb, work);
787 wrote += wb_writeback(wb, nr_pages, work->sb, sync_mode, 0);
790 * This is a data integrity writeback, so only do the
791 * notification when we have completed the work.
793 if (sync_mode == WB_SYNC_ALL)
794 wb_clear_pending(wb, work);
798 * Check for periodic writeback, kupdated() style
800 wrote += wb_check_old_data_flush(wb);
806 * Handle writeback of dirty data for the device backed by this bdi. Also
807 * wakes up periodically and does kupdated style flushing.
809 int bdi_writeback_task(struct bdi_writeback *wb)
811 unsigned long last_active = jiffies;
812 unsigned long wait_jiffies = -1UL;
815 while (!kthread_should_stop()) {
816 pages_written = wb_do_writeback(wb, 0);
819 last_active = jiffies;
820 else if (wait_jiffies != -1UL) {
821 unsigned long max_idle;
824 * Longest period of inactivity that we tolerate. If we
825 * see dirty data again later, the task will get
826 * recreated automatically.
828 max_idle = max(5UL * 60 * HZ, wait_jiffies);
829 if (time_after(jiffies, max_idle + last_active))
833 wait_jiffies = msecs_to_jiffies(dirty_writeback_interval * 10);
834 set_current_state(TASK_INTERRUPTIBLE);
835 schedule_timeout(wait_jiffies);
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
848 static void bdi_writeback_all(struct writeback_control *wbc)
850 const bool must_wait = wbc->sync_mode == WB_SYNC_ALL;
851 struct backing_dev_info *bdi;
852 struct bdi_work *work;
856 spin_lock(&bdi_lock);
858 list_for_each_entry(bdi, &bdi_list, bdi_list) {
859 struct bdi_work *work;
861 if (!bdi_has_dirty_io(bdi))
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.
870 work = bdi_alloc_work(wbc);
872 struct writeback_control __wbc;
875 * Not a data integrity writeout, just continue
880 spin_unlock(&bdi_lock);
883 writeback_inodes_wbc(&__wbc);
887 list_add_tail(&work->wait_list, &list);
889 bdi_queue_work(bdi, work);
892 spin_unlock(&bdi_lock);
895 * If this is for WB_SYNC_ALL, wait for pending work to complete
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);
907 * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
910 void wakeup_flusher_threads(long nr_pages)
912 struct writeback_control wbc = {
913 .sync_mode = WB_SYNC_NONE,
914 .older_than_this = NULL,
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);
925 static noinline void block_dump___mark_inode_dirty(struct inode *inode)
927 if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
928 struct dentry *dentry;
929 const char *name = "?";
931 dentry = d_find_alias(inode);
933 spin_lock(&dentry->d_lock);
934 name = (const char *) dentry->d_name.name;
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);
941 spin_unlock(&dentry->d_lock);
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.
954 * Put the inode on the super block's dirty list.
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.
961 * In short, make sure you hash any inodes _before_ you start marking
964 * This function *must* be atomic for the I_DIRTY_PAGES case -
965 * set_page_dirty() is called under spinlock in several places.
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
974 void __mark_inode_dirty(struct inode *inode, int flags)
976 struct super_block *sb = inode->i_sb;
979 * Don't do this for I_DIRTY_PAGES - that doesn't actually
980 * dirty the inode itself
982 if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
983 if (sb->s_op->dirty_inode)
984 sb->s_op->dirty_inode(inode);
988 * make sure that changes are seen by all cpus before we test i_state
993 /* avoid the locking if we can */
994 if ((inode->i_state & flags) == flags)
997 if (unlikely(block_dump))
998 block_dump___mark_inode_dirty(inode);
1000 spin_lock(&inode_lock);
1001 if ((inode->i_state & flags) != flags) {
1002 const int was_dirty = inode->i_state & I_DIRTY;
1004 inode->i_state |= flags;
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.
1011 if (inode->i_state & I_SYNC)
1015 * Only add valid (hashed) inodes to the superblock's
1016 * dirty list. Add blockdev inodes as well.
1018 if (!S_ISBLK(inode->i_mode)) {
1019 if (hlist_unhashed(&inode->i_hash))
1022 if (inode->i_state & (I_FREEING|I_CLEAR))
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).
1030 struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
1031 struct backing_dev_info *bdi = wb->bdi;
1033 if (bdi_cap_writeback_dirty(bdi) &&
1034 !test_bit(BDI_registered, &bdi->state)) {
1036 printk(KERN_ERR "bdi-%s not registered\n",
1040 inode->dirtied_when = jiffies;
1041 list_move(&inode->i_list, &wb->b_dirty);
1045 spin_unlock(&inode_lock);
1047 EXPORT_SYMBOL(__mark_inode_dirty);
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.
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.
1056 * If we're a pdlfush thread, then implement pdflush collision avoidance
1057 * against the entire list.
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.
1064 * 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.
1069 static void wait_sb_inodes(struct writeback_control *wbc)
1071 struct inode *inode, *old_inode = NULL;
1074 * We need to be protected against the filesystem going from
1075 * r/o to r/w or vice versa.
1077 WARN_ON(!rwsem_is_locked(&wbc->sb->s_umount));
1079 spin_lock(&inode_lock);
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.
1088 list_for_each_entry(inode, &wbc->sb->s_inodes, i_sb_list) {
1089 struct address_space *mapping;
1091 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
1093 mapping = inode->i_mapping;
1094 if (mapping->nrpages == 0)
1097 spin_unlock(&inode_lock);
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
1109 filemap_fdatawait(mapping);
1113 spin_lock(&inode_lock);
1115 spin_unlock(&inode_lock);
1120 * writeback_inodes_sb - writeback dirty inodes from given super_block
1121 * @sb: the superblock
1123 * 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
1128 long writeback_inodes_sb(struct super_block *sb)
1130 struct writeback_control wbc = {
1132 .sync_mode = WB_SYNC_NONE,
1134 .range_end = LLONG_MAX,
1136 unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
1137 unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
1140 nr_to_write = nr_dirty + nr_unstable +
1141 (inodes_stat.nr_inodes - inodes_stat.nr_unused);
1143 wbc.nr_to_write = nr_to_write;
1144 bdi_writeback_all(&wbc);
1145 return nr_to_write - wbc.nr_to_write;
1147 EXPORT_SYMBOL(writeback_inodes_sb);
1150 * sync_inodes_sb - sync sb inode pages
1151 * @sb: the superblock
1153 * This function writes and waits on any dirty inode belonging to this
1154 * super_block. The number of pages synced is returned.
1156 long sync_inodes_sb(struct super_block *sb)
1158 struct writeback_control wbc = {
1160 .sync_mode = WB_SYNC_ALL,
1162 .range_end = LLONG_MAX,
1164 long nr_to_write = LONG_MAX; /* doesn't actually matter */
1166 wbc.nr_to_write = nr_to_write;
1167 bdi_writeback_all(&wbc);
1168 wait_sb_inodes(&wbc);
1169 return nr_to_write - wbc.nr_to_write;
1171 EXPORT_SYMBOL(sync_inodes_sb);
1174 * 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
1178 * This function commits an inode to disk immediately if it is dirty. This is
1179 * primarily needed by knfsd.
1181 * The caller must either have a ref on the inode or must have set I_WILL_FREE.
1183 int write_inode_now(struct inode *inode, int sync)
1186 struct writeback_control wbc = {
1187 .nr_to_write = LONG_MAX,
1188 .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
1190 .range_end = LLONG_MAX,
1193 if (!mapping_cap_writeback_dirty(inode->i_mapping))
1194 wbc.nr_to_write = 0;
1197 spin_lock(&inode_lock);
1198 ret = writeback_single_inode(inode, &wbc);
1199 spin_unlock(&inode_lock);
1201 inode_sync_wait(inode);
1204 EXPORT_SYMBOL(write_inode_now);
1207 * sync_inode - write an inode and its pages to disk.
1208 * @inode: the inode to sync
1209 * @wbc: controls the writeback mode
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.
1215 * The caller must have a ref on the inode.
1217 int sync_inode(struct inode *inode, struct writeback_control *wbc)
1221 spin_lock(&inode_lock);
1222 ret = writeback_single_inode(inode, wbc);
1223 spin_unlock(&inode_lock);
1226 EXPORT_SYMBOL(sync_inode);