]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - fs/fs-writeback.c
writeback: split writeback_inodes_wb
[net-next-2.6.git] / fs / fs-writeback.c
index ea8592b906968913499b9847f35389a31e5dc1a3..8cc06d5432b53d5eadec4a998197a575bbacfcec 100644 (file)
@@ -45,7 +45,6 @@ struct wb_writeback_args {
        unsigned int for_kupdate:1;
        unsigned int range_cyclic:1;
        unsigned int for_background:1;
-       unsigned int sb_pinned:1;
 };
 
 /*
@@ -64,24 +63,16 @@ struct bdi_work {
 };
 
 enum {
-       WS_USED_B = 0,
-       WS_ONSTACK_B,
+       WS_INPROGRESS = 0,
+       WS_ONSTACK,
 };
 
-#define WS_USED (1 << WS_USED_B)
-#define WS_ONSTACK (1 << WS_ONSTACK_B)
-
-static inline bool bdi_work_on_stack(struct bdi_work *work)
-{
-       return test_bit(WS_ONSTACK_B, &work->state);
-}
-
 static inline void bdi_work_init(struct bdi_work *work,
                                 struct wb_writeback_args *args)
 {
        INIT_RCU_HEAD(&work->rcu_head);
        work->args = *args;
-       work->state = WS_USED;
+       __set_bit(WS_INPROGRESS, &work->state);
 }
 
 /**
@@ -96,43 +87,16 @@ int writeback_in_progress(struct backing_dev_info *bdi)
        return !list_empty(&bdi->work_list);
 }
 
-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);
-}
-
 static void bdi_work_free(struct rcu_head *head)
 {
        struct bdi_work *work = container_of(head, struct bdi_work, rcu_head);
 
-       if (!bdi_work_on_stack(work))
-               kfree(work);
-       else
-               bdi_work_clear(work);
-}
-
-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);
+       clear_bit(WS_INPROGRESS, &work->state);
+       smp_mb__after_clear_bit();
+       wake_up_bit(&work->state, WS_INPROGRESS);
 
-       /*
-        * For allocated work, we can clear the done/seen bit right here.
-        * For on-stack work, we need to postpone both the clear and free
-        * to after the RCU grace period, since the stack could be invalidated
-        * as soon as bdi_work_clear() has done the wakeup.
-        */
-       if (!onstack)
-               bdi_work_clear(work);
-       if (sync_mode == WB_SYNC_NONE || onstack)
-               call_rcu(&work->rcu_head, bdi_work_free);
+       if (!test_bit(WS_ONSTACK, &work->state))
+               kfree(work);
 }
 
 static void wb_clear_pending(struct bdi_writeback *wb, struct bdi_work *work)
@@ -148,7 +112,7 @@ static void wb_clear_pending(struct bdi_writeback *wb, struct bdi_work *work)
                list_del_rcu(&work->list);
                spin_unlock(&bdi->wb_lock);
 
-               wb_work_complete(work);
+               call_rcu(&work->rcu_head, bdi_work_free);
        }
 }
 
@@ -186,15 +150,14 @@ static void bdi_queue_work(struct backing_dev_info *bdi, struct bdi_work *work)
  * Used for on-stack allocated work items. The caller needs to wait until
  * the wb threads have acked the work before it's safe to continue.
  */
