From 181dbc293bf9d0a0c9674d16db6f7d9b1ed19b49 Mon Sep 17 00:00:00 2001
From: wubo <wubo40@huawei.com>
Date: Mon, 21 Sep 2020 18:54:38 +0800
Subject: [PATCH] Fix iscsiadm op new report to cannort rename error 

Since patched 9009-fix-default-file-corrupt.patch, 
In order to solve the abnormal power failure during iscsiadm login, 
the node configuration file is cleared, and the target 
problem cannot be found after restarting.

The difference between automatically creating target nodes 
and the target nodes through the discovery process is not considered. 
The automatically created taregt node s is an regual file, 
but a directory through the discovery process. 
It is incorrect to rename a regual file as a directory

1) Automatically created
# iscsiadm -m node
9.84.7.19:3260,4294967295iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.970bb3607d7d

# ls -l /etc/iscsi/nodes/iqn.2003-01.org.linux-iscsi.localhost.x8664\:sn.970bb3607d7d/
total 4
-rw-------. 1 root root 2096 Sep 21 22:44 9.84.7.19,3260

2) Discovery process 
# iscsiadm -m node
9.84.7.19:3260,1iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.970bb3607d7d
# ls -l /etc/iscsi/nodes/iqn.2003-01.org.linux-iscsi.localhost.x8664\:sn.970bb3607d7d/
total 4
drw-------. 2 root root 4096 Sep 21 22:44 9.84.7.19,3260,1

rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN is automatic mode, 
rec->tpgt is not PORTAL_GROUP_TAG_UNKNOWN is dicovery mode

Add to support the Two mode.

Signed-off-by: Wu Bo <wubo40@huawei>
---
 usr/idbm.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/usr/idbm.c b/usr/idbm.c
index 74b1dec..50e4d7e 100644
--- a/usr/idbm.c
+++ b/usr/idbm.c
@@ -2097,6 +2097,11 @@ mkdir_portal:
 		 rec->name, rec->conn[0].address, rec->conn[0].port, rec->tpgt,
 		 rec->iface.name);
 open_conf:
+	if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN) {
+		snprintf(portal, PATH_MAX, "%s/%s/%s,%d_bak", NODE_CONFIG_DIR, 
+			 rec->name, rec->conn[0].address, rec->conn[0].port);
+	}
+	
 	f = fopen(portal, "w");
 	if (!f) {
 		log_error("Could not open %s: %s", portal, strerror(errno));
@@ -2131,10 +2136,16 @@ open_conf:
 		rc = ISCSI_ERR_IDBM;
 		goto free_portal;
 	}
+	
+	if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN) {
+		snprintf(portalDef, PATH_MAX, "%s/%s/%s,%d", NODE_CONFIG_DIR,
+			 rec->name, rec->conn[0].address, rec->conn[0].port);
+	} else {
+		snprintf(portalDef, PATH_MAX, "%s/%s/%s,%d,%d/%s", NODE_CONFIG_DIR,
+			 rec->name, rec->conn[0].address, rec->conn[0].port, rec->tpgt,
+			 rec->iface.name);		 
+	}
 
-	snprintf(portalDef, PATH_MAX, "%s/%s/%s,%d,%d/%s", NODE_CONFIG_DIR,
-		 rec->name, rec->conn[0].address, rec->conn[0].port, rec->tpgt,
-		 rec->iface.name);
 	/* Renaming default_bak->default.  */
 	if (rename(portal, portalDef) < 0) {
 		log_error("Cannot rename %s -> %s\n",   portal, portalDef);
-- 
1.8.3.1

