Skip to contentSkip to conversation

dm_queue: add dm_queue and generic worker thread (take 2)

PATCH New Kiyoshi Ueda 1 participant 1 message Jul 10, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/4 dm_queue: add dm_queue and generic worker thread (take 2)

This patch adds device-mapper generic queue feature (dm_queue) so that targets can use it instead of using its own queuing mechanism.

The patch is for 2.6.18-rc1-mm1.

Regards,
Kiyoshi Ueda

drivers/md/dm-queue.c +168 -0
diff -rupN 2.6.18-rc1-mm1/drivers/md/dm-queue.c 2.6.18-rc1-mm1.dmq/drivers/md/dm-queue.c
@@ -0,0 +1,168 @@
+/*
+ * Copyright (C) 2006 NEC Corporation
+ *
+ * This file is released under the GPL.
+ */
+
+#include "dm.h"
+#include "dm-bio-list.h"
+
+#include <linux/workqueue.h>
+
+struct workqueue_struct *kdmqd;
+
+struct dm_queue {
+	spinlock_t lock; /* protects .bios and .size */
+	struct bio_list bios;
+	unsigned int size;
+
+	struct dm_target *ti;
+	struct work_struct work;
+};
+
+int dm_queue_init()
+{
+	kdmqd = create_workqueue("dm_queue");
+	if (!kdmqd)
+		return -ENOMEM;
+
+	return 0;
+}
+
+void dm_queue_exit()
+{
+	destroy_workqueue(kdmqd);
+}
+
+struct dm_queue *dm_queue_alloc(int nr)
+{
+	struct dm_queue *qs;
+
+	if (!nr)
+		return NULL;
+
+	qs = kzalloc(sizeof(struct dm_queue) * nr, GFP_KERNEL);
+
+	return qs;
+}
+EXPORT_SYMBOL_GPL(dm_queue_alloc);
+
+void dm_queue_free(struct dm_queue *qs)
+{
+	if (qs)
+		kfree(qs);
+}
+EXPORT_SYMBOL_GPL(dm_queue_free);
+
+void dm_queue_flush(struct dm_queue *q)
+{
+	/*
+	 * The work queue is shared by multiple targets and we cannot
+	 * flush the specific queue.
+	 */
+	flush_workqueue(kdmqd);
+}
+EXPORT_SYMBOL_GPL(dm_queue_flush);
+
+struct dm_queue *dm_queue_find(struct dm_queue *qs, unsigned int index)
+{
+	if (!qs)
+		return NULL;
+
+	return qs + index;
+}
+EXPORT_SYMBOL_GPL(dm_queue_find);
+
+int dm_queue_setup(struct dm_queue *q, void (*work)(void *),
+		    struct dm_target *ti)
+{
+	spin_lock_init(&q->lock);
+	bio_list_init(&q->bios);
+	q->size = 0;
+	q->ti = ti;
+	INIT_WORK(&q->work, work, q);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(dm_queue_setup);
+
+int dm_queue_add_bio(struct dm_queue *q, struct bio *bio)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&q->lock, flags);
+	bio_list_add(&q->bios, bio);
+	q->size++;
+	spin_unlock_irqrestore(&q->lock, flags);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(dm_queue_add_bio);
+
+struct bio *dm_queue_pop_bio(struct dm_queue *q)
+{
+	struct bio *bio = NULL;
+	unsigned long flags;
+
+	spin_lock_irqsave(&q->lock, flags);
+
+	bio = bio_list_pop(&q->bios);
+	if (bio)
+		q->size--;
+
+	spin_unlock_irqrestore(&q->lock, flags);
+
+	return bio;
+}
+EXPORT_SYMBOL_GPL(dm_queue_pop_bio);
+
+struct bio *dm_queue_get_bios(struct dm_queue *q)
+{
+	struct bio *bio = NULL;
+	unsigned long flags;
+
+	spin_lock_irqsave(&q->lock, flags);
+
+	bio = bio_list_get(&q->bios);
+	q->size = 0;
+
+	spin_unlock_irqrestore(&q->lock, flags);
+
+	return bio;
+}
+EXPORT_SYMBOL_GPL(dm_queue_get_bios);
+
+unsigned int dm_queue_size(struct dm_queue *q)
+{
+	unsigned int s;
+	unsigned long flags;
+
+	spin_lock_irqsave(&q->lock, flags);
+	s = q->size;
+	spin_unlock_irqrestore(&q->lock, flags);
+
+	return s;
+}
+EXPORT_SYMBOL_GPL(dm_queue_size);
+
+struct dm_target *dm_queue_get_target(struct dm_queue *q)
+{
+	return q->ti;
+}
+EXPORT_SYMBOL_GPL(dm_queue_get_target);
+
+void dm_queue_process(struct dm_queue *q)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&q->lock, flags);
+
+	if (!q->size)
+		goto out;
+
+	queue_work(kdmqd, &q->work);
+
+out:
+	spin_unlock_irqrestore(&q->lock, flags);
+}
+EXPORT_SYMBOL_GPL(dm_queue_process);
include/linux/device-mapper.h +54 -0

71 lines not on this page · raw message

drivers/md/dm.h +6 -0
diff -rupN 2.6.18-rc1-mm1/drivers/md/dm.h 2.6.18-rc1-mm1.dmq/drivers/md/dm.h
@@ -58,6 +58,12 @@ void dm_table_unplug_all(struct dm_table
 int dm_table_flush_all(struct dm_table *t);
 
 /*-----------------------------------------------------------------
+ * Queue functions.
+ *---------------------------------------------------------------*/
+int dm_queue_init(void);
+void dm_queue_exit(void);
+
+/*-----------------------------------------------------------------
  * A registry of target types.
  *---------------------------------------------------------------*/
 int dm_target_init(void);
drivers/md/dm.c +2 -0
diff -rupN 2.6.18-rc1-mm1/drivers/md/dm.c 2.6.18-rc1-mm1.dmq/drivers/md/dm.c
@@ -177,6 +177,7 @@ static void local_exit(void)
 
 int (*_inits[])(void) __initdata = {
 	local_init,
+	dm_queue_init,
 	dm_target_init,
 	dm_linear_init,
 	dm_stripe_init,
@@ -185,6 +186,7 @@ int (*_inits[])(void) __initdata = {
 
 void (*_exits[])(void) = {
 	local_exit,
+	dm_queue_exit,
 	dm_target_exit,
 	dm_linear_exit,
 	dm_stripe_exit,
drivers/md/Makefile +1 -1
diff -rupN 2.6.18-rc1-mm1/drivers/md/Makefile 2.6.18-rc1-mm1.dmq/drivers/md/Makefile
@@ -3,7 +3,7 @@
 #
 
 dm-mod-objs	:= dm.o dm-table.o dm-target.o dm-linear.o dm-stripe.o \
-		   dm-ioctl.o dm-io.o kcopyd.o
+		   dm-ioctl.o dm-io.o kcopyd.o dm-queue.o
 dm-multipath-objs := dm-hw-handler.o dm-path-selector.o dm-mpath.o
 dm-snapshot-objs := dm-snap.o dm-exception-store.o
 dm-mirror-objs	:= dm-log.o dm-raid1.o