-static void bdi_wait_on_work_clear(struct bdi_work *work)
+static void bdi_wait_on_work_done(struct bdi_work *work)
 {
-       wait_on_bit(&work->state, WS_USED_B, bdi_sched_wait,
+       wait_on_bit(&work->state, WS_INPROGRESS, bdi_sched_wait,
                    TASK_UNINTERRUPTIBLE);
 }
 
 static void bdi_alloc_queue_work(struct backing_dev_info *bdi,
-                                struct wb_writeback_args *args,
-                                int wait)
+                                struct wb_writeback_args *args)
 {
        struct bdi_work *work;
 
@@ -206,8 +169,6 @@ static void bdi_alloc_queue_work(struct backing_dev_info *bdi,
        if (work) {
                bdi_work_init(work, args);
                bdi_queue_work(bdi, work);
-               if (wait)
-                       bdi_wait_on_work_clear(work);
        } else {
                struct bdi_writeback *wb = &bdi->wb;
 
@@ -217,72 +178,65 @@ static void bdi_alloc_queue_work(struct backing_dev_info *bdi,
 }
 
 /**
- * bdi_sync_writeback - start and wait for writeback
- * @bdi: the backing device to write from
- * @sb: write inodes from this super_block
+ * bdi_queue_work_onstack - start and wait for writeback
+ * @args: parameters to control the work queue writeback
  *
  * Description:
- *   This does WB_SYNC_ALL data integrity writeback and waits for the
- *   IO to complete. Callers must hold the sb s_umount semaphore for
+ *   This function initiates writeback and waits for the operation to
+ *   complete. Callers must hold the sb s_umount semaphore for
  *   reading, to avoid having the super disappear before we are done.
  */
-static void bdi_sync_writeback(struct backing_dev_info *bdi,
-                              struct super_block *sb)
+static void bdi_queue_work_onstack(struct wb_writeback_args *args)
 {
-       struct wb_writeback_args args = {
-               .sb             = sb,
-               .sync_mode      = WB_SYNC_ALL,
-               .nr_pages       = LONG_MAX,
-               .range_cyclic   = 0,
-               /*
-                * Setting sb_pinned is not necessary for WB_SYNC_ALL, but
-                * lets make it explicitly clear.
-                */
-               .sb_pinned      = 1,
-       };
        struct bdi_work work;
 
-       bdi_work_init(&work, &args);
-       work.state |= WS_ONSTACK;
+       bdi_work_init(&work, args);
+       __set_bit(WS_ONSTACK, &work.state);
 
-       bdi_queue_work(bdi, &work);
-       bdi_wait_on_work_clear(&work);
+       bdi_queue_work(args->sb->s_bdi, &work);
+       bdi_wait_on_work_done(&work);
 }
 
 /**
  * bdi_start_writeback - start writeback
  * @bdi: the backing device to write from
- * @sb: write inodes from this super_block
  * @nr_pages: the number of pages to write
- * @sb_locked: caller already holds sb umount sem.
  *
  * Description:
  *   This does WB_SYNC_NONE opportunistic writeback. The IO is only
  *   started when this function returns, we make no guarentees on
- *   completion. Caller specifies whether sb umount sem is held already or not.
+ *   completion. Caller need not hold sb s_umount semaphore.
  *
  */
-void bdi_start_writeback(struct backing_dev_info *bdi, struct super_block *sb,
-                        long nr_pages, int sb_locked)
+void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages)
 {
        struct wb_writeback_args args = {
-               .sb             = sb,
                .sync_mode      = WB_SYNC_NONE,
                .nr_pages       = nr_pages,
                .range_cyclic   = 1,
-               .sb_pinned      = sb_locked,
        };
 
-       /*
-        * 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);
+}
 
-       bdi_alloc_queue_work(bdi, &args, sb_locked);
+/**
+ * bdi_start_background_writeback - start background writeback
+ * @bdi: the backing device to write from
+ *
+ * Description:
+ *   This does WB_SYNC_NONE background writeback. The IO is only
+ *   started when this function returns, we make no guarentees on
+ *   completion. Caller need not hold sb s_umount semaphore.
+ */
+void bdi_start_background_writeback(struct backing_dev_info *bdi)
+{
+       struct wb_writeback_args args = {
+               .sync_mode      = WB_SYNC_NONE,
+               .nr_pages       = LONG_MAX,
+               .for_background = 1,
+               .range_cyclic   = 1,
+       };
+       bdi_alloc_queue_work(bdi, &args);
 }
 
 /*
@@ -572,75 +526,69 @@ select_queue:
        return ret;
 }
 
-static void unpin_sb_for_writeback(struct super_block *sb)
-{
-       up_read(&sb->s_umount);
-       put_super(sb);
-}
-
-enum sb_pin_state {
-       SB_PINNED,
-       SB_NOT_PINNED,
-       SB_PIN_FAILED
-};
-
 /*
- * For WB_SYNC_NONE writeback, the caller does not have the sb pinned
+ * For background writeback the caller does not have the sb pinned
  * before calling writeback. So make sure that we do pin it, so it doesn't
  * go away while we are writing inodes from it.
  */
-static enum sb_pin_state pin_sb_for_writeback(struct writeback_control *wbc,
-                                             struct super_block *sb)
+static bool pin_sb_for_writeback(struct super_block *sb)
 {
-       /*
-        * Caller must already hold the ref for this
-        */
-       if (wbc->sync_mode == WB_SYNC_ALL || wbc->sb_pinned) {
-               WARN_ON(!rwsem_is_locked(&sb->s_umount));
-               return SB_NOT_PINNED;
-       }
        spin_lock(&sb_lock);
+       if (list_empty(&sb->s_instances)) {
+               spin_unlock(&sb_lock);
+               return false;
+       }
+
        sb->s_count++;
+       spin_unlock(&sb_lock);
+
        if (down_read_trylock(&sb->s_umount)) {
-               if (sb->s_root) {
-                       spin_unlock(&sb_lock);
-                       return SB_PINNED;
-               }
-               /*
-                * umounted, drop rwsem again and fall through to failure
-                */
+               if (sb->s_root)
+                       return true;
                up_read(&sb->s_umount);
        }
-       sb->s_count--;
-       spin_unlock(&sb_lock);
-       return SB_PIN_FAILED;
+
+       put_super(sb);
+       return false;
 }
 
 /*
  * Write a portion of b_io inodes which belong to @sb.
- * If @wbc->sb != NULL, then find and write all such
+ *
+ * If @only_this_sb is true, then find and write all such
  * inodes. Otherwise write only ones which go sequentially
  * in reverse order.
+ *
  * Return 1, if the caller writeback routine should be
  * interrupted. Otherwise return 0.
  */
-static int writeback_sb_inodes(struct super_block *sb,
-                              struct bdi_writeback *wb,
-                              struct writeback_control *wbc)
+static int writeback_sb_inodes(struct super_block *sb, struct bdi_writeback *wb,
+               struct writeback_control *wbc, bool only_this_sb)
 {
        while (!list_empty(&wb->b_io)) {
                long pages_skipped;
                struct inode *inode = list_entry(wb->b_io.prev,
                                                 struct inode, i_list);
-               if (wbc->sb && sb != inode->i_sb) {
-                       /* super block given and doesn't
-                          match, skip this inode */
-                       redirty_tail(inode);
-                       continue;
-               }
-               if (sb != inode->i_sb)
-                       /* finish with this superblock */
+
+               if (inode->i_sb != sb) {
+                       if (only_this_sb) {
+                               /*
+                                * We only want to write back data for this
+                                * superblock, move all inodes not belonging
+                                * to it back onto the dirty list.
+                                */
+                               redirty_tail(inode);
+                               continue;
+                       }
+
+                       /*
+                        * The inode belongs to a different superblock.
+                        * Bounce back to the caller to unpin this and
+                        * pin the next superblock.
+                        */
                        return 0;
+               }
+
                if (inode->i_state & (I_NEW | I_WILL_FREE)) {
                        requeue_io(inode);
                        continue;
@@ -678,8 +626,8 @@ static int writeback_sb_inodes(struct super_block *sb,
        return 1;
 }
 
-static void writeback_inodes_wb(struct bdi_writeback *wb,
-                               struct writeback_control *wbc)
+void writeback_inodes_wb(struct bdi_writeback *wb,
+               struct writeback_control *wbc)
 {
        int ret = 0;
 
@@ -692,24 +640,14 @@ static void writeback_inodes_wb(struct bdi_writeback *wb,
                struct inode *inode = list_entry(wb->b_io.prev,
                                                 struct inode, i_list);
                struct super_block *sb = inode->i_sb;
-               enum sb_pin_state state;
 
-               if (wbc->sb && sb != wbc->sb) {
-                       /* super block given and doesn't
-                          match, skip this inode */
-                       redirty_tail(inode);
-                       continue;
-               }
-               state = pin_sb_for_writeback(wbc, sb);
-
-               if (state == SB_PIN_FAILED) {
+               if (!pin_sb_for_writeback(sb)) {
                        requeue_io(inode);
                        continue;
                }
-               ret = writeback_sb_inodes(sb, wb, wbc);
+               ret = writeback_sb_inodes(sb, wb, wbc, false);
+               drop_super(sb);
 
-               if (state == SB_PINNED)
-                       unpin_sb_for_writeback(sb);
                if (ret)
                        break;
        }
@@ -717,11 +655,17 @@ static void writeback_inodes_wb(struct bdi_writeback *wb,
        /* Leave any unwritten inodes on b_io */
 }
 
-void writeback_inodes_wbc(struct writeback_control *wbc)
+static void __writeback_inodes_sb(struct super_block *sb,
+               struct bdi_writeback *wb, struct writeback_control *wbc)
 {
-       struct backing_dev_info *bdi = wbc->bdi;
+       WARN_ON(!rwsem_is_locked(&sb->s_umount));
 
-       writeback_inodes_wb(&bdi->wb, wbc);
+       wbc->wb_start = jiffies; /* livelock avoidance */
+       spin_lock(&inode_lock);
+       if (!wbc->for_kupdate || list_empty(&wb->b_io))
+               queue_io(wb, wbc->older_than_this);
+       writeback_sb_inodes(sb, wb, wbc, true);
+       spin_unlock(&inode_lock);
 }
 
 /*
@@ -762,14 +706,11 @@ static long wb_writeback(struct bdi_writeback *wb,
                         struct wb_writeback_args *args)
 {
        struct writeback_control wbc = {
-               .bdi                    = wb->bdi,
-               .sb                     = args->sb,
                .sync_mode              = args->sync_mode,
                .older_than_this        = NULL,
                .for_kupdate            = args->for_kupdate,
                .for_background         = args->for_background,
                .range_cyclic           = args->range_cyclic,
-               .sb_pinned              = args->sb_pinned,
        };
        unsigned long oldest_jif;
        long wrote = 0;
@@ -802,7 +743,10 @@ static long wb_writeback(struct bdi_writeback *wb,
                wbc.more_io = 0;
                wbc.nr_to_write = MAX_WRITEBACK_PAGES;
                wbc.pages_skipped = 0;
-               writeback_inodes_wb(wb, &wbc);
+               if (args->sb)
+                       __writeback_inodes_sb(args->sb, wb, &wbc);
+               else
+                       writeback_inodes_wb(wb, &wbc);
                args->nr_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
                wrote += MAX_WRITEBACK_PAGES - wbc.nr_to_write;
 
@@ -912,7 +856,6 @@ long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
 
        while ((work = get_next_work_item(bdi, wb)) != NULL) {
                struct wb_writeback_args args = work->args;
-               int post_clear;
 
                /*
                 * Override sync mode, in case we must wait for completion
@@ -920,13 +863,11 @@ long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
                if (force_wait)
                        work->args.sync_mode = args.sync_mode = WB_SYNC_ALL;
 
-               post_clear = WB_SYNC_ALL || args.sb_pinned;
-
                /*
                 * If this isn't a data integrity operation, just notify
                 * that we have seen this work and we are now starting it.
                 */
-               if (!post_clear)
+               if (!test_bit(WS_ONSTACK, &work->state))
                        wb_clear_pending(wb, work);
 
                wrote += wb_writeback(wb, &args);
@@ -935,7 +876,7 @@ long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
                 * This is a data integrity writeback, so only do the
                 * notification when we have completed the work.
                 */
-               if (post_clear)
+               if (test_bit(WS_ONSTACK, &work->state))
                        wb_clear_pending(wb, work);
        }
 
@@ -993,42 +934,32 @@ int bdi_writeback_task(struct bdi_writeback *wb)
 }
 
 /*
- * Schedule writeback for all backing devices. This does WB_SYNC_NONE
- * writeback, for integrity writeback see bdi_sync_writeback().
+ * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
+ * the whole world.
  */
-static void bdi_writeback_all(struct super_block *sb, long nr_pages)
+void wakeup_flusher_threads(long nr_pages)
 {
+       struct backing_dev_info *bdi;
        struct wb_writeback_args args = {
-               .sb             = sb,
-               .nr_pages       = nr_pages,
                .sync_mode      = WB_SYNC_NONE,
        };
-       struct backing_dev_info *bdi;
 
-       rcu_read_lock();
+       if (nr_pages) {
+               args.nr_pages = nr_pages;
+       } else {
+               args.nr_pages = global_page_state(NR_FILE_DIRTY) +
+                               global_page_state(NR_UNSTABLE_NFS);
+       }
 
+       rcu_read_lock();
        list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
                if (!bdi_has_dirty_io(bdi))
                        continue;
-
-               bdi_alloc_queue_work(bdi, &args, 0);
+               bdi_alloc_queue_work(bdi, &args);
        }
-
        rcu_read_unlock();
 }
 
-/*
- * Start writeback of `nr_pages' pages.  If `nr_pages' is zero, write back
- * the whole world.
- */
-void wakeup_flusher_threads(long nr_pages)
-{
-       if (nr_pages == 0)
-               nr_pages = global_page_state(NR_FILE_DIRTY) +
-                               global_page_state(NR_UNSTABLE_NFS);
-       bdi_writeback_all(NULL, nr_pages);
-}
-
 static noinline void block_dump___mark_inode_dirty(struct inode *inode)
 {
        if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
@@ -1220,18 +1151,6 @@ static void wait_sb_inodes(struct super_block *sb)
        iput(old_inode);
 }
 
-static void __writeback_inodes_sb(struct super_block *sb, int sb_locked)
-{
-       unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
-       unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
-       long nr_to_write;
-
-       nr_to_write = nr_dirty + nr_unstable +
-                       (inodes_stat.nr_inodes - inodes_stat.nr_unused);
-
-       bdi_start_writeback(sb->s_bdi, sb, nr_to_write, sb_locked);
-}
-
 /**
  * writeback_inodes_sb -       writeback dirty inodes from given super_block
  * @sb: the superblock
@@ -1243,21 +1162,21 @@ static void __writeback_inodes_sb(struct super_block *sb, int sb_locked)
  */
 void writeback_inodes_sb(struct super_block *sb)
 {
-       __writeback_inodes_sb(sb, 0);
-}
-EXPORT_SYMBOL(writeback_inodes_sb);
+       unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
+       unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
+       struct wb_writeback_args args = {
+               .sb             = sb,
+               .sync_mode      = WB_SYNC_NONE,
+       };
 
-/**
- * writeback_inodes_sb_locked  - writeback dirty inodes from given super_block
- * @sb: the superblock
- *
- * Like writeback_inodes_sb(), except the caller already holds the
- * sb umount sem.
- */
-void writeback_inodes_sb_locked(struct super_block *sb)
-{
-       __writeback_inodes_sb(sb, 1);
+       WARN_ON(!rwsem_is_locked(&sb->s_umount));
+
+       args.nr_pages = nr_dirty + nr_unstable +
+                       (inodes_stat.nr_inodes - inodes_stat.nr_unused);
+
+       bdi_queue_work_onstack(&args);
 }
+EXPORT_SYMBOL(writeback_inodes_sb);
 
 /**
  * writeback_inodes_sb_if_idle -       start writeback if none underway
@@ -1269,7 +1188,9 @@ void writeback_inodes_sb_locked(struct super_block *sb)
 int writeback_inodes_sb_if_idle(struct super_block *sb)
 {
        if (!writeback_in_progress(sb->s_bdi)) {
+               down_read(&sb->s_umount);
                writeback_inodes_sb(sb);
+               up_read(&sb->s_umount);
                return 1;
        } else
                return 0;
@@ -1285,7 +1206,16 @@ EXPORT_SYMBOL(writeback_inodes_sb_if_idle);
  */
 void sync_inodes_sb(struct super_block *sb)
 {
-       bdi_sync_writeback(sb->s_bdi, sb);
+       struct wb_writeback_args args = {
+               .sb             = sb,
+               .sync_mode      = WB_SYNC_ALL,
+               .nr_pages       = LONG_MAX,
+               .range_cyclic   = 0,
+       };
+
+       WARN_ON(!rwsem_is_locked(&sb->s_umount));
+
+       bdi_queue_work_onstack(&args);
        wait_sb_inodes(sb);
 }
 EXPORT_SYMBOL(sync_inodes_sb);