writeback: kupdate writeback shall not stop when more io is possible
[linux-block.git] / fs / fs-writeback.c
index 6bca6f8176f0d8f105be5379b31837bf85f27da1..551684de13924dc256d89bb3b7ce175a02b73fdf 100644 (file)
@@ -41,8 +41,9 @@ struct wb_writeback_args {
        long nr_pages;
        struct super_block *sb;
        enum writeback_sync_modes sync_mode;
-       int for_kupdate;
-       int range_cyclic;
+       int for_kupdate:1;
+       int range_cyclic:1;
+       int for_background:1;
 };
 
 /*
@@ -97,6 +98,11 @@ static void bdi_work_clear(struct bdi_work *work)
 {
        clear_bit(WS_USED_B, &work->state);
        smp_mb__after_clear_bit();
+       /*
+        * work can have disappeared at this point. bit waitq functions
+        * should be able to tolerate this, provided bdi_sched_wait does
+        * not dereference it's pointer argument.
+       */
        wake_up_bit(&work->state, WS_USED_B);
 }
 
@@ -113,6 +119,7 @@ static void bdi_work_free(struct rcu_head *head)
 static void wb_work_complete(struct bdi_work *work)
 {
        const enum writeback_sync_modes sync_mode = work->args.sync_mode;
+       int onstack = bdi_work_on_stack(work);
 
        /*
         * For allocated work, we can clear the done/seen bit right here.
@@ -120,9 +127,9 @@ static void wb_work_complete(struct bdi_work *work)
         * to after the RCU grace period, since the stack could be invalidated
         * as soon as bdi_work_clear() has done the wakeup.
         */
-       if (!bdi_work_on_stack(work))
+       if (!onstack)
                bdi_work_clear(work);
-       if (sync_mode == WB_SYNC_NONE || bdi_work_on_stack(work))
+       if (sync_mode == WB_SYNC_NONE || onstack)
                call_rcu(&work->rcu_head, bdi_work_free);
 }
 
@@ -168,13 +175,7 @@ static void bdi_queue_work(struct backing_dev_info *bdi, struct bdi_work *work)
        else {
                struct bdi_writeback *wb = &bdi->wb;
 
-               /*
-                * End work now if this wb has no dirty IO pending. Otherwise
-                * wakeup the handling thread
-                */
-               if (!wb_has_dirty_io(wb))
-                       wb_clear_pending(wb, work);
-               else if (wb->task)
+               if (wb->task)
                        wake_up_process(wb->task);
        }
 }
@@ -257,6 +258,15 @@ void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages)
                .range_cyclic   = 1,
        };
 
+       /*
+        * We treat @nr_pages=0 as the special case to do background writeback,
+        * ie. to sync pages until the background dirty threshold is reached.
+        */
+       if (!nr_pages) {
+               args.nr_pages = LONG_MAX;
+               args.for_background = 1;
+       }
+
        bdi_alloc_queue_work(bdi, &args);
 }
 
@@ -706,6 +716,7 @@ static long wb_writeback(struct bdi_writeback *wb,
        };
        unsigned long oldest_jif;
        long wrote = 0;
+       struct inode *inode;
 
        if (wbc.for_kupdate) {
                wbc.older_than_this = &oldest_jif;
@@ -719,20 +730,16 @@ static long wb_writeback(struct bdi_writeback *wb,
 
        for (;;) {
                /*
-                * Don't flush anything for non-integrity writeback where
-                * no nr_pages was given
+                * Stop writeback when nr_pages has been consumed
                 */
-               if (!args->for_kupdate && args->nr_pages <= 0 &&
-                    args->sync_mode == WB_SYNC_NONE)
+               if (args->nr_pages <= 0)
                        break;
 
                /*
-                * If no specific pages were given and this is just a
-                * periodic background writeout and we are below the
-                * background dirty threshold, don't do anything
+                * For background writeout, stop when we are below the
+                * background dirty threshold
                 */
-               if (args->for_kupdate && args->nr_pages <= 0 &&
-                   !over_bground_thresh())
+               if (args->for_background && !over_bground_thresh())
                        break;
 
                wbc.more_io = 0;
@@ -746,9 +753,25 @@ static long wb_writeback(struct bdi_writeback *wb,
                /*
                 * If we ran out of stuff to write, bail unless more_io got set
                 */
-               if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
-                       if (wbc.more_io && !wbc.for_kupdate)
+               if (wbc.nr_to_write > 0) {
+                       if (wbc.more_io) {
+                               if (wbc.nr_to_write < MAX_WRITEBACK_PAGES)
+                                       continue;
+                               /*
+                                * Nothing written. Wait for some inode to
+                                * become available for writeback. Otherwise
+                                * we'll just busyloop.
+                                */
+                               spin_lock(&inode_lock);
+                               if (!list_empty(&wb->b_more_io))  {
+                                       inode = list_entry(
+                                                       wb->b_more_io.prev,
+                                                       struct inode, i_list);
+                                       inode_wait_for_writeback(inode);
+                               }
+                               spin_unlock(&inode_lock);
                                continue;
+                       }
                        break;
                }
        }