[Stgt-devel] [patch] support different type in one target.

Hu Gang hugang
Thu Mar 29 07:35:48 CEST 2007


support different type in one target.

all of the logical units at same target must same type. this patch extend current tgt to support different type in one target.
 
./tgtadm --lld iscsi --op new --mode target --tid 1 -T iqn.2001-04.com.example:storage.disk2.amiens.sys1.xyz
./tgtadm --lld iscsi --op new --mode logicalunit --tid 1 --lun 0 -b /p/ref/ubuntu-7.04-beta-alternate-i386.iso --target-type cd
./tgtadm --lld iscsi --op new --mode logicalunit --tid 1 --lun 1 -b /tmp/100M --target-type disk
./tgtadm --lld iscsi --op bind --mode target --tid 1 -I ALL
./tgtadm --lld iscsi --op show --mode target

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

 usr/bs_aio.c |    2 +-
 usr/mgmt.c   |    5 ++--
 usr/mmc.c    |    2 +-
 usr/osd.c    |    2 +-
 usr/sbc.c    |    2 +-
 usr/scsi.c   |    2 +-
 usr/spc.c    |    4 ++-
 usr/spt.c    |    2 +-
 usr/target.c |   68 ++++++++++++++++++++++++++++++----------------------------
 usr/target.h |    5 ----
 usr/tgtd.h   |   38 ++++++++++++++++++++------------
 11 files changed, 68 insertions(+), 64 deletions(-)

diff --git a/usr/bs_aio.c b/usr/bs_aio.c
index c10be4b..733265f 100644
--- a/usr/bs_aio.c
+++ b/usr/bs_aio.c
@@ -77,7 +77,7 @@ retry:
 
 	ret = io_getevents(info->ctx, 1, MAX_AIO_REQS, info->events, NULL);
 	nr = ret;
