Reconnect: If we are logged in and we experience a session failure, then
try to re-connect and redrive all I/O Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
This commit is contained in:
109
lib/connect.c
109
lib/connect.c
@@ -15,8 +15,11 @@
|
||||
along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include "slist.h"
|
||||
#include "iscsi.h"
|
||||
#include "iscsi-private.h"
|
||||
#include "scsi-lowlevel.h"
|
||||
@@ -109,6 +112,9 @@ iscsi_full_connect_async(struct iscsi_context *iscsi, const char *portal,
|
||||
{
|
||||
struct connect_task *ct;
|
||||
|
||||
iscsi->lun = lun;
|
||||
iscsi->portal = strdup(portal);
|
||||
|
||||
ct = malloc(sizeof(struct connect_task));
|
||||
if (ct == NULL) {
|
||||
iscsi_set_error(iscsi, "Out-of-memory. Failed to allocate "
|
||||
@@ -124,3 +130,106 @@ iscsi_full_connect_async(struct iscsi_context *iscsi, const char *portal,
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int iscsi_reconnect(struct iscsi_context *old_iscsi)
|
||||
{
|
||||
struct iscsi_context *iscsi;
|
||||
|
||||
try_again:
|
||||
|
||||
iscsi = iscsi_create_context(old_iscsi->initiator_name);
|
||||
iscsi->is_reconnecting = 1;
|
||||
|
||||
iscsi_set_targetname(iscsi, old_iscsi->target_name);
|
||||
|
||||
iscsi_set_header_digest(iscsi, old_iscsi->want_header_digest);
|
||||
|
||||
if (old_iscsi->user != NULL) {
|
||||
iscsi_set_initiator_username_pwd(iscsi, old_iscsi->user, old_iscsi->passwd);
|
||||
}
|
||||
|
||||
iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL);
|
||||
|
||||
iscsi->lun = old_iscsi->lun;
|
||||
|
||||
iscsi->portal = strdup(old_iscsi->portal);
|
||||
|
||||
if (iscsi_full_connect_sync(iscsi, iscsi->portal, iscsi->lun) != 0) {
|
||||
iscsi_destroy_context(iscsi);
|
||||
sleep(1);
|
||||
goto try_again;
|
||||
}
|
||||
|
||||
while (old_iscsi->waitpdu) {
|
||||
struct iscsi_pdu *pdu = old_iscsi->waitpdu;
|
||||
|
||||
SLIST_REMOVE(&old_iscsi->waitpdu, pdu);
|
||||
if (pdu->itt == 0xffffffff) {
|
||||
continue;
|
||||
}
|
||||
|
||||
pdu->itt = iscsi->itt++;
|
||||
iscsi_pdu_set_itt(pdu, pdu->itt);
|
||||
|
||||
pdu->cmdsn = iscsi->cmdsn++;
|
||||
iscsi_pdu_set_cmdsn(pdu, pdu->cmdsn);
|
||||
|
||||
iscsi_pdu_set_expstatsn(pdu, iscsi->statsn);
|
||||
iscsi->statsn++;
|
||||
|
||||
pdu->written = 0;
|
||||
SLIST_ADD_END(&iscsi->outqueue, pdu);
|
||||
}
|
||||
|
||||
while (old_iscsi->outqueue) {
|
||||
struct iscsi_pdu *pdu = old_iscsi->outqueue;
|
||||
|
||||
SLIST_REMOVE(&old_iscsi->outqueue, pdu);
|
||||
if (pdu->itt == 0xffffffff) {
|
||||
continue;
|
||||
}
|
||||
|
||||
pdu->itt = iscsi->itt++;
|
||||
iscsi_pdu_set_itt(pdu, pdu->itt);
|
||||
|
||||
pdu->cmdsn = iscsi->cmdsn++;
|
||||
iscsi_pdu_set_cmdsn(pdu, pdu->cmdsn);
|
||||
|
||||
iscsi_pdu_set_expstatsn(pdu, iscsi->statsn);
|
||||
iscsi->statsn++;
|
||||
|
||||
pdu->written = 0;
|
||||
SLIST_ADD_END(&iscsi->outqueue, pdu);
|
||||
}
|
||||
|
||||
if (dup2(iscsi->fd, old_iscsi->fd) == -1) {
|
||||
iscsi_destroy_context(iscsi);
|
||||
goto try_again;
|
||||
}
|
||||
|
||||
|
||||
free(discard_const(old_iscsi->initiator_name));
|
||||
free(discard_const(old_iscsi->target_name));
|
||||
free(discard_const(old_iscsi->target_address));
|
||||
free(discard_const(old_iscsi->alias));
|
||||
free(discard_const(old_iscsi->portal));
|
||||
if (old_iscsi->incoming != NULL) {
|
||||
iscsi_free_iscsi_in_pdu(old_iscsi->incoming);
|
||||
}
|
||||
if (old_iscsi->inqueue != NULL) {
|
||||
iscsi_free_iscsi_inqueue(old_iscsi->inqueue);
|
||||
}
|
||||
free(old_iscsi->error_string);
|
||||
free(discard_const(old_iscsi->user));
|
||||
free(discard_const(old_iscsi->passwd));
|
||||
free(discard_const(old_iscsi->chap_c));
|
||||
|
||||
close(iscsi->fd);
|
||||
iscsi->fd = old_iscsi->fd;
|
||||
memcpy(old_iscsi, iscsi, sizeof(struct iscsi_context));
|
||||
free(iscsi);
|
||||
|
||||
old_iscsi->is_reconnecting = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user