Skip to content

Commit 4697bd5

Browse files
Trond MyklebustTrond Myklebust
authored andcommitted
NFSv4: Fix a race in the net namespace mount notification
Since the struct nfs_client gets added to the global nfs_client_list before it is initialised, it is possible that rpc_pipefs_event can end up trying to create idmapper entries on such a thing. The solution is to have the mount notification wait for the initialisation of each nfs_client to complete, and then to skip any entries for which the it failed. Reported-by: Stanislav Kinsbursky <[email protected]> Signed-off-by: Trond Myklebust <[email protected]> Acked-by: Stanislav Kinsbursky <[email protected]>
1 parent 7b38c36 commit 4697bd5

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

fs/nfs/client.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,17 @@ static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *dat
507507
return NULL;
508508
}
509509

510+
static bool nfs_client_init_is_complete(const struct nfs_client *clp)
511+
{
512+
return clp->cl_cons_state != NFS_CS_INITING;
513+
}
514+
515+
int nfs_wait_client_init_complete(const struct nfs_client *clp)
516+
{
517+
return wait_event_killable(nfs_client_active_wq,
518+
nfs_client_init_is_complete(clp));
519+
}
520+
510521
/*
511522
* Found an existing client. Make sure it's ready before returning.
512523
*/
@@ -516,8 +527,7 @@ nfs_found_client(const struct nfs_client_initdata *cl_init,
516527
{
517528
int error;
518529

519-
error = wait_event_killable(nfs_client_active_wq,
520-
clp->cl_cons_state < NFS_CS_INITING);
530+
error = nfs_wait_client_init_complete(clp);
521531
if (error < 0) {
522532
nfs_put_client(clp);
523533
return ERR_PTR(-ERESTARTSYS);
@@ -1333,7 +1343,7 @@ static int nfs4_init_client_minor_version(struct nfs_client *clp)
13331343
* so that the client back channel can find the
13341344
* nfs_client struct
13351345
*/
1336-
clp->cl_cons_state = NFS_CS_SESSION_INITING;
1346+
nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING);
13371347
}
13381348
#endif /* CONFIG_NFS_V4_1 */
13391349

fs/nfs/idmap.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,24 @@ static struct nfs_client *nfs_get_client_for_event(struct net *net, int event)
530530
struct nfs_net *nn = net_generic(net, nfs_net_id);
531531
struct dentry *cl_dentry;
532532
struct nfs_client *clp;
533+
int err;
533534

535+
restart:
534536
spin_lock(&nn->nfs_client_lock);
535537
list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
538+
/* Wait for initialisation to finish */
539+
if (clp->cl_cons_state == NFS_CS_INITING) {
540+
atomic_inc(&clp->cl_count);
541+
spin_unlock(&nn->nfs_client_lock);
542+
err = nfs_wait_client_init_complete(clp);
543+
nfs_put_client(clp);
544+
if (err)
545+
return NULL;
546+
goto restart;
547+
}
548+
/* Skip nfs_clients that failed to initialise */
549+
if (clp->cl_cons_state < 0)
550+
continue;
536551
if (clp->rpc_ops != &nfs_v4_clientops)
537552
continue;
538553
cl_dentry = clp->cl_idmap->idmap_pipe->dentry;

fs/nfs/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ extern struct nfs_server *nfs_clone_server(struct nfs_server *,
168168
struct nfs_fh *,
169169
struct nfs_fattr *,
170170
rpc_authflavor_t);
171+
extern int nfs_wait_client_init_complete(const struct nfs_client *clp);
171172
extern void nfs_mark_client_ready(struct nfs_client *clp, int state);
172173
extern struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
173174
const struct sockaddr *ds_addr,

0 commit comments

Comments
 (0)