Skip to content

Commit

Permalink
cifs: check reconnects for channels of active tcons too
Browse files Browse the repository at this point in the history
With the new multichannel logic, when a channel needs reconnection,
the tree connect and other channels can still be active.
This fix will handle cases of checking for channel reconnect,
when the tcon does not need reconnect.

Signed-off-by: Shyam Prasad N <sprasad@microsoft.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
  • Loading branch information
Shyam Prasad N authored and Steve French committed Jan 19, 2022
1 parent e4e2787 commit 3663c90
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 19 deletions.
2 changes: 2 additions & 0 deletions fs/cifs/cifsglob.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ enum statusEnum {
CifsInSessSetup,
CifsNeedTcon,
CifsInTcon,
CifsNeedFilesInvalidate,
CifsInFilesInvalidate
};

Expand Down Expand Up @@ -923,6 +924,7 @@ struct cifs_chan {
*/
struct cifs_ses {
struct list_head smb_ses_list;
struct list_head rlist; /* reconnect list */
struct list_head tcon_list;
struct cifs_tcon *tcon_ipc;
struct mutex session_mutex;
Expand Down
34 changes: 31 additions & 3 deletions fs/cifs/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ static int __cifs_reconnect(struct TCP_Server_Info *server,
spin_unlock(&cifs_tcp_ses_lock);
cifs_swn_reset_server_dstaddr(server);
mutex_unlock(&server->srv_mutex);
mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
}
} while (server->tcpStatus == CifsNeedReconnect);

Expand Down Expand Up @@ -4404,9 +4405,22 @@ int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const stru
char *tree;
struct dfs_info3_param ref = {0};

/* only send once per connect */
spin_lock(&cifs_tcp_ses_lock);
if (tcon->ses->status != CifsGood ||
(tcon->tidStatus != CifsNew &&
tcon->tidStatus != CifsNeedTcon)) {
spin_unlock(&cifs_tcp_ses_lock);
return 0;
}
tcon->tidStatus = CifsInTcon;
spin_unlock(&cifs_tcp_ses_lock);

tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
if (!tree)
return -ENOMEM;
if (!tree) {
rc = -ENOMEM;
goto out;
}

if (tcon->ipc) {
scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
Expand Down Expand Up @@ -4438,11 +4452,18 @@ int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const stru
kfree(tree);
cifs_put_tcp_super(sb);

if (rc) {
spin_lock(&cifs_tcp_ses_lock);
tcon->tidStatus = CifsNeedTcon;
spin_unlock(&cifs_tcp_ses_lock);
}

return rc;
}
#else
int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
{
int rc;
const struct smb_version_operations *ops = tcon->ses->server->ops;

/* only send once per connect */
Expand All @@ -4456,6 +4477,13 @@ int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const stru
tcon->tidStatus = CifsInTcon;
spin_unlock(&cifs_tcp_ses_lock);

return ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
if (rc) {
spin_lock(&cifs_tcp_ses_lock);
tcon->tidStatus = CifsNeedTcon;
spin_unlock(&cifs_tcp_ses_lock);
}

return rc;
}
#endif
82 changes: 66 additions & 16 deletions fs/cifs/smb2pdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,18 @@ smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
rc = -EHOSTDOWN;
goto failed;
}
}

if (rc || !tcon->need_reconnect) {
} else {
mutex_unlock(&ses->session_mutex);
goto out;
}
mutex_unlock(&ses->session_mutex);

skip_sess_setup:
mutex_lock(&ses->session_mutex);
if (!tcon->need_reconnect) {
mutex_unlock(&ses->session_mutex);
goto out;
}
cifs_mark_open_files_invalid(tcon);
if (tcon->use_persistent)
tcon->need_reopen_files = true;
Expand Down Expand Up @@ -3787,27 +3791,35 @@ void smb2_reconnect_server(struct work_struct *work)
{
struct TCP_Server_Info *server = container_of(work,
struct TCP_Server_Info, reconnect.work);
struct cifs_ses *ses;
struct TCP_Server_Info *pserver;
struct cifs_ses *ses, *ses2;
struct cifs_tcon *tcon, *tcon2;
struct list_head tmp_list;
int tcon_exist = false;
struct list_head tmp_list, tmp_ses_list;
bool tcon_exist = false, ses_exist = false;
bool tcon_selected = false, ses_selected = false;
int rc;
int resched = false;
bool resched = false;

/* If server is a channel, select the primary channel */
pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;

/* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
mutex_lock(&server->reconnect_mutex);
mutex_lock(&pserver->reconnect_mutex);

INIT_LIST_HEAD(&tmp_list);
cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
INIT_LIST_HEAD(&tmp_ses_list);
cifs_dbg(FYI, "Reconnecting tcons and channels\n");

spin_lock(&cifs_tcp_ses_lock);
list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {

tcon_selected = ses_selected = false;

list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
if (tcon->need_reconnect || tcon->need_reopen_files) {
tcon->tc_count++;
list_add_tail(&tcon->rlist, &tmp_list);
tcon_exist = true;
tcon_selected = tcon_exist = true;
}
}
/*
Expand All @@ -3816,15 +3828,25 @@ void smb2_reconnect_server(struct work_struct *work)
*/
if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
tcon_exist = true;
tcon_selected = tcon_exist = true;
ses->ses_count++;
}
/*
* handle the case where channel needs to reconnect
* binding session, but tcon is healthy (some other channel
* is active)
*/
if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) {
list_add_tail(&ses->rlist, &tmp_ses_list);
ses_selected = ses_exist = true;
ses->ses_count++;
}
}
/*
* Get the reference to server struct to be sure that the last call of
* cifs_put_tcon() in the loop below won't release the server pointer.
*/
if (tcon_exist)
if (tcon_exist || ses_exist)
server->srv_count++;

spin_unlock(&cifs_tcp_ses_lock);
Expand All @@ -3842,13 +3864,41 @@ void smb2_reconnect_server(struct work_struct *work)
cifs_put_tcon(tcon);
}

cifs_dbg(FYI, "Reconnecting tcons finished\n");
if (!ses_exist)
goto done;

/* allocate a dummy tcon struct used for reconnect */
tcon = kzalloc(sizeof(struct cifs_tcon), GFP_KERNEL);
if (!tcon) {
resched = true;
list_del_init(&ses->rlist);
cifs_put_smb_ses(ses);
goto done;
}

tcon->tidStatus = CifsGood;
tcon->retry = false;
tcon->need_reconnect = false;

/* now reconnect sessions for necessary channels */
list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
tcon->ses = ses;
rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
if (rc)
resched = true;
list_del_init(&ses->rlist);
cifs_put_smb_ses(ses);
}
kfree(tcon);

done:
cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
if (resched)
queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
mutex_unlock(&server->reconnect_mutex);
mutex_unlock(&pserver->reconnect_mutex);

/* now we can safely release srv struct */
if (tcon_exist)
if (tcon_exist || ses_exist)
cifs_put_tcp_session(server, 1);
}

Expand Down

0 comments on commit 3663c90

Please sign in to comment.