From: Alasdair G Kergon <[email-masked]>

Add a workqueue so that snapshot bios can be flushed in a separate thread
when necessary.

A new per-snapshot spinlock pe_lock is introduced to protect snapshot_bios.
---

Index: linux-2.6.14-rc2/drivers/md/dm-snap.c
===================================================================
--- linux-2.6.14-rc2.orig/drivers/md/dm-snap.c	2006-01-06 21:50:23.000000000 +0000
+++ linux-2.6.14-rc2/drivers/md/dm-snap.c	2006-01-09 15:39:46.000000000 +0000
@@ -18,6 +18,7 @@
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
+#include <linux/workqueue.h>
 
 #include "dm-snap.h"
 #include "dm-bio-list.h"
@@ -38,6 +39,9 @@
  */
 #define SNAPSHOT_PAGES 256
 
+struct workqueue_struct *ksnapd;
+static void process_snapshot_bios(void *data);
+
 struct pending_exception {
 	struct exception e;
 
@@ -476,6 +480,7 @@ static int snapshot_ctr(struct dm_target
 	s->active = 0;
 	s->last_percent = 0;
 	init_rwsem(&s->lock);
+	spin_lock_init(&s->pe_lock);
 	s->table = ti->table;
 
 	/* Allocate hash table for COW data */
@@ -511,6 +516,8 @@ static int snapshot_ctr(struct dm_target
 	/* Metadata must only be loaded into one table at once */
 	read_snapshot_metadata(s);
 
+	INIT_WORK(&s->process_snapshot_bios, process_snapshot_bios, s);
+
 	/* Add snapshot to the list of snapshots for this origin */
 	/* Exceptions aren't triggered till snapshot_resume() is called */
 	if (register_snapshot(s)) {
@@ -549,6 +556,8 @@ static void snapshot_dtr(struct dm_targe
 {
 	struct dm_snapshot *s = (struct dm_snapshot *) ti->private;
 
+	flush_workqueue(ksnapd);
+
 	unregister_snapshot(s);
 
 	exit_exception_table(&s->pending, pending_cache);
@@ -578,6 +587,19 @@ static void flush_bios(struct bio *bio)
 	}
 }
 
+static void process_snapshot_bios(void *data)
+{
+	struct dm_snapshot *s = (struct dm_snapshot *) data;
+	struct bio *snapshot_bios;
+	unsigned long flags;
+
+	spin_lock_irqsave(&s->pe_lock, flags);
+	snapshot_bios = bio_list_get(&s->snapshot_bios);
+	spin_unlock_irqrestore(&s->pe_lock, flags);
+
+	flush_bios(snapshot_bios);
+}
+
 /*
  * Error a list of buffers.
  */
@@ -1179,8 +1201,18 @@ static int __init dm_snapshot_init(void)
 		goto bad5;
 	}
 
+	ksnapd = create_singlethread_workqueue("ksnapd");
+	if (!ksnapd) {
+		DMERR("%s: failed to create workqueue ksnapd",
+		      origin_target.name);
+		r = -ENOMEM;
+		goto bad6;
+	}
+
 	return 0;
 
+      bad6:
+	mempool_destroy(pending_pool);
       bad5:
 	kmem_cache_destroy(pending_cache);
       bad4:
@@ -1198,6 +1230,8 @@ static void __exit dm_snapshot_exit(void
 {
 	int r;
 
+	destroy_workqueue(ksnapd);
+
 	r = dm_unregister_target(&snapshot_target);
 	if (r)
 		DMERR("snapshot unregister failed %d", r);
Index: linux-2.6.14-rc2/drivers/md/dm-snap.h
===================================================================
--- linux-2.6.14-rc2.orig/drivers/md/dm-snap.h	2006-01-06 21:50:23.000000000 +0000
+++ linux-2.6.14-rc2/drivers/md/dm-snap.h	2006-01-09 15:42:27.000000000 +0000
@@ -10,7 +10,9 @@
 #define DM_SNAPSHOT_H
 
 #include "dm.h"
+#include "dm-bio-list.h"
 #include <linux/blkdev.h>
+#include <linux/workqueue.h>
 
 struct exception_table {
 	uint32_t hash_mask;
@@ -112,10 +114,19 @@ struct dm_snapshot {
 	struct exception_table pending;
 	struct exception_table complete;
 
+	/*
+	 * pe_lock protects all pending_exception operations and access
+	 * as well as the snapshot_bios list.
+	 */
+	spinlock_t pe_lock;
+
 	/* The on disk metadata handler */
 	struct exception_store store;
 
 	struct kcopyd_client *kcopyd_client;
+
+	struct work_struct process_snapshot_bios;
+	struct bio_list snapshot_bios;
 };
 
 /*