-	dprintf("%d", ret);
+	dprintf("%d,%d\n", ret, errno);
 	if (nr > 0) {
 	rewrite:
 		ret = write(info->done_fd[1], &nr, sizeof(nr));
diff --git a/usr/mgmt.c b/usr/mgmt.c
index ce73425..aa135ee 100644
--- a/usr/mgmt.c
+++ b/usr/mgmt.c
@@ -77,8 +77,7 @@ static int target_mgmt(int lld_no, struc
 
 	switch (req->op) {
 	case OP_NEW:
-		err = tgt_target_create(lld_no, req->tid, mtask->buf,
-					req->target_type);
+		err = tgt_target_create(lld_no, req->tid, mtask->buf);
 		break;
 	case OP_DELETE:
 		err = tgt_target_destroy(req->tid);
@@ -169,7 +168,7 @@ static int device_mgmt(int lld_no, struc
 
 	switch (req->op) {
 	case OP_NEW:
-		err = tgt_device_create(req->tid, req->lun, params);
+		err = tgt_device_create(req->tid, req->lun, req->target_type, params);
 		break;
 	case OP_DELETE:
 		err = tgt_device_destroy(req->tid, req->lun);
diff --git a/usr/mmc.c b/usr/mmc.c
index 6c6acfb..4eab50b 100644
--- a/usr/mmc.c
+++ b/usr/mmc.c
@@ -35,7 +35,7 @@ static int mmc_rw(int host_no, struct sc
 	int ret;
 
 	cmd->offset = (scsi_rw_offset(cmd->scb) << MMC_BLK_SHIFT);
-	ret = cmd->c_target->bst->bs_cmd_submit(cmd);
+	ret = cmd->dev->bst->bs_cmd_submit(cmd);
 	if (ret) {
 		cmd->offset = 0;
 		cmd->len = 0;
diff --git a/usr/osd.c b/usr/osd.c
index 26cc511..da3853b 100644
--- a/usr/osd.c
+++ b/usr/osd.c
@@ -130,7 +130,7 @@ static int osd_varlen_cdb(int host_no, s
 	}
 
 /* 	return SAM_STAT_GOOD; */
-	return cmd->c_target->bst->bs_cmd_submit(cmd);
+	return cmd->dev->bst->bs_cmd_submit(cmd);
 }
 
 static void device_osd_init(struct scsi_lu *lu)
diff --git a/usr/sbc.c b/usr/sbc.c
index f4f625f..edcd6e8 100644
--- a/usr/sbc.c
+++ b/usr/sbc.c
@@ -53,7 +53,7 @@ static int sbc_rw(int host_no, struct sc
 	}
 
 	cmd->offset = (scsi_rw_offset(cmd->scb) << BLK_SHIFT);
-	ret = cmd->c_target->bst->bs_cmd_submit(cmd);
+	ret = cmd->dev->bst->bs_cmd_submit(cmd);
 	if (ret) {
 		key = HARDWARE_ERROR;
 		asc = 0;
diff --git a/usr/scsi.c b/usr/scsi.c
index dbc1474..d616cf4 100644
--- a/usr/scsi.c
+++ b/usr/scsi.c
@@ -119,5 +119,5 @@ uint64_t scsi_rw_offset(uint8_t *scb)
 int scsi_cmd_perform(int host_no, struct scsi_cmd *cmd)
 {
 	unsigned char op = cmd->scb[0];
-	return cmd->c_target->dev_type_template.ops[op].cmd_perform(host_no, cmd);
+	return cmd->dev->dev_type_template.ops[op].cmd_perform(host_no, cmd);
 }
diff --git a/usr/spc.c b/usr/spc.c
index ef8b62d..f5c7c86 100644
--- a/usr/spc.c
+++ b/usr/spc.c
@@ -39,8 +39,8 @@ int spc_inquiry(int host_no, struct scsi
 	int len, ret = SAM_STAT_CHECK_CONDITION;
 	uint8_t *data;
 	uint8_t *scb = cmd->scb;
-	unsigned char device_type = cmd->c_target->dev_type_template.type;
-	char *product_id = cmd->c_target->dev_type_template.pid;
+	unsigned char device_type = cmd->dev->dev_type_template.type;
+	char *product_id = cmd->dev->dev_type_template.pid;
 	unsigned char key = ILLEGAL_REQUEST, asc = 0x24;
 
 	if (((scb[1] & 0x3) == 0x3) || (!(scb[1] & 0x3) && scb[2]))
diff --git a/usr/spt.c b/usr/spt.c
index ffcda5e..3d6b97d 100644
--- a/usr/spt.c
+++ b/usr/spt.c
@@ -67,7 +67,7 @@ static int spt_cmd_perform(int host_no, 
 	struct device_type_operations *ops;
 
 	if (!cmd->dev) {
-		ops = cmd->c_target->dev_type_template.ops;
+		ops = cmd->dev->dev_type_template.ops;
 		return ops[cmd->scb[0]].cmd_perform(host_no, cmd);
 	}
 
diff --git a/usr/target.c b/usr/target.c
index f48839e..b49b143 100644
--- a/usr/target.c
+++ b/usr/target.c
@@ -166,7 +166,7 @@ static int tgt_device_path_update(struct
 	if (!path)
 		return TGTADM_NOMEM;
 
-	err = target->bst->bs_open(lu, path, &dev_fd, &size);
+	err = lu->bst->bs_open(lu, path, &dev_fd, &size);
 	if (err) {
 		free(path);
 		return TGTADM_INVALID_REQUEST;
@@ -198,12 +198,13 @@ __device_lookup(int tid, uint64_t lun, s
 	return lu;
 }
 
-int tgt_device_create(int tid, uint64_t lun, char *args)
+int tgt_device_create(int tid, uint64_t lun, uint32_t t_type, char *args)
 {
 	char *p;
 	int err;
 	struct target *target;
 	struct scsi_lu *lu, *pos;
+    struct backingstore_template *bst;
 
 	dprintf("%d %" PRIu64 "\n", tid, lun);
 
@@ -225,9 +226,34 @@ int tgt_device_create(int tid, uint64_t 
 		return TGTADM_INVALID_REQUEST;
 	p++;
 
-	lu = zalloc(sizeof(*lu) + target->bst->bs_datasize);
+    /* FIXME */
+	if (t_type == TYPE_SPT)
+		bst = &sg_bst;
+	else
+		bst = tgt_drivers[target->lid]->default_bst;
+
+	lu = zalloc(sizeof(*lu) + bst->bs_datasize);
 	if (!lu)
 		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:
+		free(lu);
+		return TGTADM_INVALID_REQUEST;
+	}
 
 	err = tgt_device_path_update(target, lu, p);
 	if (err) {
@@ -243,8 +269,8 @@ int tgt_device_create(int tid, uint64_t 
 
 	tgt_cmd_queue_init(&lu->cmd_queue);
 
-	if (target->dev_type_template.device_init)
-		target->dev_type_template.device_init(lu);
+	if (lu->dev_type_template.device_init)
+		lu->dev_type_template.device_init(lu);
 
 	list_for_each_entry(pos, &target->device_list, device_siblings) {
 		if (lu->lun < pos->lun)
@@ -275,7 +301,7 @@ int tgt_device_destroy(int tid, uint64_t
 	free(lu->path);
 	list_del(&lu->device_siblings);
 
-	target->bst->bs_close(lu);
+	lu->bst->bs_close(lu);
 	free(lu);
 	return 0;
 }
@@ -509,7 +535,7 @@ static void __cmd_done(struct target *ta
 		if (cmd->dev->addr)
 			do_munmap = 0;
 	}
-	err = target->bst->bs_cmd_done(do_munmap,
+	err = cmd->dev->bst->bs_cmd_done(do_munmap,
 				       !cmd->mmapped,
 				       cmd->uaddr, cmd->len);
 
@@ -1073,7 +1099,7 @@ int tgt_target_show_all(char *buf, int r
 			 _TAB2 "Status: %s\n",
 			 target->tid,
 			 target->name,
-			 target->dev_type_template.name,
+			 /*target->dev_type_template.name*/"",
 			 tgt_drivers[target->lid]->name,
 			 target_state_name(target->target_state));
 
@@ -1140,7 +1166,7 @@ char *tgt_targetname(int tid)
 
 #define DEFAULT_NR_ACCOUNT 16
 
-int tgt_target_create(int lld, int tid, char *args, int t_type)
+int tgt_target_create(int lld, int tid, char *args)
 {
 	int i;
 	struct target *target, *pos;
@@ -1174,24 +1200,6 @@ int tgt_target_create(int lld, int tid, 
 	if (!target)
 		return TGTADM_NOMEM;
 
-	switch (t_type) {
-	case TYPE_DISK:
-		target->dev_type_template = sbc_template;
-		break;
-	case TYPE_ROM:
-		target->dev_type_template = mmc_template;
-		break;
-	case TYPE_OSD:
-		target->dev_type_template = osd_template;
-		break;
-	case TYPE_SPT:
-		target->dev_type_template = spt_template;
-		break;
-	default:
-		free(target);
-		return TGTADM_INVALID_REQUEST;
-	}
-
 	target->name = strdup(targetname);
 	if (!target->name) {
 		free(target);
@@ -1212,12 +1220,6 @@ int tgt_target_create(int lld, int tid, 
 
 	INIT_LIST_HEAD(&target->device_list);
 
-	/* FIXME */
-	if (t_type == TYPE_SPT)
-		target->bst = &sg_bst;
-	else
-		target->bst = tgt_drivers[lld]->default_bst;
-
 	target->target_state = SCSI_TARGET_RUNNING;
 	target->lid = lld;
 
diff --git a/usr/target.h b/usr/target.h
index fe2e21e..847e4ac 100644
--- a/usr/target.h
+++ b/usr/target.h
@@ -44,14 +44,9 @@ struct target {
 
 	struct tgt_cmd_queue cmd_queue;
 
-	struct backingstore_template *bst;
-
 	struct list_head acl_list;
 
 	struct tgt_account account;
-
-	/* we don't use a pointer because a lld could change this. */
-	struct device_type_template dev_type_template;
 };
 
 struct it_nexus {
diff --git a/usr/tgtd.h b/usr/tgtd.h
index 7eecc0a..de7d889 100644
--- a/usr/tgtd.h
+++ b/usr/tgtd.h
@@ -34,6 +34,23 @@ struct tgt_cmd_queue {
 	struct list_head queue;
 };
 
+struct scsi_lu;
+struct scsi_cmd;
+
+struct device_type_operations {
+	int (*cmd_perform)(int host_no, struct scsi_cmd *cmd);
+};
+
+struct device_type_template {
+	unsigned char type;
+	char *name;
+	char *pid;
+
+	void (*device_init)(struct scsi_lu *dev);
+
+	struct device_type_operations ops[256];
+};
+
 struct scsi_lu {
 	int fd;
 	uint64_t addr; /* persistent mapped address */
@@ -54,6 +71,10 @@ struct scsi_lu {
 
 	/* TODO: needs a structure for lots of device parameters */
 	uint8_t d_sense;
+
+    struct device_type_template dev_type_template;
+    struct backingstore_template *bst;
+    void *private;
 };
 
 struct scsi_cmd {
@@ -102,32 +123,19 @@ static inline int kreq_init(void)	\
 }
 #endif
 
-struct device_type_operations {
-	int (*cmd_perform)(int host_no, struct scsi_cmd *cmd);
-};
-
-struct device_type_template {
-	unsigned char type;
-	char *name;
-	char *pid;
-
-	void (*device_init)(struct scsi_lu *dev);
-
-	struct device_type_operations ops[256];
-};
 
 extern int kspace_send_tsk_mgmt_res(uint64_t nid, uint64_t mid, int result);
 extern int kspace_send_cmd_res(uint64_t nid, int result, struct scsi_cmd *);
 
 extern int ipc_init(void);
-extern int tgt_device_create(int tid, uint64_t lun, char *args);
+extern int tgt_device_create(int tid, uint64_t lun, uint32_t type, char *args);
 extern int tgt_device_destroy(int tid, uint64_t lun);
 extern int tgt_device_update(int tid, uint64_t dev_id, char *name);
 extern int device_reserve(uint64_t nid, uint64_t lun, uint64_t reserve_id);
 extern int device_release(uint64_t nid, uint64_t lun, uint64_t reserve_id, int force);
 extern int device_reserved(uint64_t nid, uint64_t lun, uint64_t reserve_id);
 
-extern int tgt_target_create(int lld, int tid, char *args, int t_type);
+extern int tgt_target_create(int lld, int tid, char *args);
 extern int tgt_target_destroy(int tid);
 extern int tgt_target_bind(int tid, int host_no, int lld);
 extern char *tgt_targetname(int tid);



More information about the stgt mailing list