--- open-iscsi-2.0-865/usr/discovery.c	2007/12/06 10:03:04	1.1
+++ open-iscsi-2.0-865/usr/discovery.c	2007/12/06 09:55:13
@@ -823,7 +823,7 @@
 	struct iscsi_hdr *pdu = &pdu_buffer;
 	char *data = NULL;
 	char *end_of_data;
-	int active = 0, valid_text = 0;
+	int active = 0, valid_text = 1;
 	struct timeval connection_timer, async_timer;
 	int timeout;
 	int rc;
--- open-iscsi-2.0-865/usr/idbm.c	2007/12/06 07:54:58	1.5
+++ open-iscsi-2.0-865/usr/idbm.c	2007/12/06 10:22:42
@@ -126,7 +126,7 @@
 static char *get_global_string_param(char *pathname, const char *key)
 {
 	FILE *f = NULL;
-	int c, len;
+	int len;
 	char *line, buffer[1024];
 	char *name = NULL;
 
@@ -2086,10 +2086,12 @@
 	if (!idbm_discovery_read(db, &rec, newrec->address,
 				newrec->port)) {
 		log_debug(7, "overwriting existing record");
-	} else
+	} else {
 		log_debug(7, "adding new DB record");
+		memcpy(&rec, newrec, sizeof(discovery_rec_t));
+	}
 
-	rc = idbm_discovery_write(db, newrec);
+	rc = idbm_discovery_write(db, &rec);
 	return rc;
 }
 
@@ -2151,10 +2153,16 @@
 	if (!idbm_rec_read(db, &rec, newrec->name, newrec->tpgt,
 			   newrec->conn[0].address, newrec->conn[0].port,
 			   &newrec->iface)) {
+		if ((!drec) || (drec->type == rec.disc_type &&
+				drec->port == rec.disc_port)) {
+			log_debug(7, "using existing record");
+			idbm_unlock(db);
+			return 0;
+		}
+		log_debug(7, "overwriting existing record");
 		rc = idbm_delete_node(db, NULL, &rec);
 		if (rc)
 			return rc;
-		log_debug(7, "overwriting existing record");
 	} else
 		log_debug(7, "adding new DB record");
 
@@ -2272,6 +2280,68 @@
 		log_error("can not update discovery record.");
 }
 
+/*
+ * Backwards Compat or SLP:
+ * if there is no link then this is pre svn 780 version where
+ * we did not link the disc source and node
+ */
+static int idbm_remove_disc_to_node_link(idbm_t *db, node_rec_t *rec,
+					 char *portal)
+{
+	int rc = 0, portal_alloc=0;
+	struct stat statb;
+	node_rec_t *tmprec;
+
+	tmprec = malloc(sizeof(*tmprec));
+	if (!tmprec)
+		return ENOMEM;
+
+	if (!portal) {
+		portal = malloc(PATH_MAX);
+		if (!portal)
+			return ENOMEM;
+		portal_alloc =1;
+	}
+
+	memset(portal, 0, PATH_MAX);
+	snprintf(portal, 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);
+
+	rc = __idbm_rec_read(db, tmprec, portal);
+	if (rc) {
+		/* old style recs will not have tpgt or a link so skip */
+		rc = 0;
+		goto done;
+	}
+
+	log_debug(7, "found drec %s %d\n", tmprec->disc_address,
+		 tmprec->disc_port); 
+	/* rm link from discovery source to node */
+	memset(portal, 0, PATH_MAX);
+	rc = setup_disc_to_node_link(portal, tmprec);
+	if (rc)
+		goto done;
+
+	idbm_lock(db);
+	if (!stat(portal, &statb)) {
+		if (unlink(portal)) {
+			log_error("Could not remove link %s err %d\n",
+				  portal, errno);
+			rc = errno;
+		} else
+			log_debug(7, "rmd %s", portal);
+	} else
+		log_debug(7, "Could not stat %s", portal);
+	idbm_unlock(db);
+
+done:
+	if (portal_alloc)
+		free(portal);
+	free(tmprec);
+	return rc;
+}
+
 static void idbm_rm_disc_node_links(idbm_t *db, char *disc_dir)
 {
 	char *target = NULL, *tpgt = NULL, *port = NULL;
@@ -2304,18 +2374,18 @@
 		log_debug(5, "disc removal removing link %s %s %s %s",
 			  target, address, port, iface_id);
 
-		memset(rec, 0, sizeof(*rec));	
+		memset(rec, 0, sizeof(*rec));
 		strncpy(rec->name, target, TARGET_NAME_MAXLEN);
 		rec->tpgt = atoi(tpgt);
 		rec->conn[0].port = atoi(port);
 		strncpy(rec->conn[0].address, address, NI_MAXHOST);
 		strncpy(rec->iface.name, iface_id, ISCSI_MAX_IFACE_LEN);
 
-		if (idbm_delete_node(db, NULL, rec))
-			log_error("Could not delete node %s/%s/%s,%s/%s",
+		if (idbm_remove_disc_to_node_link(db, rec, NULL))
+			log_error("Could not remove link %s/%s/%s,%s/%s",
 				  NODE_CONFIG_DIR, target, address, port,
 				  iface_id);
- 	}
+	}
 
 	closedir(disc_dirfd);
 free_rec:
@@ -2368,65 +2438,14 @@
 	return rc;
 }
 
-/*
- * Backwards Compat or SLP:
- * if there is no link then this is pre svn 780 version where
- * we did not link the disc source and node
- */
-static int idbm_remove_disc_to_node_link(idbm_t *db, node_rec_t *rec,
-					 char *portal)
-{
-	int rc = 0;
-	struct stat statb;
-	node_rec_t *tmprec;
-
-	tmprec = malloc(sizeof(*tmprec));
-	if (!tmprec)
-		return ENOMEM;
-
-	memset(portal, 0, PATH_MAX);
-	snprintf(portal, 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);
-
-	rc = __idbm_rec_read(db, tmprec, portal);
-	if (rc) {
-		/* old style recs will not have tpgt or a link so skip */
-		rc = 0;
-		goto done;
-	}
-
-	log_debug(7, "found drec %s %d\n", tmprec->disc_address,
-		 tmprec->disc_port); 
-	/* rm link from discovery source to node */
-	memset(portal, 0, PATH_MAX);
-	rc = setup_disc_to_node_link(portal, tmprec);
-	if (rc)
-		goto done;
-
-	idbm_lock(db);
-	if (!stat(portal, &statb)) {
-		if (unlink(portal)) {
-			log_error("Could not remove link %s err %d\n",
-				  portal, errno);
-			rc = errno;
-		} else
-			log_debug(7, "rmd %s", portal);
-	} else
-		log_debug(7, "Could not stat %s", portal);
-	idbm_unlock(db);
-
-done:
-	free(tmprec);
-	return rc;
-}
-
 int idbm_delete_node(idbm_t *db, void *data, node_rec_t *rec)
 {
 	struct stat statb;
 	char *portal;
 	int rc = 0, dir_rm_rc = 0;
 
+	log_debug(5, "Delete node %s/%s,%d\n",
+		  rec->name, rec->conn[0].address, rec->conn[0].port);
 	portal = calloc(1, PATH_MAX);
 	if (!portal)
 		return ENOMEM;
