[Stgt-devel] modularization target types.

Hu Gang hugang
Thu Mar 29 08:07:57 CEST 2007


this patch adding support mutiple scsi type as a modules.

Signed-off-by: hu gang <hugang at soulinfo.com>

---

 usr/mmc.c    |    5 +++++
 usr/osd.c    |    5 +++++
 usr/sbc.c    |    5 +++++
 usr/target.c |   61 +++++++++++++++++++++++++++++++++++++++++++---------------
 usr/tgtd.h   |    4 ++++
 5 files changed, 64 insertions(+), 16 deletions(-)

diff --git a/usr/mmc.c b/usr/mmc.c
index 4eab50b..cbba763 100644
--- a/usr/mmc.c
+++ b/usr/mmc.c
@@ -213,3 +213,8 @@ struct device_type_template mmc_template
 		[0xb0 ... 0xff] = {spc_illegal_op},
 	}
 };
+
+__attribute__((constructor)) static void mmc_init(void)
+{
+    device_type_register(&mmc_template);
+}
diff --git a/usr/osd.c b/usr/osd.c
index da3853b..1612a20 100644
--- a/usr/osd.c
+++ b/usr/osd.c
@@ -209,3 +209,8 @@ struct device_type_template osd_template
 		[0xb0 ... 0xff] = {spc_illegal_op},
 	}
 };
+
+__attribute__((constructor)) static void osd_init(void)
+{
+        device_type_register(&osd_template);
+}
diff --git a/usr/sbc.c b/usr/sbc.c
index edcd6e8..66c0c2e 100644
--- a/usr/sbc.c
+++ b/usr/sbc.c
@@ -465,3 +465,8 @@ struct device_type_template sbc_template
 		[0xb0 ... 0xff] = {spc_illegal_op},
 	}
 };
+
+__attribute__((constructor)) static void sbc_init(void)
+{
+        device_type_register(&sbc_template);
+}
diff --git a/usr/target.c b/usr/target.c
index b49b143..e986985 100644
--- a/usr/target.c
+++ b/usr/target.c
@@ -36,8 +36,37 @@ #include "target.h"
 #include "scsi.h"
 #include "tgtadm.h"
 
-extern struct device_type_template sbc_template, mmc_template, osd_template,
-	spt_template;
+static LIST_HEAD(type_list);
+
+int device_type_register(struct device_type_template *t)
+{
+    list_add_tail(&t->list_entry, &type_list);
+    return 0;
+}
+
+static struct device_type_template *device_type_find_name(const char *name)
+{
+    struct device_type_template *t;
+    
+    list_for_each_entry(t, &type_list, list_entry) {
+        if (strcmp(name, t->name) == 0)
+            return t;
+    }
+
+    return NULL;
+}
+
+static struct device_type_template *device_type_find_type(unsigned char type)
+{
+    struct device_type_template *t;
+    
+    list_for_each_entry(t, &type_list, list_entry) {
+        if (t->type == type)
+            return t;
+    }
+
+    return NULL;
+}
 
 static LIST_HEAD(target_list);
 
@@ -205,6 +234,7 @@ int tgt_device_create(int tid, uint64_t 
 	struct target *target;
 	struct scsi_lu *lu, *pos;
     struct backingstore_template *bst;
+    struct device_type_template *tt;
 
 	dprintf("%d %" PRIu64 "\n", tid, lun);
 
@@ -237,23 +267,12 @@ int tgt_device_create(int tid, uint64_t 
 		return TGTADM_NOMEM;
     lu->bst = bst;
 
-	switch (t_type) {
-	case TYPE_DISK:
-		lu->dev_type_template = sbc_template;
-		break;
-	case TYPE_ROM:
-		lu->dev_type_template = mmc_template;
-		break;
-	case TYPE_OSD:
-		lu->dev_type_template = osd_template;
-		break;
-	case TYPE_SPT:
-		lu->dev_type_template = spt_template;
-		break;
-	default:
+    tt = device_type_find_type(t_type);
+    if (tt == NULL) {
 		free(lu);
 		return TGTADM_INVALID_REQUEST;
 	}
+    lu->dev_type_template = *tt;
 
 	err = tgt_device_path_update(target, lu, p);
 	if (err) {
@@ -1089,6 +1108,7 @@ int tgt_target_show_all(char *buf, int r
 	struct scsi_lu *lu;
 	struct acl_entry *acl;
 	struct it_nexus *nexus;
+    struct device_type_template *dtt;
 
 	list_for_each_entry(target, &target_list, target_siblings) {
 		shprintf(total, buf, rest,
@@ -1148,6 +1168,15 @@ int tgt_target_show_all(char *buf, int r
 		list_for_each_entry(acl, &target->acl_list, aclent_list)
 			shprintf(total, buf, rest, _TAB2 "%s\n", acl->address);
 	}
+
+    shprintf(total, buf, rest, "Device type:\n");
+    list_for_each_entry(dtt, &type_list, list_entry) {
+        shprintf(total, buf, rest, 
+                _TAB1 "Device Type: %d\n"
+                _TAB2 "Device Name: %s\n"
+                _TAB2 "Device Pid : %s\n",
+                dtt->type, dtt->name, dtt->pid ? dtt->pid : "");
+    }
 	return total;
 overflow:
 	return max;
diff --git a/usr/tgtd.h b/usr/tgtd.h
index de7d889..413271e 100644
--- a/usr/tgtd.h
+++ b/usr/tgtd.h
@@ -49,8 +49,12 @@ struct device_type_template {
 	void (*device_init)(struct scsi_lu *dev);
 
 	struct device_type_operations ops[256];
+
+    struct list_head list_entry;
 };
 
+extern int device_type_register(struct device_type_template *);
+
 struct scsi_lu {
 	int fd;
 	uint64_t addr; /* persistent mapped address */



More information about the stgt mailing list